Object-Oriented Programming in C++: Constructors
Submitted by PhoeniciansSy on Wednesday, September 10, 2014 - 06:51.
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:- class point {
- double x,y; // private data members
- public: // public methods
- point (int x0,int y0); // a constructor
- point () { x = 0; y = 0;}; // a constructor
- point (double alpha; doubler); // a constructor
- void move (int dx,int dy);
- void rotate (double alpha);
- int distance (point p);
- }
- int main()
- {
- point p1 (10,10), p2, p3 (pi /4, 2.5);
- }
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:- class point {
- double x,y;
- public:
- point () { x = 0; y = 0;}; // default constructor
- }
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:- class point {
- double x,y;
- public:
- point (int x0,int y0); // a constructor
- point (double alpha; double r); // a constructor
- }
- point p1 (10, 20); // call constructor with given arguments
- 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:- // Declaration of the Time class.
- class Time {
- public:
- Time( int h = 0, int m = 0,int s = 0 ) // default constructor
- {
- hour = h;
- minute = m;
- second = s;
- }
- void setTime( int h, int m, int s) // set hour, minute, second
- {
- hour = ( h >= 0 && h < 24 ) ? h : 0;
- minute = ( m >= 0 && m < 60 ) ? m : 0;
- second = ( s >= 0 && s < 60 ) ? s : 0;
- }
- void print24() // print time in 24 format
- {
- cout << ( hour < 10 ? "0" : "" ) << hour << ":"
- << ( minute < 10 ? "0" : "" ) << minute;
- }
- void print12() // print time in 12 format
- {
- cout << ( ( hour == 0 || hour ==12 ) ? 12 : hour % 12 )
- << ":" << ( minute < 10 ? "0" : "" ) << minute
- << ":" << ( second < 10 ? "0" : "" ) << second
- << ( hour < 12 ? " AM" : " PM" );
- }
- private:
- int hour; // 0 - 23
- int minute; // 0 - 59
- int second; // 0 - 59
- };
- int main()
- {
- Time t1, // all arguments defaulted
- t2(2), // minute and second defaulted
- t3(21, 34), // second defaulted
- t4(12, 25, 42), // all values specified
- t5(27, 74, 99); // all bad values specified
- cout << "Constructed with:\n"
- << "all arguments defaulted:\n ";
- t1.print24();
- cout << "\n ";
- t1.print12();
- cout << "\nhourspecified; minute and second defaulted:"
- << "\n ";
- t2.print24();
- cout << "\n ";
- t2.print12();
- cout << "\nhourand minute specified; second defaulted:"
- << "\n ";
- t3.print24();
- cout << "\n ";
- t3.print24();
- cout << "\nhour, minute, and second specified:"
- << "\n ";
- t4.print24();
- cout << "\n ";
- t4.print12();
- cout << "\nall invalid values specified:"
- << "\n ";
- t5.print24();
- cout << "\n ";
- t5.print12();
- cout <<endl;
- return 0;
- }
Add new comment
- 89 views