Download presentation
Presentation is loading. Please wait.
Published byAudra Hunter Modified over 8 years ago
1
1
2
Inheritance Inheritance Define a Class Hierarchy Define a Class Hierarchy Visibility Mode Visibility Mode Access Rights of Derived Classes Access Rights of Derived Classes Access Control to different Members of a Class Access Control to different Members of a Class
3
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. 3
4
4 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 EngineerEmployee BACK
5
5 Syntax: class DerivedClassName : visibility-mode BaseClassName { ………; // 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
When visibility mode is Private Then public members of a base class will be considered as private menbers of the derived class Then all public members of a base class will be considered as public menbers of the derived class 6 When visibility mode is Public
7
7 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
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
9 The type of inheritance defines the access level for the members of derived class that are inherited from the base class Access Specifier Private Derivation Protected Derivation Public Derivation private Not Inherited protectedprivateprotected publicprivateprotectedpublic Type of Inheritance Access Controlfor Members BACK
10
10 Access Specifier Accessible from own class Accessible from derived class Accesssible from Objects Outside Class PublicYes ProtectedYes No PrivateYesNo BACK
11
11
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.