Pages

Sunday, 26 April 2015

U6P2

Program to create a class THREE_D contains data members like X, Y, Z. Include constructor to initialize data. and overload Unary +,-,++,-- and Binary * Operator.

Code : 
 #include<iostream.h>  
 #include<conio.h>  
 class THREE_D  
 {  
      public:  
      int x;  
      int y,z;  
      THREE_D()  
      {  
           x=0;  
           y=0;  
           z=0;  
      }  
      THREE_D(int c)  
      {  
           z=c;  
      }  
      THREE_D(int a,int b)  
      {  
           x=a;  
           y=b;  
      }  
      void showadd()  
      {  
           cout<<"\nAddition (X + Y + z) : "<<x<<endl;  
      }  
      void showsub()  
      {  
           cout<<"\nSubstraction (Y - x) : "<<y<<endl;  
      }  
      void showmul()  
      {  
           cout<<"\nMultiplication (X * Y) : "<<z<<endl;  
      }  
      THREE_D operator +(THREE_D p)  
      {  
           THREE_D temp;  
           temp.x=p.x+x;  
           return temp;  
      }  
      THREE_D operator -(THREE_D p1)  
      {  
           THREE_D temp1;  
           temp1.y=p1.y-y;  
           return temp1;  
      }  
      THREE_D operator *(THREE_D p2)  
      {  
           THREE_D temp2;  
           temp2.z=p2.z*z;  
           return temp2;  
      }  
      THREE_D operator ++()  
      {  
           z++;  
      }  
      void displayin()  
      {  
           cout<<"\nIncrement Z(++) : " <<z<<endl;  
      }  
      THREE_D operator --()  
      {  
           z--;  
      }  
      void displayde()  
      {  
           cout<<"\nDecrement Y(--) : "<<z;  
      }  
 };  
 void main()  
 {  
      clrscr();  
      int x,y,z;  
      cout<<"Program will overload unary +, -, ++, -- and * operators."<<endl<<endl;  
      cout<<"Enter X : ";  
      cin>>x;  
      cout<<"Enter Y : ";  
      cin>>y;  
      cout<<"Enter Z : ";  
      cin>>z;  
      THREE_D z1(x,x),z2(y,y),z3(z,z),z4;  
      //ADDTION  
      z4=z1+z2+z3;  
      z4.showadd();  
      //SUBSTRACTION  
      z3=z1-z2;  
      z3.showsub();  
      //MULTIPLICATION  
      THREE_D z5(x),z6(y),z7;  
      z7=z5*z6;  
      z7.showmul();  
      //INCREMENT  
      THREE_D z8(z);  
      z8++;  
      z8.displayin();  
      //DECREMENT  
      z6--;  
      z6.displayde();  
      getch();  
 }  

Output : 

No comments:

Post a Comment