c++ - Inheritence
-
Ronald Weidner
(143/143)
Dec 19 2008
#include
#include <iostream> using namespace std; class Mother { private: int iAge; public: Mother () { cout << "Mother Constructor: no parameters\n"; iAge = 0; } Mother (int a) { cout << "Mother Constructor: int parameter\n"; iAge = a; } void PrintAge() { cout << "Age: " << iAge << "\n"; } void SetAge(int a) { iAge = a; } int GetAge() { return iAge; } }; // Here we will create a Daughter class that will inherit from Mother class Daughter : public Mother { public: Daughter (int a) { cout << "Daughter Constructor: int parameter\n"; // The Lesson to be Learned Here... // We need to call SetAge() because in C++ constructors // are not inherited. So... just because there is a // Mother(int a ) constructor doesn't mean that the // child class constructor will call it even though // calling parameters match. See the "Son" class to see // how map constructors so that the desired one is called. SetAge(a); // This PrintAge is defined in this class and over rides // the PrintAge defined in Mother. PrintAge(); } // Over ride PrintAge defined in Mother. void PrintAge() { // Here is the C++ equivalent to java's super() // The main distinction here is that this did not // have to be the first line of the method. We can, // in C++ execute other commands before mimicking super() Mother::PrintAge(); cout << "\n"; } }; // Here we will create a Son class that will also inherit from Mother class Son : public Mother { public: // Here we are "mapping" the Mother constructor to the Son // constructor. What will happen here is Son(int a) constructor // will be called. Immediately before execution of the Son(int a) // construtor, the Mother(int a) code will be executed, // Then the Son(int a) code will execute. Son (int a) : Mother(a) { cout << "Son Constructor: int parameter\n"; PrintAge(); } // Comment out this constructor to see that Son will not inherit from // Mother the constructor with the empty params (). If a constructor // is defined, there will be no more "free" constructors. (Inherited or // otherwise.) ///* Son() : Mother() { cout << "Son Constructor: No Parameters\n"; } //*/ void PrintAge() { cout << "The Son's age is: " << GetAge() << "\n\n"; } }; int main () { Daughter katie(15); Son bobby(12); // If we comment out the Son() constructor in the Son class, we will see that // we will not inherit the Mother(). That's because once we define a constructor // in a class, (Son(int a) in this case.) we will no longer get a default // constructor, or inherit parent constructors. // Additionally, we cannot instantiate this class as follows... // // Son ronnie(); // // Well, technically we could but, this may lead to compile time errors that could // potentially be hard to track down. If Son() is not defined, a compile time // error should immediately occur. But it won't. Instead the compile error won't // occur until you try to use the object. (Which could be many lines of code later.) // // So, as a best practice let's not use Son ronnie() to represent the empty param // constructor. Instead we should use Son ronnie . The error doesn't seem to // manifest when using "new" to create the object under the same conditions. Instead, // a compile time exception is thrown as it should given the rules/conditions stated // above. Son ronnie; ronnie.SetAge(18); ronnie.PrintAge(); return 0; } // Program Output... // ************** // // Mother Constructor: no parameters // Daughter Constructor: int parameter // Age: 15 // // Mother Constructor: int parameter // Son Constructor: int parameter // The Son's age is: 12 // // Mother Constructor: no parameters // Son Constructor: No Parameters // The Son's age is: 18 // **************** // -- // Ronald Weidner
Dec 19 2008