upsidedown

oops operator overloading

Mar 9th, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. // constructor.cpp : Defines the entry point for the console application.
  2. #include "stdafx.h"
  3. #include<iostream>
  4.  
  5.  
  6. //write a program to overload unary - operator
  7. //write a program to overload binary operators ie + - * / for any object that stores cmplx nos
  8. //write a program to overload increment and decrement operator
  9.  
  10.  
  11. using namespace std;
  12.  
  13.  
  14. class cmplx
  15. {
  16.     int real;
  17.     int imag;
  18. public:
  19.    
  20.     void enter()
  21.     {
  22.         cout<<"enter real part";
  23.         cin>>real;
  24.         cout<<"enter imaginary part";
  25.         cin>>imag;
  26.     }
  27.  
  28.  
  29.     void display()
  30.     {
  31.         cout<<"\nthe number is"<<real<<"+"<<imag<<"i";
  32.     }
  33.  
  34.     cmplx operator+(cmplx &a)
  35.     {
  36.         cmplx t;
  37.         t.real=real+a.real;
  38.         t.imag=imag+a.imag;
  39.         return t;
  40.     }
  41.  
  42.     cmplx operator-(cmplx &a)
  43.     {
  44.         cmplx t;
  45.         t.real=real-a.real;
  46.         t.imag=imag-a.imag;
  47.         return t;
  48.     }
  49.  
  50.     cmplx operator*(cmplx &a)
  51.     {
  52.         cmplx t;
  53.         t.real=real*a.real-imag*a.imag;
  54.         t.imag=0;
  55.         return t;
  56.     }
  57.  
  58.     cmplx operator/(cmplx &a)
  59.     {
  60.         cmplx t;
  61.         t.real=real/a.real;
  62.         t.imag=imag/a.imag;
  63.         return t;
  64.     }
  65.  
  66.     void operator++()
  67.     {
  68.         real++;
  69.         imag++;
  70.        
  71.     }
  72.  
  73.     void operator--()
  74.     {
  75.         real--;
  76.         imag--;
  77.        
  78.     }
  79.  
  80.     void operator-()
  81.     {
  82.        
  83.         real=-real;
  84.         imag=-imag;
  85.        
  86.     }
  87.  
  88.    
  89.  
  90.  
  91.  
  92.  
  93.  
  94. };
  95.  
  96.  
  97.  
  98.  
  99. int _tmain(int argc, _TCHAR* argv[])
  100. {
  101.     cmplx a;
  102.     a.enter();
  103.     cmplx b;
  104.     b.enter();
  105.  
  106.     cmplx c;
  107.     cout<<"\noverloaded + :";
  108.     c=a+b;
  109.     c.display();
  110.  
  111.     cout<<"\noverloaded - :";
  112.     c=a-b;
  113.     c.display();
  114.  
  115.     cout<<"\noverloaded * :";
  116.     c=a*b;
  117.     c.display();
  118.  
  119.     cout<<"\noverloaded / :";
  120.     c=a/b;
  121.     c.display();
  122.  
  123.     cout<<"\noverloaded ++ (object a):";
  124.     a++;
  125.     a.display();
  126.    
  127.     cout<<"\noverloaded --(object b) :";
  128.     b--;
  129.     b.display();
  130.  
  131.     cout<<"\noverloaded unary -(object a) :";
  132.     -a;
  133.     a.display();
  134.    
  135.     system("pause");
  136.     return 0;
  137. }
  138.  
  139.  
  140.  
  141. /*
  142. enter real part4
  143. enter imaginary part5
  144. enter real part2
  145. enter imaginary part3
  146.  
  147. overloaded + :
  148. the number is6+8i
  149. overloaded - :
  150. the number is2+2i
  151. overloaded * :
  152. the number is-7+0i
  153. overloaded / :
  154. the number is2+1i
  155. overloaded ++ (object a):
  156. the number is5+6i
  157. overloaded --(object b) :
  158. the number is1+2i
  159. overloaded unary -(object a) :
  160. the number is-5+-6iPress any key to continue . . .
  161. */
  162. // constructor.cpp : Defines the entry point for the console application.
  163. #include "stdafx.h"
  164. #include<iostream>
  165. #include<string>
  166.  
  167.  
  168.  
  169. //write a program to compare 2 strings using overloaded = operator
  170. using namespace std;
  171.  
  172.  
  173. class stringg
  174. {
  175.     char str[200];
  176. public:
  177.    
  178.     void enter()
  179.     {
  180.         cout<<"enter string";
  181.         cin>>str;
  182.     }
  183.  
  184.  
  185.     void display()
  186.     {
  187.         cout<<"\nThe string is \" "<<str<<"\"";
  188.     }
  189.  
  190.     void operator=(stringg &a)
  191.     {
  192.         int b=strcmp(str,a.str);
  193.         if(b)
  194.         {
  195.             cout<<"\nStrings not equal";
  196.         }
  197.         else
  198.         {
  199.             cout<<"\nStrings are equal";
  200.         }
  201.  
  202.     }
  203.  
  204. };
  205.  
  206.    
  207.  
  208.  
  209.  
  210. int _tmain(int argc, _TCHAR* argv[])
  211. {
  212.     stringg a,b;
  213.     a.enter();
  214.     b.enter();
  215.  
  216.     cout<<"\n\n";
  217.     a.display();
  218.     b.display();
  219.     a=b;
  220.  
  221.     system("pause");
  222.     return 0;
  223. }
  224.  
  225.  
  226.  
  227.  
  228. /*
  229. OUTPUT 1:
  230. enter stringPranav
  231. enter stringPranav
  232.  
  233. The string is " Pranav"
  234. The string is " Pranav"
  235. Strings are equal
  236. Press any key to continue . . .
  237.  
  238. OUTPUT 2:
  239. enter stringPRANAV
  240. enter stringpranav
  241.  
  242. The string is " PRANAV"
  243. The string is " pranav"
  244. Strings not equal
  245. Press any key to continue . . .
  246.  
  247. */
Advertisement
Add Comment
Please, Sign In to add comment