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

C++Programs(30-11-2020)

C++++ programs sample pprs

Uploaded by

mohitnajkani786
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C++Programs(30-11-2020)

C++++ programs sample pprs

Uploaded by

mohitnajkani786
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C++ Programming

Topics Covered= Operator Overloading, Overloading of pre increment, post


increment, pre decrement, post decrement.

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);

return temp; //Operator Function a=-12


temp
}
};
void main()
{ a=12
Num p(12);
clrscr(); a=-12
Num re=-p;
re.show();
getch(); re object
}
/*output :
Value of a is=-12*/

Program 196

1|Page 30 November 2020


C++ Programming
//196.Example of Overloading of Decrement Operator(--)
#include<iostream.h>
#include<conio.h>
class Num
{
int a;
public:
void show()
{
cout<<endl<<"Value of a is="<<a;
}

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

2|Page 30 November 2020


C++ Programming
Program 197
//197.Example of Overloading of Post ,Pre Increment 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 Increment operator function
}
Return type Num operator ++() a=0 13
Of
Operator { t
function Num t;
t.a=++a;
return t;
}
Num operator ++(int) //Post Increment operator function
{
Num t;
t.a=a++;
a=0 12
return t;
} t
};

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=

3|Page 30 November 2020


C++ Programming
Value of a is=13
Value of a is=13
Output of Post Increment operator=
Value of a is=12
Value of a is=13 */

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=";

4|Page 30 November 2020


C++ Programming
c.show(); // c.a=12
d.show(); // d.a=11
getch();
}
/*Output :
Output of Pre Decrement operator=
Value of a is=11
Value of a is=11
Output of Post Decrement operator=
Value of a is=12
Value of a is=11 */

5|Page 30 November 2020

You might also like