Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance in Classes tMyn1 Inheritance in Classes We have used the Box class to describe a rectangular box – our definition of a Box object consisted.

Similar presentations


Presentation on theme: "Inheritance in Classes tMyn1 Inheritance in Classes We have used the Box class to describe a rectangular box – our definition of a Box object consisted."— Presentation transcript:

1 Inheritance in Classes tMyn1 Inheritance in Classes We have used the Box class to describe a rectangular box – our definition of a Box object consisted of just the three orthogonal dimensions. We might describe a Carton class which has the same properties as a Box object plus the additional property of its composite material. We might then specialize even further, by using the Carton definition to describe a class called FoodCarton – this will be a special kind of Carton which is designed to hold food.

2 Inheritance in Classes tMyn2 length breadth height length breadth height material length breadth height material contents class FoodCarton class Box class Carton Inherited members Inherited members More General More Specialized Each class has the properties of the class it is derived from, plus additional properties that differentiate it.

3 Inheritance in Classes tMyn3 The Carton class is an extension of the Box class – you might say that the Carton class is derived from the specification of the Box class. In a similar way, the FoodCarton class has been derived from the Cartoon class. By following this process, we develop a hierarchy of interrelated classes. In the hierarchy, one class is derived from another by adding extra properties – in other words, by specializing.

4 Inheritance in Classes tMyn4 Given a class A, suppose that we create a new, specialized class B. So the class A is called the base class and the B is called the derived class. The derived class automatically contains all of the data members of the base class, and (with some restrictions which we will discuss) all the function members. The derived class is said to inherit the data members and function members of the base class.

5 Inheritance in Classes tMyn5 If class B is a derived class defined directly in terms of class A, then we say that class A is a direct base class of B. We also say that B is derived from A. In the example above, the class Carton is a direct base class of FoodCarton. Because Carton is itself defined in terms of the class Box, we say that the class Box is an indirect base class of the class FoodCarton. An object of the FoodCarton class will have inherited members from Carton – including the members that the Carton class inherited from the Box class.

6 Inheritance in Classes tMyn6 In the diagram above, each class has all the properties of the Box class (on which it is based), and this illustrates precisely the mechanism of class inheritance in C++. The derived class has a complete set of data members and member functions from the base class, plus its own data members and member functions. Thus, each derived class object contains a complete base class sub-object, plus other members. When does inheritance take place? There are a number of simple tests that you can employ.

7 Inheritance in Classes tMyn7 The first is the is a kind of test: any derived class object is a kind of base class object. In other words, a derived class should describe a subset of the objects represented by the base class. For example: a class Dog might be derived from a class Animal. This makes sense because a dog is a kind of animal. The is a kind of test is an excellent first check, but it’s not infallible. For example, suppose that we defined a class, Bird, that (among other things) reflected the fact that most birds can fly…

8 Inheritance in Classes tMyn8 …but now an ostrich is a kind of bird, but it’s nonsense to derive a class Ostrich from the Bird class, because ostriches can’t fly!! If your classes pass the is a kind of test, then you should double check by asking the following question: Is there anything I can say about (or demand of) the base class that’s inapplicable to the derived class? If there is, then the derivation probably isn’t safe.

9 Inheritance in Classes tMyn9 If your classes fail the is a kind of test, then you almost certainly shouldn’t use class derivation. In this case, you could instead implement the has a test. A class object passes the has a test, if it contains an instance of another class. You can implement this situation by including an object of the second class as a data member of the first. This type of dependence is called aggregation.

10 Inheritance in Classes tMyn10 For example, consider a class Automobile. This class is likely to contain major automobile components as its class members. An automobile has an engine, has a transmission, has a chassis and has suspension. So it makes sense for the Automobile class to contain objects of type Engine, Transmission, Chassis and Suspension. But it is not true to say that an engine is a kind of automobile! Next example tries to demonstrate the order of constructor and destructor calls:

11 Inheritance in Classes tMyn11 #include "stdafx.h" #include using namespace System; using namespace std; class Box { public: Box(); ~Box(); void showBoxMembers(); protected: double length; double breadth; double* height; };

12 Inheritance in Classes tMyn12 Box::Box() { cout<<"Base class constructor called."<<endl <<"Creating now object "<<this<<endl; height=new double; cout<<"Input length: "; cin>>length; cin.get(); cout<<"Input breadth: "; cin>>breadth; cin.get(); cout<<"Input height: "; cin>>*height; cin.get(); }

