Object-Oriented Programming in C++: Constructors

Contents:

1. Constructors. 2. Default Constructors. 3. Constructors with Arguments. 4. Time Class with Constructors.

Constructors:

Constructor is a member function in the class, which has the following characteristics: 1. It has the same name as the class. 2. It doesn't return any type. 3. It is called every time an instance of a class is created. 3. It initialize the class members with initial values. We can have more than one constructor in a class, for example:
  1. class point {
  2. double x,y; // private data members
  3. public: // public methods
  4. point (int x0,int y0); // a constructor
  5. point () { x = 0; y = 0;}; // a constructor
  6. point (double alpha; doubler); // a constructor
  7. void move (int dx,int dy);
  8. void rotate (double alpha);
  9. int distance (point p);
  10. }
And In the main function we can call them as follow:
  1. int main()
  2. {
  3. point p1 (10,10), p2, p3 (pi /4, 2.5);
  4. }

Default Constructors:

Default Constructor is a constructor that can be called without having to provide any arguments. Like all member function, the implementation of the default constructor begins with '{' ends with '}', and its body is located between those brackets, for example:
  1. class point {
  2. double x,y;
  3. public:
  4. point () { x = 0; y = 0;}; // default constructor
  5. }

Constructors with Arguments:

May the programmer wants to create objects from a class with initial values, then he can define constructors with arguments. For example:
  1. class point {
  2. double x,y;
  3. public:
  4. point (int x0,int y0); // a constructor
  5. point (double alpha; double r); // a constructor
  6. }
And we can call the constructors in the above example as follow:
  1. point p1 (10, 20); // call constructor with given arguments
  2. p3 (pi / 4, 2.5); // call constructor with given arguments

Time Class with Constructors:

In the following example we re-implement Time class with constructors and default arguments:
  1. // Declaration of the Time class.
  2. class Time {
  3. public:
  4. Time( int h = 0, int m = 0,int s = 0 ) // default constructor
  5. {
  6. hour = h;
  7. minute = m;
  8. second = s;
  9. }
  10. void setTime( int h, int m, int s) // set hour, minute, second
  11. {
  12. hour = ( h >= 0 && h < 24 ) ? h : 0;
  13. minute = ( m >= 0 && m < 60 ) ? m : 0;
  14. second = ( s >= 0 && s < 60 ) ? s : 0;
  15. }
  16. void print24() // print time in 24 format
  17. {
  18. cout << ( hour < 10 ? "0" : "" ) << hour << ":"
  19. << ( minute < 10 ? "0" : "" ) << minute;
  20. }
  21. void print12() // print time in 12 format
  22. {
  23. cout << ( ( hour == 0 || hour ==12 ) ? 12 : hour % 12 )
  24. << ":" << ( minute < 10 ? "0" : "" ) << minute
  25. << ":" << ( second < 10 ? "0" : "" ) << second
  26. << ( hour < 12 ? " AM" : " PM" );
  27. }
  28. private:
  29. int hour; // 0 - 23
  30. int minute; // 0 - 59
  31. int second; // 0 - 59
  32. };
  33.  
  34. int main()
  35. {
  36. Time t1, // all arguments defaulted
  37. t2(2), // minute and second defaulted
  38. t3(21, 34), // second defaulted
  39. t4(12, 25, 42), // all values specified
  40. t5(27, 74, 99); // all bad values specified
  41.  
  42. cout << "Constructed with:\n"
  43. << "all arguments defaulted:\n ";
  44. t1.print24();
  45. cout << "\n ";
  46. t1.print12();
  47.  
  48. cout << "\nhourspecified; minute and second defaulted:"
  49. << "\n ";
  50. t2.print24();
  51. cout << "\n ";
  52. t2.print12();
  53.  
  54. cout << "\nhourand minute specified; second defaulted:"
  55. << "\n ";
  56. t3.print24();
  57. cout << "\n ";
  58. t3.print24();
  59.  
  60. cout << "\nhour, minute, and second specified:"
  61. << "\n ";
  62. t4.print24();
  63. cout << "\n ";
  64. t4.print12();
  65.  
  66. cout << "\nall invalid values specified:"
  67. << "\n ";
  68. t5.print24();
  69. cout << "\n ";
  70. t5.print12();
  71. cout <<endl;
  72.  
  73. return 0;
  74. }
Note: You can find the full source code of this example in code.zip file.

Tags

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
3 + 16 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.