1 / 11

INHERITANCE IN C++

INHERITANCE IN C++. Topics to be discussed. Inheritance Define a Class Hierarchy Visibility Mode Access Rights of Derived Classes Access Control to different Members of a Class. Inheritance. Inheritance is a mechanism of deriving a new class(derived class) from an old one(base class).

Download Presentation

INHERITANCE IN C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. INHERITANCE IN C++

  2. Topics to be discussed • Inheritance • Define a Class Hierarchy • Visibility Mode • Access Rights of Derived Classes • Access Control to different Members of a Class

  3. Inheritance • Inheritance is a mechanism of deriving a new class(derived class) from an old one(base class). • For example,an organization consists of various types of employees like managers,engineers,technicians and so on.In this case,employee is the base class and engineer is the derived class.

  4. Employee Engineer Putting Them Together • Employee is the base class • Engineer is the derived class • The derived class can • inherit all members from the base class, except the constructor • access all public and protected members of the base class • define its private data member • provide its own constructor • define its public member functions BACK

  5. Define a Class Hierarchy • Syntax: classDerivedClassName : visibility-modeBaseClassName { ………; // members of derived class ………; } The Colon Symbol(: ) shows the relationship of a derived class to its base class where • Visibility-mode specifies the type of derivation • private by default, or public • Any class can serve as a base class • Thus a derived class can also be a base class BACK

  6. Visibility Mode • Then all public members of a base class will be considered as public menbers of the derived class When visibility mode is Public When visibility mode is Private Then public members of a base class will be considered as private menbers of the derived class

  7. Protected Access Specifier • The private members of the base class can not be inherited and it is not available for the derived class. • One way to solve this problem is by changing the visibility mode of the private member by making it public. • When a member declared as Protected is accesible by the member functions within the class and any class immediately derived from it.

  8. class class_name { private: … //By default … //visible to member functions … of its own protected: … //visible to member functions … of its own and derived class … public: … // visible to all … … }; BACK

  9. Access Rights of Derived Classes Type of Inheritance • The type of inheritance defines the access level for the members of derived classthat are inherited from the base class Access Control for Members BACK

  10. Access Control to different Members of a Class BACK

  11. Thanks

More Related