Complex Number Operations, Add, Sub,Mul and Div
This program can be used to perform operations such as addition, substraction, multiplication and division between two complex numbers. In this program, all the three types of constuctors are used, i.e. default, parameterised and copy constructor. Besides that a get() function is also used which will fetch input from the console. Overal this program will show you how to use constructors or functions to assign values to the variables of the object of the class.
This Program is crafted using C++.
Description
In the main function the ojects a,b,c,d of class complex are created.
The object a uses default constructor, while b uses the parametrized, c uses the copy constructor which copies the properties of object a and d uses again the default constructor. The get() is used to assign values to the object a and d. later on inside the main function a switch case block drives the menu driven operations.
This Program is crafted using C++.
#include<iostream> using namespace std; class complex { public: int a,b;//a+ib complex() { a=0; b=0; } complex(int a,int b) { this->a=a;// This is used to prevent Ambiguity. this->b=b; } complex(complex &obj) { a=obj.a; b=obj.b; } void get() { cout<<"Enter The Real Part: ";cin>>a; cout<<"Enter The Imaginary Part: ";cin>>b; } void add(complex ob1,complex ob2) { a=ob1.a+ob2.a; b=ob1.b+ob2.b; } void sub(complex ob1,complex ob2) { a=ob1.a-ob2.a; b=ob1.b-ob2.b; } void mul(complex ob1,complex ob2) { a=ob1.a*ob2.a - ob1.b*ob2.b; b=ob1.a*ob2.b + ob1.b*ob2.a; } void div(complex ob1,complex ob2) { a=((ob1.a*ob2.a)+(ob1.b*ob2.b))/((ob2.a*ob2.a)+(ob2.b*ob2.b)); b=((ob1.b*ob2.a)-(ob1.a*ob2.b))/((ob2.a*ob2.a)+(ob2.b*ob2.b)); } void disp() { cout<<a<<" + "<<b<<"i"<<endl; } }; int main() { complex a,b(3,4),c(a),d; int ch; cout<<"Enter The Value Of the Object 'a': "<<endl; a.get(); cout<<"\nEnter The Value Of the Object 'd': "<<endl; d.get(); while(1) { cout<<"\nEnter Your Choice: \n 1.Add, 2.Sub, 3.Mul, 4.Div, 5.Exit \n:- "; cin>>ch; switch(ch) { case 1: c.add(a,b); cout<<"When object 'b' is passed: "; c.disp(); c.add(a,d); cout<<"When object 'd' is passed: "; c.disp(); break; case 2: c.sub(a,b); cout<<"When object 'b' is passed: "; c.disp(); c.sub(a,d); cout<<"When object 'd' is passed: "; c.disp(); break; case 3: c.mul(a,b); cout<<"When object 'b' is passed: "; c.disp(); c.mul(a,d); cout<<"When object 'd' is passed: "; c.disp(); break; case 4: c.div(a,b); cout<<"When object 'b' is passed: "; c.disp(); c.div(a,d); cout<<"When object 'd' is passed: "; c.disp(); break; case 5: return 0; } } }
Description
In the main function the ojects a,b,c,d of class complex are created.
The object a uses default constructor, while b uses the parametrized, c uses the copy constructor which copies the properties of object a and d uses again the default constructor. The get() is used to assign values to the object a and d. later on inside the main function a switch case block drives the menu driven operations.
Comments
Post a Comment