300 likes | 307 Views
Learn the basic concepts of inheritance in object oriented programming, including single inheritance and the "IS-A" relationship.
E N D
In the Name of Allah the Most Beneficent the Most Merciful Subject: Object Oriented ProgrammingBasic concepts of inheritance
Inheritance A child inherits characteristics of its parents Besides inherited characteristics, a child may have its own unique characteristics
Inheritance… The mechanism of deriving a new class from an old one is called inheritance (or derivation). The old class is referred to as the base class the new one is called the derived class or subclass. The derived class inherits some or all of the characteristics from the base class. Besides inherited characteristics, a child may have its own unique characteristics
Single Inheritance The colon indicates that the derived-class-name is derived from the base-class-name. The visibility mode is optional, and if present may be private or public. The default visibility mode is private. • A derived class with only one base class, is called single inheritance. • Syntax: • Class ABC : private XYZ • { • Members of ABC • }; • Class ABC : public XYZ • { • Members of ABC • };
Inheritance in Classes If a class B inherits from class A then it contains all the characteristics (information structure and behavior) of class A The parent class is called base class and the child class is called derivedclass Besides inherited characteristics, derived class may have its own unique characteristics
Example – Inheritance Person Doctor Student Teacher
Example – Inheritance Shape Triangle Line Circle
Inheritance – “IS A” or“IS A KIND OF” Relationship Each derived class is a special kind of its base class
Example – “IS A” Relationship Person name age gender Here, Student IS A Person Teacher IS A Person Doctor IS A Person eat walk Student Teacher Doctor program studyYear designation salary designation salary study heldExam teach takeExam checkUp prescribe
Example – “IS A” Relationship Shape color coord draw rotate setColor Here, Circle IS A Shape Line IS A Shape Triangle IS A Shape Triangle Circle angle radius Line draw computeArea length draw computeArea draw
Inheritance – Advantages Reuse Less redundancy Increased maintainability
Reuse with Inheritance • Main purpose of inheritance is reuse • We can easily add new classes by inheriting from existing classes • Select an existing class closer to the desired functionality • Create a new class and inherit it from the selected class • Add to and/or modify the inherited functionality
Example Reuse Shape color coord draw rotate setColor Triangle Circle angle radius Line draw computeArea length draw computeArea draw
Example Reuse Person name age gender eat walk Student Teacher Doctor program studyYear designation salary designation salary study heldExam teach takeExam checkUp prescribe
Example Reuse Person name age gender eat walk Student Teacher Doctor program studyYear designation salary designation salary study heldExam teach takeExam checkUp prescribe
Access Control and Inheritance A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.
What Is Inherited? • Inherited • Data members • Most member functions • Functions not inherited • Private members • Constructors, copy constructors • Friend functions
Making Private Member Inheritable C++ provides a third visibility modifier, Protected which serve a limited purpose in inheritance. A member declared as a protected is accessible by the member functions within its class and any class immediately derived from it. It can not be accessed by the functions outside these two classes. The keywords Private, Protected and Public may appear in any order and any number of times in the declaration of class.
Example Is a valid class definition • Class ABC • { • Protecte : • ……………….. • Public : • ……………….. • Private : • ………………… • Public : • ……………….. • };
Types of Inheritance When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. The type of inheritance is specified by the access-specifier. We hardly use protected or private inheritance, but public inheritance is commonly used. While using different type of inheritance, following rules are applied:
Types of Inheritance… Public Inheritance: When deriving a class from public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class. Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protected members of the derived class. Private Inheritance: When deriving from a private base class, public and protected members of the base class become private members of the derived class.
Example • Class ABC : private XYZ • { • Members of ABC • }; • Class ABC : Protected XYZ • { • Members of ABC • }; • Class ABC : public XYZ • { • Members of ABC • };
Abstract Classes An abstract class implements an abstract concept Main purpose is to be inherited by other classes Can’t be instantiated Promotes reuse
Example – Abstract Classes Person name age gender eat walk Student Doctor Teacher Here, Person is an abstract class
Example – Abstract Classes Vehicle color model accelerate applyBrakes Truck Car Bus Here, Vehicle is an abstract class
Concrete Classes A concrete class implements a concrete concept Main purpose is to be instantiated Provides implementation details specific to the domain context
Example – Concrete Classes Person Student Doctor program studyYear Teacher study heldExam Here, Student, Teacher and Doctor are concrete classes
Example – Concrete Classes Vehicle Truck Car Bus capacity load unload • Here, Car, Bus and Truck are concrete classes