Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*add 2 complex numbers */
- #include<iostream.h>
- #include<conio.h>
- class complex //class declaration
- {
- private : float real;
- float imag;
- public : void read();
- void display();
- complex sum(complex);
- };
- void complex :: read()
- {
- cout<<"Enter value for real part: ";
- cin>>real;
- cout<<"Enter value for imaginary part: ";
- cin>>imag;
- }
- complex complex :: sum(complex x)
- {
- real=real+x.real;
- imag=imag+x.imag;
- return x;
- }
- void complex :: display()
- {
- cout<<real<<"+"<<imag<<"i"<<endl;
- }
- int main()
- {
- complex r1,r2,r3;
- clrscr();
- r1.read();
- r2.read();
- cout<<"Before Addition:\n";
- r1.display();
- r2.display();
- r3=r1.sum(r2);
- cout<<"After Addition:\n";
- r1.display();
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment