0% found this document useful (0 votes)
16 views12 pages

C++ Operator and Function Overloading Examples

The document contains several C++ programs demonstrating various concepts such as binary operator overloading, function overloading, unary operator overloading, single inheritance, and multiple inheritance. Each program includes class definitions, member functions, and sample output to illustrate the functionality. The examples cover complex number operations, area calculations, employee payroll systems, and student grading systems.

Uploaded by

019praveenkumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views12 pages

C++ Operator and Function Overloading Examples

The document contains several C++ programs demonstrating various concepts such as binary operator overloading, function overloading, unary operator overloading, single inheritance, and multiple inheritance. Each program includes class definitions, member functions, and sample output to illustrate the functionality. The examples cover complex number operations, area calculations, employee payroll systems, and student grading systems.

Uploaded by

019praveenkumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Binary Operator Overloading Example Program

#include<iostream.h>

#include<conio.h>

class complex {

int a, b;

public:

void getvalue() {

cout << "Enter the value of Complex Numbers a,b:";

cin >> a>>b;

complex operator+(complex ob) {

complex t;

t.a = a + ob.a;

t.b = b + ob.b;

return (t);

complex operator-(complex ob) {

complex t;

t.a = a - ob.a;

t.b = b - ob.b;

return (t);
}

void display() {

cout << a << "+" << b << "i" << "\n";

};

void main() {

clrscr();

complex obj1, obj2, result, result1;

[Link]();

[Link]();

result = obj1 + obj2;

result1 = obj1 - obj2;

cout << "Input Values:\n";

[Link]();

[Link]();

cout << "Result:";

[Link]();

[Link]();
getch();

Sample Output:

Enter the value of Complex Numbers a, b

4 5

Enter the value of Complex Numbers a, b

2 2

Input Values

4 + 5i

2 + 2i

Result

6 + 7i

2 + 3i

Simple Program for Function Overloading


#include<iostream.h>

#include<stdlib.h>

#include<conio.h>

#define pi 3.14

class fn {

public:

void area(int); //circle

void area(int, int); //rectangle

void area(float, int, int); //triangle

};
void fn::area(int a) {

cout << "Area of Circle:" << pi * a*a;

void fn::area(int a, int b) {

cout << "Area of rectangle:" << a*b;

void fn::area(float t, int a, int b) {

cout << "Area of triangle:" << t * a*b;

void main() {

int ch;

int a, b, r;

clrscr();

fn obj;

cout << "\n\t\tFunction Overloading";

cout << "\[Link] of Circle\[Link] of Rectangle\[Link] of Triangle\[Link]\n:?;

cout << ?Enter your Choice : ";

cin>>ch;
switch (ch) {

case 1:

cout << "Enter Radious of the Circle:";

cin>>r;

[Link](r);

break;

case 2:

cout << "Enter Sides of the Rectangle:";

cin >> a>>b;

[Link](a, b);

break;

case 3:

cout << "Enter Sides of the Triangle:";

cin >> a>>b;

[Link](0.5, a, b);

break;

case 4:

exit(0);

getch();

Sample Output

Function Overloading

1. Area of Circle

2. Area of Rectangle
3. Area of Triangle

4. Exit

Enter Your Choice: 2

Enter the Sides of the Rectangle: 5 5

Area of Rectangle is: 25

1. Area of Circle

2. Area of Rectangle

3. Area of Triangle

4. Exit

Enter Your Choice: 4

Simple Program for Unary Operator Overloading


Program
#include<iostream.h>

#include<conio.h>

class complex {

int a, b, c;

public:

complex() {

void getvalue() {

cout << "Enter the Two Numbers:";

cin >> a>>b;

}
void operator++() {

a = ++a;

b = ++b;

void operator--() {

a = --a;

b = --b;

void display() {

cout << a << "+\t" << b << "i" << endl;

};

void main() {

clrscr();

complex obj;

[Link]();

obj++;

cout << "Increment Complex Number\n";


[Link]();

obj--;

cout << "Decrement Complex Number\n";

[Link]();

getch();

Sample Output
Enter the two numbers: 3 6

Increment Complex Number


4 + 7i

Decrement Complex Number

3 + 6i

Payroll System: Simple Program for Single


Inheritance Example Program
#include<iostream.h>

#include<conio.h>

class emp {

public:

int eno;

char name[20], des[20];

void get() {

cout << "Enter the employee number:";

cin>>eno;

cout << "Enter the employee name:";

cin>>name;

cout << "Enter the designation:";

cin>>des;

};

class salary : public emp {

float bp, hra, da, pf, np;

public:

void get1() {

cout << "Enter the basic pay:";

cin>>bp;
cout << "Enter the Humen Resource Allowance:";

cin>>hra;

cout << "Enter the Dearness Allowance :";

cin>>da;

cout << "Enter the Profitablity Fund:";

cin>>pf;

void calculate() {

np = bp + hra + da - pf;
}

void display() {

cout << eno << "\t" << name << "\t" << des << "\t" << bp << "\t"
<< hra << "\t" << da << "\t" << pf << "\t" << np << "\n";

};

void main() {

int i, n;

char ch;

salary s[10];

clrscr();

cout << "Enter the number of employee:";

cin>>n;

for (i = 0; i < n; i++) {

s[i].get();

s[i].get1();

s[i].calculate();

cout << "\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";

for (i = 0; i < n; i++) {

s[i].display();
}

getch();

Sample Output
Enter the Roll no: 100

Enter two marks

90

80

Enter the Sports Mark: 90

Roll No: 100

Total : 260

Average: 86.66

Simple Program for Multiple Inheritance


#include<iostream.h>

#include<conio.h>

class student {

protected:

int rno, m1, m2;

public:

void get() {

cout << "Enter the Roll no :";

cin>>rno;

cout << "Enter the two marks :";


cin >> m1>>m2;

};

class sports {

protected:

int sm; // sm = Sports mark

public:

void getsm() {
cout << "\nEnter the sports mark :";

cin>>sm;

};

class statement : public student, public sports {

int tot, avg;

public:

void display() {

tot = (m1 + m2 + sm);


avg = tot / 3;

cout << "\n\n\tRoll No : " << rno << "\n\tTotal : " <<
tot;

cout << "\n\tAverage : " << avg;

};

void main() {

clrscr();

statement obj;

[Link]();
[Link]();

[Link]();

getch();

Sample Output
Enter the Roll no: 100

Enter two marks

90

80

Enter the Sports Mark: 90

Roll No: 100

Total : 260

Average: 86.66

You might also like