0% found this document useful (0 votes)
20 views

Operator Overloading

Uploaded by

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

Operator Overloading

Uploaded by

freefire206354
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Operator Overloading:

1 write a program to negate values of two variables contained in an object. By overloading the unary
operator i.e. -.

#include<iostream>

using namespace std;

class Negate

int x,y;

public:

void read()

cout<<"Enter Two Numberss:";

cin>>x>>y;

void operator -()

x=-x;

y=-y;

void display()

cout<<"X="<<x<<"Y="<<y<<endl;

};
int main()

Negate n;

n.read();

-n;

n.display();

return 0;

Output:

C:\Users\Dhiraj\Desktop\Overloading>g++ Negate.cpp -o k

C:\Users\Dhiraj\Desktop\Overloading>.\k

Enter Two Numberss:4

X=-4Y=-5

2 Write a Program to overload unary operator i.e. increment and decrement.

#include<iostream>

using namespace std;

class IncDec

int x,y;

public:

void read()

cout<<"Enter two Numbers";


cin>>x>>y;

void operator --()

x--;

y--;

void operator ++()

x++;

y++;

void display()

cout<<"X="<<x<<endl<<"Y="<<y<<endl;

};

int main()

IncDec n;

n.read();

--n;

cout<<"After Decremting the object one time\n";

n.display();

++n;

++n;

cout<<"After INcremting the object one time\n";


n.display();

return 0;

Output:

C:\Users\Dhiraj\Desktop\Overloading>.\k

Enter two Numbers4

After Decremting the object one time

X=3

Y=4

After INcremting the object one time

X=5

Y=6

3. write a program to overload binary operator “+” to add two complex numbers.

#include<iostream>

using namespace std;

class Complex

int x,y;

public:

void read()

cout<<"Enter The real and Imaginary part of a complex number";

cin>>x>>y;

Complex operator +(Complex c)


{

Complex c1;

c1.x=x+c.x;

c1.y=y+c.y;

return c1;

void display()

if(y<0)

cout<<x<<y<<"i";

else{

cout<<x<<"i"<<y;

};

int main()

Complex c1;

Complex c2;

Complex c3;

c1.read();

c2.read();

c3=c1+c2;

c3.display();
return 0;

Output:

C:\Users\Dhiraj\Desktop\Overloading>g++ Oerat.cpp -o k

C:\Users\Dhiraj\Desktop\Overloading>.\k

Enter The real and Imaginary part of a complex number4

Enter The real and Imaginary part of a complex number5

9i9

4. write a program to overload binary operator “+=” to add two complex numbers

#include<iostream>

using namespace std;

class Complex

int x,y;

public:

void read()

cout<<"Enter The real and Imaginary part of a complex number";

cin>>x>>y;

void operator +=(Complex c)


{

x=x+c.x;

y=y+c.y;

void display()

if(y<0)

cout<<x<<y<<"i";

else{

cout<<x<<"+i"<<y;

};

int main()

Complex c1;

Complex c2;

Complex c3;

c1.read();

c2.read();

c1+=c2;

c1.display();
return 0;

Output:

C:\Users\Dhiraj\Desktop\Overloading>g++ Z.cpp -o k

C:\Users\Dhiraj\Desktop\Overloading>.\k

Enter The real and Imaginary part of a complex number2

Enter The real and Imaginary part of a complex number4

6+i8

5. write a program to add two distance entered by the user in feet and inches using overloaded binary
operator “+”.

#include<iostream>

#include<conio.h>

using namespace std;

class Distance

int ft,in;

public:

void read()

cout<<"Enterthe distance in feet and inches";

cin>>ft>>in;

}
Distance operator +(Distance d)

Distance dr;

dr.ft=ft+d.ft;

dr.in=in+d.in;

if(dr.in>=12){

dr.ft++;

dr.in=dr.in-12;

return dr;

void display()

cout<<"Distance is"<<ft<<"feet and"<<in<<"inches";

};

int main()

Distance d1;

Distance d2;

Distance d3;

d1.read();

d2.read();

d3=d1+d2;

d3.display();

return 0;

}
Output:

