Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class Complex //create a new class type named Complex
- {
- // no accessor here means we are using the default – private for the following (until the next accessor) – so for _re and _im
- double _re; // declare the two attributes as private _re and _im
- double _im; // prefix the attributes with _ to distinbguish them from methods
- //from here onward the accessor is public (methods are public for this class example)
- public:
- //declaring the class methods inside the class
- //we can define them outside the class due to the “public:” accessor
- void Re(double val); //setter method for Re() - public
- void Im(double val); //setter method for Im() – public
- double Re(); //getter method for Re() - public
- double Im(); //getter method for Im() - public
- Complex(); //implicit constructor for class Complex
- };
- //the scope resolution (::) operator below says Re() belongs to class Complex
- void Complex::Re(double r)//define setter method for real part, argument r
- {
- _re=r; //attribute _re is set with value from method Re() argument r
- }
- void Complex::Im(double i)// define setter method for imaginary part,argument i
- {
- _im=i; // attribute _im is set with value from method Im() argument i
- }
- double Complex::Re() // define getter method for real part
- {
- return _re;
- }
- double Complex::Im() // define getter method for imaginary part
- {
- return _im;
- }
- Complex::Complex()//define class Complex constructor
- {
- //Need the scope operator (::) since we are outside the Complex class
- // scope. Useful for multi-class multi-file .cpp programs
- _re =0.0; //set default values for attributes
- _im =0.0; //to avoid uninitialization problems if not using setter method
- //upon object creation they are initialized with values from constructor
- }
- int main()
- {
- double real,imag;
- Complex c0;//create object named c0, as instance of class Complex
- //using setters
- cout<<"real part is:"; //cout is iostream’s printf equivalent
- cin>>real; //cin is iostream’s scanf equivalent
- // notice the difference in direction of the >> << operators
- // used for cout and cin
- cout<<"imaginary part is:";
- cin>>imag;
- c0.Re(real);
- c0.Im(imag);
- // print attributes using getters
- cout <<"Complex number entered by keyboard is: "<<c0.Re()<< " + " <<c0.Im()<<"i\n";
- Complex c1; //create object named c1, as instance of class Complex
- //use setters to set values in c1
- c1.Re(2.0); //use setter to set C1’s real part
- c1.Im(3.5); // use setter to set C1’s imaginary part
- // print attributes using getters
- cout <<"Complex number obtained by using setters is: "<<c1.Re()<< " + "
- << c1.Im()<<"i\n";
- Complex c2;//create object named c2, as instance of class Complex
- //will get it’s values from implicit constructor
- cout <<" Complex number obtained by using implicit constructor is:"<<c2.Re()<< " + " << c2.Im()<<"i\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement