Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Inheritance: constructors Constructors, copy constructors and destructors are NOT inherited. Each subclass has its own constructors and destructor to.

Similar presentations


Presentation on theme: "1 Inheritance: constructors Constructors, copy constructors and destructors are NOT inherited. Each subclass has its own constructors and destructor to."— Presentation transcript:

1 1 Inheritance: constructors Constructors, copy constructors and destructors are NOT inherited. Each subclass has its own constructors and destructor to handle the creation and destruction of its own data members. The superclass constructor initializes the data members defined in the superclass The subclass constructor calls the superclass constructor (which initializes the inherited members) and then goes on to initialize its own data members. The destructor works in a similar way, but in reverse order.

2 2 Inheritance : constructors Example 1: class Base { public: Base() { cout << “Base::Base()\n”; } }; class Derived : public Base { public: Derived() { cout << “Derived::Derived()\n”; } }; int main () { Derived obj; return 0; } Output: Base::Base() Derived::Derived()

3 3 Inheritance : constructors Example 2: class Base { public: Base() { cout << “Base::Base()\n”; } Base(int x) { cout << “Base::Base(x)\n”; } }; int main () { Derived obj(10); return 0; } class Derived : public Base { public: Derived() { cout << “Derived::Derived()\n”; } Derived(int x) : Base(x) { cout << “Derived::Derived(x)\n” } }; Output: Base::Base(x) Derived::Derived(x)

4 4 Inheritance : destructors Example 3: class Base { public: Base() {} ~Base() { cout << “Base::~Base()\n”; } }; class Derived : public Base { public: Derived() { } ~Derived() { cout << “Derived::~Derived()\n”; } }; int main () { Derived obj; return 0; } Output: Derived::~Derived() Base::~Base()

5 5 Inheritance: hiding methods A derived class may use the base class’s member functions. A derived class may also redefine the base class’s member functions. In that case, the derived class’s definition hides the one in the base class.

6 6 Inheritance : hiding methods Example 1: class Base { private: public: Base() { } void print() {cout << "Base::print()\n";} }; class Derived : public Base { private: public: Derived() { } }; int main () { Derived obj; obj.print(); return 0; } Output: Base::print()

7 7 Inheritance : hiding methods Example 2: class Base { private: public: Base() { } void print() {cout << "Base::print()\n";} }; class Derived : public Base { private: public: Derived() { } void print() {cout << “Derived::print()\n";} }; int main () { Derived obj; obj.print(); return 0; } Output: Derived::print()

8 8 Inheritance : hiding methods Example 3: class Base { private: public: Base() { } void print() {cout << "Base::print()\n";} void print(int x) { cout << “Base::print(x)\n”;} }; class Derived : public Base { private: public: Derived() { } void print() {cout << “Derived::print()\n";} }; int main () { Derived obj; obj.print(10); return 0; } print() is overloaded this definition hides BOTH Base::prints. COMPILER ERROR! Base::print(x) is hidden by Derived::print()

9 9 Inheritance : hiding methods Example 4: class Base { private: public: Base() { } void print() {cout << "Base::print()\n";} void print(int x) { cout << “Base::print(x)\n”;} }; class Derived : public Base { private: public: Derived() { } void print() {cout << “Derived::print()\n";} void print(int x) {cout << “Derived::print(x)\n";} }; int main () { Derived obj; obj.print(10); return 0; } Output: Derived::print(x)

10 10 Virtual functions Recall the class hierarchy: We wish to create an array of pointers to various subtypes of Vehicles. In addition, each member of the array should behave according to each subclass. This can be achieved if every Vehicle member function that is overriden in the subclasses is declared as virtual. Vehicle TruckTrainBicycle

11 11 Virtual functions Example class Vehicle { public: Vehicle() { } virtual void print() { cout << “Vehicle\n”;} }; class Train : public Vehicle { public: Train() { } virtual void print() { cout << “Train\n”;} } class Truck : public Vehicle { public: Truck() { } virtual void print() { cout << “Truck\n”;} }; int main () { Vehicle *fleet[2]; fleet[0] = new Train; fleet[1] = new Truck; fleet[0]->print(); fleet[1]->print(); delete fleet[0]; delete fleet[1]; return 0; } Output: Train Truck

12 12 Multiple inheritance A class may inherit from more than one base class. BoatCar inherits the members of Car and those of Boat. BoatCar's constructor calls the constructors of Car and Boat in the order specified in BoatCar's definition (i.e. as they appear after the colon) class Car {... }; class Boat {... }; class BoatCar: public Car, public Boat {... };


Download ppt "1 Inheritance: constructors Constructors, copy constructors and destructors are NOT inherited. Each subclass has its own constructors and destructor to."

Similar presentations


Ads by Google