C++Programs(5-12-2020)
C++Programs(5-12-2020)
==================================================================
Program 201
//201.Overloading of Compound Assignment operators with returning object
#include<iostream.h>
#include<conio.h>
class Num
{
int a;
public:
void show()
{
cout<<endl<<"Value of a is="<<a;
}
Num()
{
a=0;
}
Num(int x)
{
a=x;
}
Num operator +=(Num b) //b.a=10
{
a+=b.a; //a=a+b.a a=20+10
return *this;
}
Num operator *=(Num b) //b.a=10
{
a*=b.a; a=40*10
return *this;
Here * this will return current
}
object,who called the
};
*=(operator function)
void main()
{
Num ob1(20),ob2(10),ob3; //ob3.a=0
clrscr();
cout<<endl<<"Example of compound assignment=";
cout<<endl<<"Addition is=";
ob3=ob1+=ob2; //[ob3=ob1+=(ob2)]
ob3.show();
cout<<endl<<"Multiplication is=";
Num c(40);
ob3=c*=ob2; //[ob3=c*=(ob2)]---- ob3=c=c*ob2
1|Page 5-12-2020
C++ Programming
ob3.show();
getch();
}
/*Output =
Example of compound assignment=
Addition is=
Value of a =30
Multiplication is=
Value of a =400
*/
Program 202
//202.Example of overloading of increment operator using friend Function
#include<iostream.h>
#include<conio.h>
class Num
{
int a;
public:
void show()
{
cout<<endl<<"Value of a is="<<a;
}
Num()
{
a=0;
}
Num(int x)
{
a=x;
}
friend Num operator ++(Num &);
};
Num operator ++(Num &p1) //&p1=p
{ a=12 13
p1.a++; p1/p
return p1; a=0 13
}
void main()
{ b
Num p(12),b; //b=p.++() [without friend function]
b=p++; //b=++(p) [ with friend function]
clrscr();
cout<<endl<<"Details of b object is=";
b.show();
cout<<endl<<"Details of p object is=";
p.show();
getch();
}
/*Output
Details of b object is=
Value of a is=13
2|Page 5-12-2020
C++ Programming
Details of p object is=
Value of a is=13 */
Program 203
//203.Example of overloading of Arithmetic Operator using friend Function
#include<iostream.h>
#include<conio.h>
class Num
{
int a;
public:
void show()
{
cout<<endl<<"Value of a is="<<a;
}
Num()
{
a=0;
}
Num(int x)
{
a=x;
}
friend Num operator +(Num ,Num);
friend Num operator -(Num ,Num);
};
Num operator +(Num ob1,Num ob2) //ob1.a=12 ob2.a=10
{
Num res;
res.a=ob1.a+ob2.a; //res.a=12+10
return res;
}
Num operator -(Num ob1,Num ob2) //ob1.a=12 ob2.a=10
{
Num res;
res.a=ob1.a-ob2.a; //res.a=12-10 =2
return res;
}
void main()
{
Num ob1(12),ob2(10),res; //res=ob1.+(ob2) without Friend Function
res=ob1+ob2; //This statement internally implemented as +(ob1,ob2)
clrscr();
cout<<endl<<"Result of addition is=";
res.show();
res=ob1-ob2; //res=-(ob1,ob2)[With Friend Function]
cout<<endl<<"Result of Subtraction is=";
res.show(); //2
getch();
}
3|Page 5-12-2020
C++ Programming
/*Output
Result of addition is=
Value of a is=22
Result of Subtraction is=
Value of a is=2*/
Program 204
//204.Example of overloading of Unary Negation(-) operator using friend Function
#include<iostream.h>
#include<conio.h>
class Num
{
int a;
public:
void show()
{
cout<<endl<<"Value a is="<<a;
}
Num()
{
a=0;
}
Num(int x)
{
a=x;
}
friend Num operator -(Num);
};
Num operator -(Num ob1) //ob1.a=12
{
Num res(-ob1.a); //Num res(-12) //res.a=-12
return res;
}
void main()
{
Num ob1(12),res; //ob1.a=12 res.a=0
res=-ob1; //This statement internally implemented as -(ob1) [With Friend Function]
clrscr();
cout<<endl<<"Result of Negation operator is =";
res.show(); //res.a=-12
getch();
}
/*Output
Result of Negation operator is=
Value of a is=-12*/
4|Page 5-12-2020