C++Programs(7-12-2020)
C++Programs(7-12-2020)
Program 205
//205.Example of overloading of Relational operator(==,!=,>,<,>=,<=)
#include<iostream.h>
#include<conio.h>
enum bool{false,true}; //user defined data typeenum
class pixel
{
int a;
int b;
public:
void show()
{
cout<<endl<<"Value of a is="<<a;
cout<<endl<<"Value of b is="<<b;
}
pixel()
{
a=0;
b=0;
}
pixel(int x)
{
a=x;
b=x;
}
pixel(int x,int y)
{
a=x;
b=y;
}
bool operator >(pixel ob2) //ob2.a=10 , ob2.b=5
{
if(a>ob2.a && b>ob2.b) //12>10 && 12>5
return true;
else
return false;
}
bool operator ==(pixel ob3) //ob3.a=15 ob3.b=4
{ //a=12 ,b=12 (ob1)
if(a==ob3.a && b==ob3.b) //12==15 && 12==4
return true;
else
return false;
1|Page 7-12-2020
C++ Programming
}
bool operator !=(pixel ob31) //ob31.a=15 ob31.b=4
{ //a=10 b=5
if(a!=ob31.a && b!=ob31.b) //10!=15 && 5!=4
return true;
else
return false;
}
};
void main() a=12
b=12 a=10
{
b=5
pixel ob1(12),ob2(10,5);
bool res; ob2
clrscr();
res=ob1>ob2; //This statement internally implemented as ob1>(ob2)
cout<<endl<<"The bigger object is=";
if(res==true)
cout<<endl<<"Object 1 is bigger";
a=15
else
b=4
cout<<endl<<"Object 2 is bigger";
pixel ob3(15,4);
res=ob1==ob3; //res= ob1==(ob3)
if(res==true)
cout<<endl<<"object1 and object3 are equal";
else
cout<<endl<<"object1 and object3 are not equal";
res=ob2!=ob3; //ob2!=(ob3)
if(res==true)
cout<<endl<<"object2 and object3 are not equal";
else
cout<<endl<<"object2 and object3 are equal";
getch();
}
/*Output
The bigger object is=
object 1 is bigger
object1 and object3 are not equal
object2 and object3 are not equal
*/
Program 206
//206.Overloading of Binary (+) operator When 2nd operand is of basic data type.
#include<iostream.h>
#include<conio.h>
class Num
{
int a;
public:
2|Page 7-12-2020
C++ Programming
Num()
{
a=0;
}
Num(int x)
{
a=x; //a=4
}
Num operator +(Num b) //b.a=10 4
{
b.a=a+b.a //b.a=20+10 b.a=30(1st time) //b.a=20+4=24
return b;
}
void show() When we write d=p+4 then at the place of 4
{ one argument constructor will be called and
cout<<endl<<"Value of a is="<<a; anonymous object will be created .4 value
} will be assigned to data member a of that
object. Then data member of p object will be
}; added with data member of that anonymous
void main()
{
Num p(20),b(10),d; //p.a=20 ,b.a= 10 ,d.a=0
clrscr();
d=p+b; //p+(b)
cout<<endl<<"Addition is=";
d.show(); //d.a=30
cout<<endl<<"Addition when one operator is of basic data type=";
d=p+4; //(here second operand is of basic data type(int)) //d=p+(4)
d.show(); //d.a=24
//d=40+p; //error +(40,p)
getch();
}
/*Output =
Addition is=
Value of a =30
Addition when one operator is of basic data type
Value of a =24*/
Program 207
//207.Example of overloading of Arithmetic Operator(+) using friend Function when
first operand is of basic data type
#include<iostream.h>
#include<conio.h>
class Num
{
int a;
public:
void show()
3|Page 7-12-2020
C++ Programming
{
cout<<endl<<"Value of a is="<<a;
}
Num()
{
a=0;
}
Num(int x)
{
a=x;
}
friend Num operator +(Num ,Num);
};
Num operator +(Num ob1,Num ob2) //ob1.a=22 ob2.a=10
{
Num res;
res.a=ob1.a+ob2.a; //res.a=22+10
return res; //res.a=32
}
void main()
{
Num ob1(12),ob2(10),res;
res=22+ob2; //This statement internally implemented as +(22,ob2)
clrscr();
cout<<endl<<"Result of addition is=";
res.show();
getch();
}
/*Output
Result of addition is=
Value of a is=32
*/
4|Page 7-12-2020