13 Inheritance in Classes tMyn13 Box::~Box() { cout<<"Base class destructor called."<<endl <<"Deallocating dynamically allocated memory from object " <<this<<endl; delete height; height=0; } void Box::showBoxMembers() { cout<<"The dimensions of the base object:"<<endl <<"length: "<<length<<endl <<"breadth: "<<breadth<<endl <<"height: "<<*height<<endl; }

14 Inheritance in Classes tMyn14 class Carton:public Box { public: Carton(); ~Carton(); void showCartonMembers(); private: double* weight; }; Carton::Carton() { cout<<"Derived class constructor called."<<endl <<"Creating now object "<<this<<endl; weight= new double; cout<<"Input weight: "; cin>>*weight; cin.get(); } Carton is derived directly from Box The keyword public is the base class access specifier, and it indicates how the members of Box are to be accessed within the Carton class.

15 Inheritance in Classes tMyn15 Carton::~Carton() { cout<<"Derived class destructor called."<<endl <<"Deallocating dynamically allocated memory from object " <<this<<endl; delete weight; weight=0; } void Carton::showCartonMembers() { cout<<"The dimensions of the derived object:"<<endl <<"length: "<<length<<endl <<"breadth: "<<breadth<<endl <<"height: "<<*height<<endl <<"weight: "<<*weight<<endl; }

16 Inheritance in Classes tMyn16 int main(array ^args) { Box first=Box(); first.showBoxMembers(); Carton second=Carton(); second.showCartonMembers(); return 0; } First type automatic objects…..

17 Inheritance in Classes tMyn17

18 Inheritance in Classes tMyn18 int main(array ^args) { Box* third=new Box(); third->showBoxMembers(); delete third; Carton* fourth=new Carton(); fourth->showCartonMembers(); delete fourth; return 0; } Next dynamic objects…..

19 Inheritance in Classes tMyn19

20 Inheritance in Classes tMyn20 So as a summary: Inheritance is a means of specifying hierarchical relationships between types. What happens when a derived class object is created and destroyed: 1.Space is allocated (on the stack or the free store) for the full object (that is, enough space to store the data members inherited from the base class plus the data members defined in the derived class itself). 2.The base class’s constructor is called to initialize the data members inherited from the base class.

21 Inheritance in Classes tMyn21 3.The derived class’s constructor is then called to initialize the data members added in the derived class. 4.The derived class object is then usable. 5.When the object is destroyed (goes out of scope or is deleted) the derived class’s destructor is called on the object first. 6.Then the base class’s destructor is called on the object. 7.Finally the allocated space for the full object is reclaimed.

22 Inheritance in Classes tMyn22 If we had the statement… first.length=10.9; …the program will no longer compile. Reason: The length data member of the Box object first was declared as a protected data member, and so it’s not accessible to this statement.

23 Inheritance in Classes tMyn23 If we had the statement… second.length=10.8; …the program will again no longer compile. This is perhaps a little unexpected – after all, when we defined the Carton class, didn’t we inherit the Box class members as public? The reason for the error is that length is a protected data member in the base class. In the derived class Carton, the length member is a publicly inherited protected data member.

24 Inheritance in Classes tMyn24 The compiler interprets this by making length a protected member of Carton. Access to the inherited members of a derived class object is determined by both the access specifier of the data member in the base class and the access specifier of the base class in the derived class.

25 Inheritance in Classes tMyn25 Let’s add a few more lines to the main(): Basically it is OK to use a pointer to base class to store the address of a derived class object, but so far… int main(array ^args) { Box* fifth=new Carton(); fifth->showBoxMembers(); delete fifth; return 0; }

26 Inheritance in Classes tMyn26 …If we do not make any additional changes to our program, then the output would be: The derived class destructor will never be called!!

27 Inheritance in Classes tMyn27 The problem occurs because the binding to the destructor is being set at compile time, and since we’re applying the delete operator to an object pointed to by a pointer to Box, the Box class destructor is called. To fix this, we need dynamic binding for the destructors.


Download ppt "Inheritance in Classes tMyn1 Inheritance in Classes We have used the Box class to describe a rectangular box – our definition of a Box object consisted."

Similar presentations


Ads by Google