C++Programs(30-11-2020)
C++Programs(30-11-2020)
Program 195
//195.Example of Overloading of Unary Negation (-) Operator
#include<iostream.h>
#include<conio.h>
class Num
{
int a;
public:
void show()
{
cout<<endl<<" Value of a is="<<a;
}
int getA()
{
return a;
}
Num()
{
a=0;
}
Num(int x)
{
Return type
Of a=x; //Name of Operator to Overload
Operator }
function
Num operator -()
{
Num temp(-a); // Num temp(-12);
Program 196
Num(int x)
{
a=x;
}
void operator ++()
{
a++; //operator keyword
}
void operator --()
{
a--;
}
};
void main()
{
Num p(12);
clrscr();
cout<<"Decremented Value is=";
--p;
p.show();
cout<<" Incremented Value is=";
p++;
p.show();
getch();
}
/*Output :
Decremented Value is=
Value of a is=11
Incremented Value is=
Value of a is=12 */
int a=10,c;
c=a++;
cout<<endl<<c; //10
cout<<endl<<a; //11
c=++a;
cout<<endl<<c; //12
cout<<endl<<a; //12
void main() a= 0 13
a=13
{ c object
Num p(12),c; p object
clrscr(); Internal representation of the
c=++p; statement c=++p is c=p.++()
cout<<endl<<"Output of Pre Increment operator=";
c.show();
p.show(); a=12 13
Num d(12);
c=d++; d object
cout<<endl<<"Output of Post Increment operator=";
c.show(); //12
d.show(); //13
getch();
}
/*output :
Output of Pre Increment operator=
Program 198
//198.Example of Overloading of Post ,Pre Decrement (--)Operator
#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; //Pre Decrement operator function
}
Num operator--() //a=12 11
{
Num t;
t.a= --a; //t.a=11;
return t;
}
Num operator --(int) //Post Decrement operator function
{ //a=12
Num t; //t.a=0
t.a=a--; //t.a=12 , a=11
return t;
}
};
void main()
{
Num p(12),c;
clrscr();
c=--p;
cout<<endl<<"Output of Pre Decrement operator=";
c.show(); //c.a=11
p.show(); //p.a=11 a=12 11
Num d(12);
c=d--; d object
cout<<endl<<"Output of Post Decrement operator=";