C:\Users\Dhiraj\Desktop\Overloading>.\k

Enterthe distance in feet and inches4

Enterthe distance in feet and inches5

11

Distance is10feet and2inches

C:\Users\Dhiraj\Desktop\Overloading>

6.write a program to declare class Jkurnal having data mmbers as journal name,price and no-of-
pages.accept this data for two object and display the name of journal having greatest price

#include<iostream>

#include<conio.h>

using namespace std;

class Journal

private:

char name[20];

int pages,price;

public:

void accept()

cout<<"Enter name,pagesand price";

cin>>name>>pages>>price;
}

void display()

cout<<"Name:"<<name<<"\n Pages:"<<pages<<"\n Price"<<price;

Journal operator >(Journal x)

if(price>x.price) return *this;

return x;

};

int main()

Journal a,b,c;

a.accept();

b.accept();

c=a>b;

c.display();

return 0;

Output:

C:\Users\Dhiraj\Desktop\Overloading>g++ Journal.cpp -o l

C:\Users\Dhiraj\Desktop\Overloading>.\l

Enter name,pagesand pricea

4
400

Enter name,pagesand priceb

200

Name:a

Pages:4

Price400

C:\Users\Dhiraj\Desktop\Overloading>

7. Write a program to declare class Distance having data members feet and inch. Overloaded unary –
operator so that when it is used with object of this class it will decrement value of inches by 1.

#include<iostream>

#include<conio.h>

#include<stdio.h>

using namespace std;

class Distance1

private:

int ft,inch;

public:

void accept()

cout<<"Enyter distance in ft and inches";

cin>>ft>>inch;

}
void display()

cout<<ft<<"feet"<<inch<<"inches\n";

void operator -()

inch=inch-1;

if(inch==-1)

inch=11;

ft=ft-1;

};

int main()

Distance1 d;

d.accept();

d.display();

-d;

cout<<"After Decrementing\n";

d.display();

return 0;

Output:

C:\Users\Dhiraj\Desktop\Overloading>g++ Distance1.cpp -o l

C:\Users\Dhiraj\Desktop\Overloading>.\l

Enyter distance in ft and inches3


0

3feet0inches

After Decrementing

2feet11inches

8 Write a program to declare class Distance having data members feet and inch. Overloaded binary ++
operator so that when it is used with object of this class it will increment value of inches by 1

#include<iostream>

#include<conio.h>

#include<stdio.h>

using namespace std;

class Distance2

private:

int ft,inch;

public:

void accept()

cout<<"Enyter distance in ft and inches";

cin>>ft>>inch;

void display()

cout<<ft<<"feet"<<inch<<"inches\n";

}
void operator -()

inch=inch+1;

if(inch==12)

inch=0;

ft=ft+1;

};

int main()

Distance2 d;

d.accept();

d.display();

++d;

cout<<"After Decrementing\n";

d.display();

return 0;

Output:

C:\Users\Dhiraj\Desktop\Overloading>g++ Distance2.cpp -o l

C:\Users\Dhiraj\Desktop\Overloading>.\l

Enyter distance in ft and inches3

11

3feet11inches

After Decrementing
4feet0inches

9 write a program to add two complex number using operator overloaded by a friend function

#include<iostream>

#include<conio.h>

using namespace std;

class Complex

int x,y;

public :

void read()

cout<<"Enter the real and imaginary part of complex number";

cin>>x>>y;

friend Complex operator + (Complex c1,Complex c2);

void display()

if(y<0)

cout <<x<<y<<"i";

else

cout<<x<<"+i"<<y;

}
};

Complex operator + (Complex c1,Complex c2)

Complex c;

c.x=c1.x+c2.x;

c.y=c1.y+c2.y;

return c;

int main()

Complex c1;

Complex c2;

Complex c3;

c1.read();

c2.read();

c3=c1+c2;

c3.display();

return 0;

You might also like