15.2 CONSTRUCTORS FOR DERIVED CLASSES IN C


15.2 CONSTRUCTORS FOR DERIVED CLASSES IN C++

A derived-class constructor must call a base-class constructor before it does anything else. If the base class constructor is not called explicitly in a derived-class constructor, the system will try to invoke the base-class's no-arg constructor. But, remember, a base class will have a no-arg constructor only if you provide one, or, by default, if you have defined no constructors at all for the base class. In what follows, we will first use the User class example to illustrate the more common case, which is that of a derived-class constructor making an explicit call to a base-class constructor.

 
//DerivedConstructor.cc #include <iostream> #include <string> using namespace std; class User { // BASE string name; int age; public: User( string nm, int a ) { name = nm; age = a;} void print(){cout << "Name: " << name << " Age: " << age;} }; class StudentUser : public User { // DERIVED string schoolEnrolled; public: StudentUser( string nam, int y, string school ) : User( nam, y ) { //(A) schoolEnrolled = school; } void print() { User::print(); cout << " School Enrolled: " << schoolEnrolled << endl; } }; int main() { StudentUser student( "Maura", 20, "ece" ); student.print(); // Name: Maura Age: 20 School Enrolled: ece return 0; }

Through the initialization syntax in line (A), the constructor of the derived-class object explicitly calls the base-class constructor to construct the base-class portion of the derived class object. After that, the derived class constructor proceeds to bring into existence what is defined explicitly for the derived class. A derived class object thus built is best shown pictorially as in Figure 15.1.

click to expand
Figure 15.1

As was mentioned at the beginning of this section, if a base-class constructor is not invoked explicitly in a derived-class constructor, the system tries to invoke the no-arg constructor of the base class for initializing the base-class subobject. In the following program, the derived-class constructor in line (D) makes no mention of a base-class constructor. The base-class has been provided with an explicitly defined no-arg constructor in line (A) and a 1-arg constructor in line (C):

 
//DerivedConstWithBaseNoArg.cc #include<iostream> using namespace std; class X { // BASE int m; public: // no-arg constructor: X() : m(10) { //(A) cout << "inside X';s no-arg constructor" << endl; //(B) } // 1-arg constructor: X(int mm): m(mm){} //(C) int getm() { return m; } }; class Y : public X { // DERIVED int n; public: // constructor: Y(int nn) : n(nn) {} //(D) int getn() { return n; } }; int main() { Y yobj( 100 ); //(E) cout << yobj.getm() << " " //(F) << yobj.getn() << endl; //(G) }

Even though the derived-class constructor in the program above makes no explicit call to any base-class constructors, the system nonetheless has a need for initializing the base-class subobject inside the derived-class object yobj whose construction is called for in line (E) of main. So it proceeds to use the no-arg constructor defined for the base class in line (A). This is clear from the output of this program produced by the statements in lines (B), (F), and (G):

      inside X's no-arg constructor      10 100 

A derived class may be extended further and the process continued to any depth. For example, we could have

      class X { /* */ };      class Y : public X { /* */ };      class Z : public Y { /* */ }; 

If we invoke a constructor for class Z, the resulting object will have inside it a subobject corresponding to its immediate base Y, and that subobject will in turn have inside it a sub-subobject of type X, as we show in Figure 15.2.

click to expand
Figure 15.2




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net