Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. class Complex //create a new class type named Complex
  4. {
  5. // no accessor here means we are using the default – private for the following (until the next accessor) – so for _re and _im
  6. double _re; // declare the two attributes as private _re and _im
  7. double _im; // prefix the attributes with _ to distinbguish them from methods
  8. //from here onward the accessor is public (methods are public for this class example)
  9. public:
  10. //declaring the class methods inside the class
  11. //we can define them outside the class due to the “public:” accessor
  12. void Re(double val); //setter method for Re() - public
  13. void Im(double val); //setter method for Im() – public
  14. double Re(); //getter method for Re() - public
  15. double Im(); //getter method for Im() - public
  16. Complex(); //implicit constructor for class Complex
  17. };
  18. //the scope resolution (::) operator below says Re() belongs to class Complex
  19. void Complex::Re(double r)//define setter method for real part, argument r
  20. {
  21. _re=r; //attribute _re is set with value from method Re() argument r
  22. }
  23. void Complex::Im(double i)// define setter method for imaginary part,argument i
  24. {
  25. _im=i; // attribute _im is set with value from method Im() argument i
  26. }
  27. double Complex::Re() // define getter method for real part
  28. {
  29. return _re;
  30. }
  31. double Complex::Im() // define getter method for imaginary part
  32. {
  33. return _im;
  34. }
  35. Complex::Complex()//define class Complex constructor
  36. {
  37. //Need the scope operator (::) since we are outside the Complex class
  38. // scope. Useful for multi-class multi-file .cpp programs
  39. _re =0.0; //set default values for attributes
  40. _im =0.0; //to avoid uninitialization problems if not using setter method
  41. //upon object creation they are initialized with values from constructor
  42. }
  43. int main()
  44. {
  45. double real,imag;
  46. Complex c0;//create object named c0, as instance of class Complex
  47. //using setters
  48. cout<<"real part is:"; //cout is iostream’s printf equivalent
  49. cin>>real; //cin is iostream’s scanf equivalent
  50. // notice the difference in direction of the >> << operators
  51. // used for cout and cin
  52. cout<<"imaginary part is:";
  53. cin>>imag;
  54. c0.Re(real);
  55. c0.Im(imag);
  56. // print attributes using getters
  57. cout <<"Complex number entered by keyboard is: "<<c0.Re()<< " + " <<c0.Im()<<"i\n";
  58. Complex c1; //create object named c1, as instance of class Complex
  59. //use setters to set values in c1
  60. c1.Re(2.0); //use setter to set C1’s real part
  61. c1.Im(3.5); // use setter to set C1’s imaginary part
  62. // print attributes using getters
  63. cout <<"Complex number obtained by using setters is: "<<c1.Re()<< " + "
  64. << c1.Im()<<"i\n";
  65. Complex c2;//create object named c2, as instance of class Complex
  66. //will get it’s values from implicit constructor
  67. cout <<" Complex number obtained by using implicit constructor is:"<<c2.Re()<< " + " << c2.Im()<<"i\n";
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement