Presentation is loading. Please wait.

Presentation is loading. Please wait.

CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.

Similar presentations


Presentation on theme: "CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes."— Presentation transcript:

1 CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes and functions. The class you inherit from is called the base class. The class that inherits from the base class is known as a derived class. By inheriting a class: You can add additional functionality through the derived class. You can add additional data through the derived class. You can modify how a class function behaves. 1

2 CLASS INHERITANCE Inheritance so that it works properly in all situations requires careful management. Generic form of C++ code inheritance class DerivedClass : public BaseClass {... }; : public BaseClass is the Inheritance code; that is you are inheriting from the base class you will note that the inheritance here is public. 2

3 CLASS INHERITANCE You can inherit from a class Public derivation Protected derivation Private derivation Base class accessInheritance access specifier publicprotectedprivate public protectedprivate protected private 3

4 CLASS INHERITANCE A derived object inherits the data members of the base class. (The derived class inherits the base-class implementation.) A derived object inherits the functions of the base class. (The derived class inherits the base-class interface.) Once you have inherited the base class you are now in a position to extend its functionality through the derived class. 4

5 CLASS INHERITANCE What you need to add A derived class needs its own constructors. A derived class can add additional data members and member functions as needed. A derived class does not have direct access to the private members of the base class; it has to work through the base- class functions. The derived-class constructors have to use the base-class constructors. When a program constructs a derived-class object, it first constructs the base-class object. 5

6 CLASS INHERITANCE Conceptually, that means the base-class object should be constructed before the program enters the body of the derived-class constructor. Derived-class function can call a public base-class function. void Derived::Init() { Base::Init(); cout << "Something" << endl; } 6

7 CLASS INHERITANCE C++ uses the member initialiser list syntax to accomplish this. The ‘:’ starts the member initialiser list. It’s executable code, and it calls the BaseClass constructor. DerivedClass::DerivedClass( unsigned int uiR, const char * pc_cStr, bool bFlag ) : BaseClass(pc_cStr, bFlag) { m_uiRating = uiR; } 7

8 Constructors: Access Considerations What if you omit the member initialiser list? The base-class object must be created first, so if you omit calling a base-class constructor, the program uses the default base-class constructor. These are the key points about constructors for derived classes: The base-class object is constructed first. The derived-class constructor should pass base-class information to a base-class constructor via a member initialiser list. The derived-class constructor should initialise the data members that were added to the derived class. 8

9 Constructors: Access Considerations The derived-class constructor is responsible for initialising any added data members. A derived-class constructor always calls a base-class constructor. You can use the initialiser list syntax to indicate which base-class constructor to use. Otherwise, the default base-class constructor is used. When an object of a derived class expires, the program first calls the derived-class destructor and then calls the base-class destructor. 9


Download ppt "CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes."

Similar presentations


Ads by Google