Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // constructor.cpp : Defines the entry point for the console application.
- #include "stdafx.h"
- #include<iostream>
- //write a program to overload unary - operator
- //write a program to overload binary operators ie + - * / for any object that stores cmplx nos
- //write a program to overload increment and decrement operator
- using namespace std;
- class cmplx
- {
- int real;
- int imag;
- public:
- void enter()
- {
- cout<<"enter real part";
- cin>>real;
- cout<<"enter imaginary part";
- cin>>imag;
- }
- void display()
- {
- cout<<"\nthe number is"<<real<<"+"<<imag<<"i";
- }
- cmplx operator+(cmplx &a)
- {
- cmplx t;
- t.real=real+a.real;
- t.imag=imag+a.imag;
- return t;
- }
- cmplx operator-(cmplx &a)
- {
- cmplx t;
- t.real=real-a.real;
- t.imag=imag-a.imag;
- return t;
- }
- cmplx operator*(cmplx &a)
- {
- cmplx t;
- t.real=real*a.real-imag*a.imag;
- t.imag=0;
- return t;
- }
- cmplx operator/(cmplx &a)
- {
- cmplx t;
- t.real=real/a.real;
- t.imag=imag/a.imag;
- return t;
- }
- void operator++()
- {
- real++;
- imag++;
- }
- void operator--()
- {
- real--;
- imag--;
- }
- void operator-()
- {
- real=-real;
- imag=-imag;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- cmplx a;
- a.enter();
- cmplx b;
- b.enter();
- cmplx c;
- cout<<"\noverloaded + :";
- c=a+b;
- c.display();
- cout<<"\noverloaded - :";
- c=a-b;
- c.display();
- cout<<"\noverloaded * :";
- c=a*b;
- c.display();
- cout<<"\noverloaded / :";
- c=a/b;
- c.display();
- cout<<"\noverloaded ++ (object a):";
- a++;
- a.display();
- cout<<"\noverloaded --(object b) :";
- b--;
- b.display();
- cout<<"\noverloaded unary -(object a) :";
- -a;
- a.display();
- system("pause");
- return 0;
- }
- /*
- enter real part4
- enter imaginary part5
- enter real part2
- enter imaginary part3
- overloaded + :
- the number is6+8i
- overloaded - :
- the number is2+2i
- overloaded * :
- the number is-7+0i
- overloaded / :
- the number is2+1i
- overloaded ++ (object a):
- the number is5+6i
- overloaded --(object b) :
- the number is1+2i
- overloaded unary -(object a) :
- the number is-5+-6iPress any key to continue . . .
- */
- // constructor.cpp : Defines the entry point for the console application.
- #include "stdafx.h"
- #include<iostream>
- #include<string>
- //write a program to compare 2 strings using overloaded = operator
- using namespace std;
- class stringg
- {
- char str[200];
- public:
- void enter()
- {
- cout<<"enter string";
- cin>>str;
- }
- void display()
- {
- cout<<"\nThe string is \" "<<str<<"\"";
- }
- void operator=(stringg &a)
- {
- int b=strcmp(str,a.str);
- if(b)
- {
- cout<<"\nStrings not equal";
- }
- else
- {
- cout<<"\nStrings are equal";
- }
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- stringg a,b;
- a.enter();
- b.enter();
- cout<<"\n\n";
- a.display();
- b.display();
- a=b;
- system("pause");
- return 0;
- }
- /*
- OUTPUT 1:
- enter stringPranav
- enter stringPranav
- The string is " Pranav"
- The string is " Pranav"
- Strings are equal
- Press any key to continue . . .
- OUTPUT 2:
- enter stringPRANAV
- enter stringpranav
- The string is " PRANAV"
- The string is " pranav"
- Strings not equal
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment