Source Code :
#include<iostream.h>
class complex {
int img,rl;
public:
complex operator+(complex);
complex operator-(complex);
void get_value();
void show_value();
};
void complex::get_value() {
cout<<endl<<"Enter the value of img : ";
cin>>img;
cout<<endl<<"Enter the value of real : ";
cin>>rl;
}
void complex::show_value() {
cout<<endl<<"Value of img : "<<img<<endl<<"Value of real : "<<rl<<endl;
}
complex complex::operator+(complex ob) {
complex ob1;
ob1.rl=rl+ob.rl;
ob1.img=img+ob.img;
return ob1;
}
complex complex::operator-(complex ob) {
complex ob1;
ob1.rl=rl-ob.rl;
ob1.img=img-ob.img;
return ob1;
}
int main() {
complex ob1,b2,ob3,ob4;
int ch;
while (true) {
cout<<endl<<"MAIN MENU";
cout<<endl<<"1. Take Input and Display Output";
cout<<endl<<"2. Add Two Objects";
cout<<endl<<"3. Subtract Two Objects";
cout<<endl<<"4. Exit";
cout<<endl<<"Enter Choice : ";
cin>>ch;
switch(ch) {
case 1: {
ob1.get_value();
ob1.show_value();
}break;
case 2: {
ob1.get_value();
ob2.get_value();
ob3=ob1+ob2;
ob3.show_value();
}break;
case 3: {
ob1.get_value();
ob2.get_value();
ob4=ob1-ob2;
ob4.show_value();
}break;
case 4:
exit(0);
default: {
cout<<endl<<"Invalid choice";
}
}
}
}
Output :
MAIN MENU
1. Take Input and Display Output
2. Add Two Objects
3. Subtract Two Objects
4. Exit
Enter Choice : 1
Enter the value of img : -23
Enter the value of real : 45
Value of img : -23
Value of real : 45
MAIN MENU
1. Take Input and Display Output
2. Add Two Objects
3. Subtract Two Objects
4. Exit
Enter Choice : 2
Enter the value of img : -23
Enter the value of real : 45
Enter the value of img : -12
Enter the value of real : 67
Value of img : -35
Value of real : 112
MAIN MENU
1. Take Input and Display Output
2. Add Two Objects
3. Subtract Two Objects
4. Exit
Enter Choice : 3
Enter the value of img : -12
Enter the value of real : 43
Enter the value of img : -6
Enter the value of real : 35
Value of img : -6
Value of real : 8
No comments:
Post a Comment