Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance in C++ Inheritance Protected Section

Similar presentations


Presentation on theme: "Inheritance in C++ Inheritance Protected Section"— Presentation transcript:

1 Inheritance in C++ Inheritance Protected Section
Constructors and Destructors with inheritance Multiple Inheritance Function overriding

2 Inheritance The inheriting class (commonly referred to as the derived class) automatically gets the methods and data fields that are inheritable from the inherited class (commonly referred to as the base class) in the of the class definition that is being inherited from. The private functions and data do not get inherited. Example) class savings_account : public account { more stuff }

3 Public and Private inheritance
Public inheritance class savings_account : public account { more stuff } Public elements get inherited in the public area Private Inheritance class savings_account : private account Public elements get inherited into the private area Note: Inheritance can never promote elements, only demote.

4 Protected Section Suppose you wanted to have an element that needs to be private however you would like an inheriting class to be able to inherit it. That is, you would like to have a protection level that is “private but inheritable.” The section “Protected” was introduced for just that reason. Public: accessible to the outside world Protected: can be inherited but not accessible outside the class Private: can not be inherited and not accessible outside the class

5 Inheritance hierarchy
Base class member access specifier Type of Inheritance Public Protected Private Public Area Public in derived class. Can be accessed directly by any non-static member function, friend functions and non- member functions Protected in derived class Can be accessed directly by all non-static member functions and friend functions Private in derived class Can be accessed directly by all non-static member functions and friend functions Protected Area Can be accessed directly by all non-static member functions and friend functions Protected in derived class. Can be accessed directly by all non-static member functions and friend functions. Can be accessed directly by all non-static member functions and friend functions. Private Area Hidden in the derived class Can be accessed by non-static member functions and friend functions through public or protected member functions of the base class Can be accessed by non- static member functions and friend functions through public or protected member functions of the base class Can be accessed by non- static member functions and friend functions through public or protected member functions of the base class

6 Constructors and Destructors
Constructors are called in the order of Base, then Derived The first line of a derived class constructor must be a call to the base class’s constructor. A derived class constructor always calls the constructor for its base class first to initialize the derived class’ base- class members. If the derived-class constructor is omitted, the derived class’ default constructor calls the base-class’ default constructor. Destructors are called in reverse order of construction calls, so a derived class destructor is called before its base-class destructor.

7 Example: Constructors/Destructors
class BaseClass { private: public: B b, BaseClass(); ~BaseClass(); } class DerivedClass : public BaseClass D d; DerivedClass(); ~DerivedClass();

8 Example continued When an object of the type DerivedClass is constructed: The order of constructors is B, BaseClass, D, DerivedClass The order of destructors is the reverse ~DerivedClass, ~D, ~BaseClass, ~B

9 Multiple Inheritance Structure

10 Multiple Inheritance Declaration
Consider the following multiple inheritance class definition class clock-radio : public clock, public radio { more clock-radio stuff } Consider the following questions

11 Multiple Inheritance Example Questions
How many copies of the base class will “Electronics” would a clock-radio get? Suppose clock has a member function void soundAlarm() which sounds a buzzer for 1 minute and Radio has a member function void play(float station, int volume) which plays the radio. Can clock-radio have a member function also with the signature void soundAlarm() which calls the Radio’s play function. Suppose clock and radio both had a function with the same exact signature, say void powerUp(), and you called clock-radio’s powerUp(), what would happen?

12 Order of Declaration The order in which member objects are constructed is the order in which those objects are declared within the class definition. The order in which the member initializations are listed does not affect order of constructions.

13 Order based on inheritance
In inheritance, base-class constructors are called in the order in which inheritance is specified in the derived class definition. The order in which the base-class constructors are specified in the derived-class member initializer list does not affect the order of construction.


Download ppt "Inheritance in C++ Inheritance Protected Section"

Similar presentations


Ads by Google