Showing posts with label Operator Overloading. Show all posts
Showing posts with label Operator Overloading. Show all posts

Binary Operator Overloading

#include<iostream.h>
#include<conio.h>
class num
 {
     private :
     int a,b,c,d;
public:
void input(void);
void show(void);
num operator+(num);
 };
void num ::input()
 {
cout<<"\n Enter value for a,b,c and d:\n";
cin>>a>>b>>c>>d;
 }
void num :: show()
 {
cout<<"A="<<a <<"B="<<b <<"C="<<c <<"D="<<d<<"\n";  
 }
num num::operator +(num t)
 {
num tmp;
tmp.a=a+t.a;
tmp.b=a+t.b;
tmp.c=a+t.c;
tmp.d=a+t.d;
return(tmp);
 }

void main()
 {
clrscr();
num X,Y,Z;
cout<<"\n Object X";
X.input();
cout<<"\n Object Y";
Y.input();
Z=X+Y;
cout<<"\nX: ";
X.show();
cout<<"\nY: ";
Y.show();
cout<<"\nZ: ";
Z.show();
getch();
 }

Unary Operator Overloading


#include<iostream.h>
#include<constream.h>
class num
 {
     private:
   int a,b,c,d;
     public:
   num(int j, int k, int m, int l)
     {
a=j;
b=k;
c=m;
d=l;
}
   void show(void);
   void operator ++();
   void operator --();
 };
void num::show()
 {
cout<<"A= "<<a<<"B= "<<b<<"C= "<<c<<"D= "<<d;
 }
void num::operator++()
 {
++a;++b;++c;++d;
 }
void num::operator--()
 {
--a;--b;--c;--d;
 }
void main()
 {
clrscr();
num X(3,2,5,7);
cout<<"\n Before increment of X :";
X.show(); 
++X;
cout<<"\n After increment of X :";
X.show();
cout<<"\n Before decrement of X :";
X.show();
--X;
cout<<"\n  After decrement of X :";
X.show(); 
getch();
 } 
Related Posts with Thumbnails