Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance Dr. Bhargavi Goswami Department of Computer Science

Similar presentations


Presentation on theme: "Inheritance Dr. Bhargavi Goswami Department of Computer Science"— Presentation transcript:

1 Inheritance Dr. Bhargavi Goswami Department of Computer Science
Christ University Bangalore.

2 Inheritance Purpose: Reusability of code.
Save time and money of coding. Increase reliability Reduce frustration of error solving and testing. Inheritance: reusing the properties of existing ones and deriving new class from old one. Old class is called base class New class is called derived class or subclass. Inheritance provide derived class to inherit some or all traits of base class using access specifiers.

3 Inheritance access specifier

4 Types of inheritance

5 Defining derived classes

6 Single inheritance: public
Suppose D is derived class from class B. D inherits all the public members of B as public. But, D can access B’s private members using public functions of B in main. Eg singleInhertancePublic .cpp

7 Single inheritance: private
Suppose D is derived class from class B. D inherits all the public members of B as public and private members are private. To access functions of B, D’s functions should have call statements of B’s functions. Eg singleInhertancePrivat e.cpp

8 WHAT IS PRIVATE, PROTECTED AND PUBLIC
class alpha { private: //optional, by default private ---- //visible to member functions ---- //within its class protected: //visible to member functions ---- //of its own and that of ---- //derived class public: //visible to all functions ---- // in the program ---- };

9 WHAT IS PRIVATE, PROTECTED AND PUBLIC
A public member is accessible from anywhere outside the class but within a program. You can set and get the value of public variables without any member. A private member variable or function cannot be accessed, or even viewed from outside the class. Only the class and friend functions can access private members. A protected member variable or function is very similar to a private member but, it provided one additional benefit that they can be accessed in child classes which are called derived classes.

10 Private v/s protected BASIS FOR COMPARISON PRIVATE PROTECTED
Inheriting property to the derived class Derived class cannot access base class private members. Derived class can access base class protected members. Accessibility The private members of the class are inaccessible out of the class scope. The protected members of the class are inaccessible out of the class scope except the class derived immediately. Accessible from own class Yes Accessible from derived class No Accessible from outside Flexibility Low High

11 PRIVATE, PROTECTED AND PUBLIC: Conclusion in short
private: base protected: base + derived public: base + derived + any other member Access specifier Class Subclass world Private Yes No Protected Public

12 Making private function inheritable

13 Inheritance access specifier

14 Never to forget

15 Friend and inheritance

16 Access control and inheritance

17 Multilevel inheritance
Here, A is base class, B is intermediate base class and C is derived class. See example of student, test and result class. Eg multilevelInheritance.cpp

18 Multiple inheritance Multiple Inheritance: A class that can inherit the attributes of two or more classes. See below syntax. Eg. 8-4-multipleInheritance Limitations: ambiguity in handling same named function inherited from multiple base classes.

19 Situation: Same function name in base and derived class
Can base class and derived class have same function names? Yes. Then what will happen if derived class obj invokes that common named function? Ans. Derived class function will supersedes base class definition. (obviously we don’t use things of others if we already have it) Then when the base class function definition will be executed? In case the derived class function is not redefined, base class function will be called.

20 Resolution of ambiguity in multiple inheritance
Suppose there are two classes N and M as base class and class P as derived class with multiple inheritance. If there is a common function called display() in class N and M, which one will be called by object of P? Answer is in next program. Eg. AmbiguityResolutionMultiple.cpp

21 HIERARCHICAL INHERITANCE

22 HIERARCHICAL INHERITANCE

23 HYBRID INHERITANCE There can be a situation where we need to apply multiple types of inheritance. Suppose sports credit is considered in final result. How to implement it? Ans: using hybrid inheritance. Eg. 8-5-hybridInheritance.cpp

24 Virtual base class Consider a situation where all three kind of inheritance are involved. i.e Multiple, Multi- level, Hierarchical. Grandparents Parent 1 Parent 2 Child

25 Virtual base class Now, child inherits grand parents via three separate paths. Means, child will have duplicate copy of grandparents. Does this introduce ambiguity? (Yes/No) Yes. Needs to be avoided? Yes. But how? By declaring common base class as virtual. Lets see how.

26 Virtual base class class A {}; class B1 : virtual public A {};
class B2 : public virtual A {}; class C : public B1, public B2 {}; Virtual and Public can be use in any order. See example program 8.6, virtual base class. A B1 B2 C

27 Abstract class Abstract class is designed only to act as base class inherited by other classes. No direct object is created for abstract class. Designed for? Programming concepts, projects. Has great significance in logical program design and real programming scenario. It holds common characteristics of all derived classes. It must have one pure virtual function. What is pure virtual function? A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration.

28 Abstract class If we do not override the pure virtual function in derived class, then derived class also becomes abstract class. An abstract class can have constructors. A class is abstract if it has at least one pure virtual function We can have pointers and references of abstract class type. Eg. AbstractClassUsingPureVirtualFun.cpp

29 Abstract class vehicle Class Data-type-d1 Private Member variable
virtual void spec() = 0; Pure virtual function LMV Class void spec() Public function HMV Class void spec() Public function TW Class void spec() Public function

30 Virtual v/s abstract The methods in virtual class CAN be overridden in derived classes, while abstract class methods MUST be overridden. Particulars Virtual Abstract Methods Methods may be overridden by derived class. All methods must be overridden by derived class. Instance Can be instantiated. Cannot be instantiated. Patent Class Generally implemented as solution to multiple inheritance. Partial implementation of concept in parent and partial in derived.

31 Constructors and inheritance
If base class has unparameterized constructor, to access that, any action is required in derived class? If base class has parameterized constructor, what action is required in derived class? Answer: For unparameterized constructor in base class, no action is required. But, mandatory action is required for parameterized constructor. Means? Parameters has to be passed from derived class to base class constructors. What if derived and base both class has constructor? What order they execute? Ans: First Base Class Constructors and then Derived Class Constructors.

32 Constructors and inheritance
But, what if it is multiple inheritance? The order will be in accordance with declaration statement. And what if it is multi level inheritance? Constructor will be according to order of levels. Who is responsible for providing initial values to base class constructors? Derived class. How do they do that? Supply all the required initial values when object is declared. How the arguments are passed to base class? Order of arguments are followed. And if one is virtual base class and one is ordinary? Higher priority is given to virtual base class. Confused? Don’t worry. Lets see two example to clarify. Two Methods: a) With colon, b) Initialization list.

33 Alternate of inheritance: Initialization list
Inheritance is the only way to derive properties of base class? Is there any alternate of inheritance? (Yes/No) Ans: Yes. How? Objects can be a collection of many other objects. Means? Class can contain objects of other classes as its members. Eg: class alpha{……}; class beta{……}; class gamma { alpha a; //object of class alpha beta b; //object of class beta …… }; We call it containership of nesting. For this we need initialization list to provide values to all the members of base classes. See next slide for example.

34 Alternate of inheritance
For the same example we inherit properties without inheritance. Note: Y we do not provide datatype to argument list? Because it is function call and not direct access to members. Eg. 8-7-ConstructorsInDerivedClass.cpp Eg. 8-8-InitializationListInConstructors.cpp stx: class gamma { ..... alpha a; beta b; public: gamma (arglist) : a(arglist1), b(arglist2) //constructor body } }; eg: gamma(int x, int y, float z) : a(x), b(x,z) { //write action or print stmt }

35 Chapter Over. Thank you. See u soon.


Download ppt "Inheritance Dr. Bhargavi Goswami Department of Computer Science"

Similar presentations


Ads by Google