All Operator overloading program in C++


Content



1. Program to explain Overloading of Unary Minus ( - ) Operator

#include <iostream.h>
#include <conio.h>

class Counter
{
unsigned int count;
public:
Counter()
{
count = 0;
}

Counter( int c )
{
count = c;
}

int getCount()
{
return count;
}

void operator -() // prefix
{
count = -count;
}
};

void main()
{
clrscr();
Counter c1(10), c2(20);
cout << "\n c1 : " << c1.getCount();
cout << "\n c2 : " << c2.getCount();

-c2;

cout << "\n c1 : " << c1.getCount();
cout << "\n -c2 : " << c2.getCount();
getch();
}

Output:

c1 : 10
c2 : 20
c1 : 10
-c2 : 20





2. PROGRAM TO USE UNARY OPERATOR OVERLOADING

#include<conio.h>
#include<iostream.h>
class increase
{
private:
   int data;
public:
   increase()
   {
data=0;
   }
   int display()
   {
cout<<data<<endl;
   }
   void operator++()
   {
data++;
   }
};

void main()
{
clrscr();
increase i1,i2;
i1.display();
i2.display();
i1++;
++i1;
i2++;
i1.display();
i2.display();
getch();;

Output:

0
0
2
1






3. Program to Overload Unary Increment ( ++ ) Operator

#include <iostream.h>
#include <conio.h>

class Counter
{
unsigned int count;
public:
Counter()
{
count = 0;
}

int getCount()
{
return count;
}

void operator ++() // prefix
{
count++;
}
};

void main()
{
clrscr();
Counter c1, c2;
cout << "\n c1 : " << c1.getCount();
cout << "\n c2 : " << c2.getCount();

++c1;
++c2;
c2++; // warning.

cout << "\n c1 : " << c1.getCount();
cout << "\n c2 : " << c2.getCount();
getch();
}

Output:

c1 : 0
c2 : 0
c1 : 1
c2 : 2





4. Program to Overload Prefix and Postfix Increment ( ++ ) Operator

#include <iostream.h>
#include <conio.h>

class Counter
{
unsigned int count;
public:
Counter()
{
count = 0;
}

int getCount()
{
return count;
}

void operator ++() // prefix
{
count++;
cout << "\n Prefix Increment.";
}

void operator ++( int ) // postfix
{
count++;
cout << "\n Postfix Increment.";
}
};

void main()
{
clrscr();
Counter c1, c2;
cout << "\n c1 : " << c1.getCount();
cout << "\n c2 : " << c2.getCount();

++c1;
++c2;
c2++;

cout << "\n c1 : " << c1.getCount();
cout << "\n c2 : " << c2.getCount();
getch();
}

Output:

c1 : 0
c2 : 0
Prefix Increment.
Prefix Increment.
Postfix Increment.
c1 : 1
c2 : 2





5. Program to Return Object from Increment Operator(++) Overloading function

#include <iostream.h>
#include <conio.h>

class Counter
{
unsigned int count;
public:
Counter()
{
count = 0;
}

int getCount()
{
return count;
}

Counter operator ++()
{
count++;
Counter c;
c.count = count;
return c;
}
};

void main()
{
clrscr();
Counter c1, c2;
cout << "\n c1 : " << c1.getCount();
cout << "\n c2 : " << c2.getCount();

++c1;
c2 = ++c1;

cout << "\n c1 : " << c1.getCount();
cout << "\n c2 : " << c2.getCount();
getch();
}

Output:

c1 : 0
c2 : 0
c1 : 2
c2 : 2





6. Program to Return Nameless Object from increment Operator(++) Overloading function

#include <iostream.h>
#include <conio.h>

class Counter
{
unsigned int count;
public:
Counter()
{
count = 0;
}

Counter( int cnt )
{
count = cnt;
}

int getCount()
{
return count;
}

Counter operator ++()
{
count++;
return Counter(count);
}
};

void main()
{
clrscr();
Counter c1, c2;
cout << "\n c1 : " << c1.getCount();
cout << "\n c2 : " << c2.getCount();

++c1;
c2 = ++c1;

cout << "\n c1 : " << c1.getCount();
cout << "\n c2 : " << c2.getCount();
getch();
}

Output:

c1 : 0
c2 : 0
c1 : 2
c2 : 2





7. PROGRAM FOR OVERLOADING BINARY + OPERATOR.

#include<conio.h>
#include<iostream.h>
class data
{
private:
   int d;
public:
   data()
   {
d=0;
   }
   data(int dt)
   {
d=dt;
   }
   void show()
   {
cout<<d<<endl;
   }
   data operator+(data obj)
   {
data temp;
temp.d=obj.d+d;
return temp;
   }
};
void main()
{
clrscr();
data d1(2),d2(10),d3;
d3=d1+d2;
d3.show();
getch();
}

Output:

12





8. + Operator Overloading

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class complex
{
  float x,y;
  public:
       complex() {}
       complex(float real,float image) {x=real,y=image;}
       complex operator +(complex);
       void display(void);
};

complex complex :: operator +(complex c)
{
   complex temp;
   temp.x=x + c.x;
   temp.y=y + c.y;
  return temp;
}

void complex :: display(void)
{
   cout<<x<<" + "<<y<<"\n";
}

int main()
{
  clrscr();
  complex c1,c2,c3;
  c1=complex(2.5,3.5);
  c2=complex(1.6,2.7);
  c3=c1+c2;
  cout<<"C1 : ";
  c1.display();
  cout<<"C2 : ";
  c2.display();
  cout<<"**************\n";
  cout<<"C3 : ";
  c3.display();
getch();
return 0;
}

Output:

C1 : 2.5 + 3.5
C2 : 1.6 + 2.7
==============
C3 : 4.1 + 6.2




9. Program to Overload Binary Relational(<) Operator with enum Boolean for true/false

#include <iostream.h>
#include <conio.h>

enum boolean { false, true };

class Distance
{
private:
int feet;
float inches;
public:
Distance()
{
feet = 0;
inches = 0.0;
}

Distance(int ft, float in)
{
feet = ft;
inches = in;
}

void getDist()
{
cout << "Enter Feet & Inches : ";
cin >> feet >> inches;
}

void showDist()
{
cout << feet << "\'-" << inches << "\"";
}

//  int operator < (Distance);
boolean operator < (Distance);
};

//  int Distance :: operator < ( Distance dd2 )
boolean Distance :: operator < ( Distance dd2 )
{
float f1 = feet + (inches/12.0);
float f2 = dd2.feet + (dd2.inches/12.0);
//  return ( (f1<f2) ? 1 : 0 );
return ( (f1<f2) ? true : false );
}

void main()
{
clrscr();
Distance d1, d3, d4;
d1.getDist();
Distance d2(10, 6.0);

cout << "\n d1 : ";
d1.showDist();
cout << "\n d2 : ";
d2.showDist();

if( d1 < d2 )
cout << "\n Distance d1 < d2.";
else
cout << "\n Distance d1 >= d2.";

getch();
}

Output:

Enter Feet & Inches : 6  6.75

d1 : 6'-6.75"
d2 : 10'-6"
Distance d1 < d2.



10. += operator overloading

#include<conio.h>
#include<iostream.h>
class height
{
private:
   int feet;
   float inch;
public:
   height()
   {
feet=inch=0;
   }
   height(int f,float i)
   {
feet=f;
inch=i;
   }
   void show()
   {
cout<<"Feet="<<feet<<"\tInches="<<inch<<endl;
   }
   void operator +=(height h)
   {
feet+=h.feet;
inch+=h.inch;
if(inch>=12)
{
inch=12;
feet++;
}
   }
};

void main()
{
clrscr();
height h1(10,15),h2(7,18);
h1+=h2;
h1.show();
getch();
}

Output:

Feet=18 Inches=12






11. Program to Overload Assignment(=) Operator
 but Default Copy constructor created by Compiler

#include <iostream.h>
#include <conio.h>

class A
{
private:
int x;
public:
A()
{ }

A( int a )
{
x = a;
}

void showX()
{
cout << "\n x : " << x;
}

A operator = (A &a)
{
x = a.x;
cout << "\n Assignment Operator called.";
return A(x);
}
};

void main ()
{
clrscr();

A a1(10);
A a2;
a1.showX();

a2 = a1;
a2.showX();

A a3 = a1;
a3.showX();

A a4(a1);
a4.showX();

getch();
}

Output:

x : 10
Assignment Operator called.
x : 10
x : 10
x : 10





12. BINARY > and == OPERATOR

#include<conio.h>
#include<iostream.h>
class data
{
private:
   int d;
public:
   data()
   { d=0;
   }
   data(int dt)
   {
dt=0;
   }
   int operator>(data obj)
   {
if(d>obj.d)
   return 1;
else
   return 0;
   }
   int operator==(data obj)
   {
if(d==obj.d)
   return 1;
else
   return 0;
   }
};
void main()
{
clrscr();
data t1(43),t2(20);
if(t1==t2)
cout<<"Numbers Are Equal";
else if(t1>t2)
cout<<"T1 Greater.";
else
cout<<"T2 Greater.";
getch();
}

Output:

T1 Greater.




13. Program to use both Copy Constructor and Overloaded Assignment Operator ( = )

#include <iostream.h>
#include <conio.h>

class A
{
private:
int x;
public:
A()
{ }

A( int a )
{
x = a;
}

A( A &a )
{
x = a.x;
cout << "\n Copy constructor called.";
}

void showX()
{
cout << "\n x : " << x;
}

A operator = (A &a)
{
x = a.x;
cout << "\n Assignment Operator called.";
return A(x);
}
};

void main ()
{
clrscr();

A a1(10);
A a2;
a1.showX();

a2 = a1;
a2.showX();

A a3 = a1;
a3.showX();

A a4(a1);
a4.showX();

getch();
}

Output:

x : 10
Assignment Operator called.
x : 10
Copy constructor called.
x : 10
Copy constructor called.
x : 10




14. Program to use both Copy Constructor and Overloaded
 Assignment Operator ( = ) with returning Object

#include <iostream.h>
#include <conio.h>

class A
{
private:
int x;
public:
A()
{ }

A( int a )
{
x = a;
}

A( A &a )
{
x = a.x;
cout << "\n Copy constructor called.";
}

void showX()
{
cout << "\n x : " << x;
}

A& operator = (A &a)
{
x = a.x;
cout << "\n Assignment Operator called.";
return *this;
}
};

void main ()
{
clrscr();

A a1(10);
A a2, a3;
a1.showX();

a3 = a2 = a1;
a2.showX();
a3.showX();

A a4 = a1;
a4.showX();

A a5(a1);
a5.showX();

getch();
}

Output:

x : 10
Assignment Operator called.
Assignment Operator called.
x : 10
x : 10
Copy constructor called.
x : 10
Copy constructor called.
x : 10





15. Program to add two Distance with Plus Operator
 Overload without friend function

#include <iostream.h>
#include <conio.h>

class Distance
{
private:
int feet;
float inches;
public:
Distance()
{
feet = 0;
inches = 0.0;
}

Distance(int ft, float in)
{
feet = ft;
inches = in;
}

Distance(float f)
{
feet = int(f);
inches = 12 * (f - feet);
}

void getDist()
{
cout << "Enter Feet & Inches : ";
cin >> feet >> inches;
}

void showDist()
{
cout << feet << "\'-" << inches << "\"";
}

Distance operator + (Distance);
};

Distance Distance :: operator + ( Distance dd2 )
{
Distance dd3;
dd3.feet = feet + dd2.feet;
dd3.inches = inches + dd2.inches;
if( dd3.inches >= 12.0 )
{
dd3.inches -= 12.0;
dd3.feet++;
}
return dd3;
}

void main()
{
clrscr();
Distance d1(12, 9.0);
Distance d2(10, 6.0);
Distance d3, d4, d5;

d3 = d1 + d2;
cout << "\n d3 : ";
d3.showDist();

d4 = d1 + 10.0;
cout << "\n d4 : ";
d4.showDist();

// d5 = 10.0 + d1; // Error
// cout << "\n d5 : ";
// d5.showDist();

getch();
}

Output:

d3 : 23'-3"
d4 : 22'-9"




16. Program to add two Distance with Plus Operator
Operload with friend function

#include <iostream.h>
#include <conio.h>

class Distance
{
private:
int feet;
float inches;
public:
Distance()
{
feet = 0;
inches = 0.0;
}

Distance(int ft, float in)
{
feet = ft;
inches = in;
}

Distance(float f)
{
feet = int(f);
inches = 12 * (f - feet);
}

void getDist()
{
cout << "Enter Feet & Inches : ";
cin >> feet >> inches;
}

void showDist()
{
cout << feet << "\'-" << inches << "\"";
}

friend Distance operator + (Distance, Distance);
};

Distance operator + ( Distance dd1, Distance dd2 )
{
Distance dd3;
dd3.feet = dd1.feet + dd2.feet;
dd3.inches = dd1.inches + dd2.inches;
if( dd3.inches >= 12.0 )
{
dd3.inches -= 12.0;
dd3.feet++;
}
return dd3;
}

void main()
{
clrscr();
Distance d1(12, 9.0);
Distance d2(10, 6.0);
Distance d3, d4, d5;

d3 = d1 + d2;
cout << "\n d3 : ";
d3.showDist();

d4 = d1 + 10.0;
cout << "\n d4 : ";
d4.showDist();

d5 = 10.0 + d1;
cout << "\n d5 : ";
d5.showDist();

getch();
}

Output:

d3 : 23'-3"
d4 : 22'-9"
d5 : 22'-9"





17. Program to Overload Insertion Operator(<<) and Extraction Operator ( >> )

#include <iostream.h>
#include <conio.h>

class Distance
{
private:
int feet;
float inches;
public:
Distance()
{
feet = 0;
inches = 0.0;
}

friend istream& operator >> (istream&, Distance&);
friend ostream& operator << (ostream&, Distance&);
};

istream& operator >> (istream &in, Distance &d)
{
cout << "Enter Feet & Inches : ";
in >> d.feet >> d.inches;
}

ostream& operator << (ostream &out, Distance &d)
{
out << d.feet << "\'-" << d.inches << "\"";
}

void main()
{
clrscr();
Distance d1;
Distance d2;

cin >> d1 >> d2;

cout << "\n d1 : " << d1;
cout << "\n d2 : " << d2;

getch();
}

Output:

Enter Feet & Inches : 12 6.5
Enter Feet & Inches : 15 9.5

d1 : 12'-6.5"
d2 : 15'-9.5"




18. Program to explain Type Conversion ( Basic to Object ) and ( Object to Basic )

#include <iostream.h>
#include <conio.h>

const float CF = 3.280833;

class Distance
{
private:
int feet;
float inches;
public:
Distance()
{
feet = 0;
inches = 0.0;
}

// for Basic to Object
Distance( float m )
{
float ft = m * CF;
feet = (int) ft;
inches = 12 * (ft - feet);
}

Distance(int ft, float in)
{
feet = ft;
inches = in;
}

void getDist()
{
cout << "Enter Feet & Inches : ";
cin >> feet >> inches;
}

void showDist()
{
cout << feet << "\'-" << inches << "\"";
}

//  for Object to Basic
operator float()
{
float ft = feet + (inches/12.0);
return (ft/CF);
}
};

void main()
{
clrscr();
Distance d1 = 1.0;
cout << "\n d1 : ";
d1.showDist();

d1 = 2.0;
cout << "\n d1 : ";
d1.showDist();

Distance d2(10, 6.0);
float m = float(d2);
cout << "\n Meters : " << m;

m = d1;
cout << "\n Meters : " << m;

getch();
}

Output:

d1 : 3'-3.369996"
d1 : 3'-6.739992"
Meters : 3.200407
Meters : 2




19. Program to explain Type Conversion (Basic to Object) and ( Object to Basic )

#include <iostream.h>
#include <conio.h>
#include <string.h>

const int SZ = 80;

class String
{
char str[SZ];
public:
String()
{
strcpy(str, "\0");
}

//  for Basic to Object
String(char s[ ])
{
strcpy(str, s);
}

void display()
{
cout << str;
}

//  for Object to Basic
operator char*()
{
return str;
}
};

void main()
{
clrscr();

String s1;
char st[ ] = "C++";
s1 = st;
String s2 = "Programming";
String s3("Examples");

cout << "\n s1 : " << (char*)s1;
cout << "\n s2 : " << (char*)s2;
cout << "\n s3 : " << (char*)s3;

getch();
}

Output:
 
s1 : C++
s2 : Programming
s3 : Examples


Reactions

Post a Comment

0 Comments