Presentation is loading. Please wait.

Presentation is loading. Please wait.

INHERITANCE : Extending Classes. Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of.

Similar presentations


Presentation on theme: "INHERITANCE : Extending Classes. Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of."— Presentation transcript:

1 INHERITANCE : Extending Classes

2 Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of things to acquire properties or capabilities from another class of things. Inheritance is the capability of one class of things to acquire properties or capabilities from another class of things. For example:- For example:- Vehicles Automobiles

3 Conceptual Examples of Hierarchical Classes Animal PersonReptileBird ManWoman MotherFather Person Engineer Student UGGrad Lawyer

4 Class Inheritance in C++ Inheritance in c++ allows us to create new classes which are derived from older Inheritance in c++ allows us to create new classes which are derived from older classes. A class is a group of objects classes. A class is a group of objects that share common properties & that share common properties & relationship. relationship. The derived classes are The derived classes are called subclasses or simply derived classes The older classes are superclasses The older classes are superclasses or parent classes or base classes A derived class automatically includes some of its parent's members, and can have additional ones. A derived class automatically includes some of its parent's members, and can have additional ones. base subclass1subclass2 Notation:

5 Staff Need for inheritance 1.Capability to express the inheritance relationship which makes it ensure the closeness with the real world models. For example: when we want to maintain a database of an organisation, the database is divided into number of classes whose relationships are as follows empcode name basic Clerk grade Officer designation

6 Need for inheritance 2.It gives the idea of re-usability. It allows the addition of additional features to an existing class without modifying it. One can desire a new class (sub-class) from an existing one (super-class) and add new features to a sub- class. Student sturoll stuname stuaddress Graduatestu stuyear

7 B Need for inheritance 3.It has transitive nature. If a class A inherits properties of another class B, then all subclasses of class A will automatically inherits properties of class B. A CED

8 Advantages of inheritance 1. Increase reuse and software quality  Programmers reuse the base classes instead of writing new classes.  Using well-tested base classes helps reduce bugs in applications that use them.  Reduce object code size. 2. Enhance extensibility

9 Types of inheritance Inheritance may take place in many forms, which are as follows:- 1. Single Inheritance 2. Multiple Inheritance 3. Hierarchical Inheritance 4. Multilevel Inheritance 5. Hybrid Inheritance

10 1.Single Inheritance  When a sub-class is inherited only from one base-class. e.g. base class derived class derived class X Y

11 2.Multiple inheritance When a sub class is inherited from multiple base classes. When a sub class is inherited from multiple base classes. e.g.Base classes derived class There are ‘x’ and ‘y’ are super classes and sub class ‘z’ inherits the properties from both super classes. x z y

12 3. Hierarchical Inheritance When many sub-classes are inherited from a single super-class, is known as hierarchical inheritance. When many sub-classes are inherited from a single super-class, is known as hierarchical inheritance. base class base class derived-classes derived-classes A DCB

13 4.Multilevel Inheritance When a sub-class is inherited from a class that itself is inherited from another class, is known as multilevel inheritance. When a sub-class is inherited from a class that itself is inherited from another class, is known as multilevel inheritance. Base class of Y Base class of Y sub class of X sub class of X Base class of Z Sub class of Y Sub class of Y x y z

14 5.Hybrid Inheritance When a sub-class is inherited from multiple base-classes and all of its base-classes are inherited from a single base- class, is called hybrid inheritance. When a sub-class is inherited from multiple base-classes and all of its base-classes are inherited from a single base- class, is called hybrid inheritance. e.g. e.g. Base class of Y Base class of Y sub class of X sub class of X Base class of Z Sub class of Y and X Sub class of Y and X w x y z

15 Access modes or visibility modes of inheritance Access modes or visibility modes of inheritance We have seen two access modes in C++ classes: public and private  Public members are directly accessible by users of the class  Private members are NOT directly accessible by users of the class, not even by inheritors There is a 3 rd access mode: protected Protected members are directly accessible by derived classes but not by other users Protected members are directly accessible by derived classes but not by other users

16 Syntax for Inheritance: Single Inheritance class derivedClass_name : visibility mode baseClass_name { private : public: protected: }; The derived class inherits from the base class: all public members, all protected members (see later), and the default constructor The additional members defined can have the same name (and type) as those of the base class (as when some base members are to be redefined)

17 For Example class Sub : public Super {private:public:protected:};

18 Syntax for Inheritance : Multiple Inheritance class derivedClass_name : visibility mode baseClass_name1, visibility mode baseClass_name2, visibility mode baseClass_name3 {private : public: protected: };

19 For Example class Sub : public SuperA, private SuperB {private:public:protected:};

20 Access or Visibility mode of inheritance The access mode or visibility mode of derived class specifies whether the features of the base class are privately, publicly, protected derived. The access mode or visibility mode of derived class specifies whether the features of the base class are privately, publicly, protected derived. The visibility modes basically control the access-specifier of inheritable members of base class, in the derived class. The visibility modes basically control the access-specifier of inheritable members of base class, in the derived class.

21 Access or Visibility mode of inheritance Visibility of inherited base class members in derived class:- Visibility of inherited base class members in derived class:- Visibility mode is In derived class PUBLIC MEMBERS OF BASE CLASS become In derived class PROTECTED MEMBERS OF BASE CLASS become Private Members of base class are not directly accessible to derived class. PublicProtectedprivatePublicProtectedprivateProtectedProtectedprivate

22 The Public visibility mode It means that the derived class can access the public and protected members of the base class but not the private members. With publicly derived class, the public members of the base class become the public members of the derived class and the protected members of the base class become the protected members of the derived class.

23 The Public visibility mode Class SuperClass Sub Private section xCheck() Public section Protected section Public section Private section c y a z y z breadit() display() init() getval() display() writeit() getval() Inherited from base class

24 The Private visibility mode It means that the derived class can access the public and protected members of the base class privately. With privately derived class, the public members and the protected members of the base class become private members of the derived class.

25 The Private visibility mode Class SuperClass Sub Private section xCheck() Public section Protected section Public section c z y breadit() getval() display() writeit() yDisplay() zgetval() a Init() Private section Inherited from base class

26 The Protected visibility mode It means that the derived class can access the public and protected members of the base class privately. With protectedly derived class, the public members and the protected members of the base class become protected members of the derived class.

27 The Protected visibility mode Class Super Class Super Class Sub Class Sub Private section x Check() Public section ydisplay() Protected section z Check() Getval() Private section aInit() Public section breadit() Protected section y z display() Getval() cWriteit() Inhetrited from base class

28 Inheritance and Base Class While defining a base class following things should be kept in mind- While defining a base class following things should be kept in mind- 1. Members intended to be inherited and at the same time intended to be available to every function, even to non members, should be declared public members in base class. 2. Members intended to be inherited but not intended to be public, should be declared as protected. 3. Members not intended to be inherited should be declared as private.

29 Accessibility of Base Class Members Access Specifiers Accessible from own class Accessible from derived class Accessible from objects outside class PublicYes ProtectedYes No privateYesNo

30 1.Single Inheritance  When a sub-class is inherited only from one base-class. e.g. base class derived class derived class  Syntax:- class subclass_name : visibility-mode baseclass_name { //members of derived class }; X Y

31 2.Multiple inheritance When a sub class is inherited from multiple base classes. When a sub class is inherited from multiple base classes. e.g.Base classes derived class There are ‘x’ and ‘y’ are super classes and sub class ‘z’ inherits the properties from both super classes.  Syntax:- class subclass : visibility mode superclass1, visibility mode superclass2…… { // members of derived class. }; x z y

32 3. Hierarchical Inheritance When many sub-classes is inherited from a single super-class, is known as hierarchical inheritance. When many sub-classes is inherited from a single super-class, is known as hierarchical inheritance.e.g.Base-classderived-classesSyntax:- class sub_class1: visibility-mode super-class { //members of derived class sub_class1 }; class sub_class2 : visibility-mode super-class { //members of derived class sub_class2 }; A DCB

33 4.Multilevel Inheritance When a sub-class is inherited from a class that itself is inherited from another class, is known as multilevel inheritance. When a sub-class is inherited from a class that itself is inherited from another class, is known as multilevel inheritance. e.g. Base class of Y e.g. Base class of Y sub class of X Base class of Z Sub class of Y  Syntax:- class sub_class1: visibility-mode super_class { //members of derived class of super_class and base class of sub_class2 }; class sub_class2 : visibility-mode sub_class1 { //members of derived class of sub_class1 }; x y z

34 5.Hybrid Inheritance When a sub-class is inherited from multiple base-classes and all of its base-classes are inherited from a single base- class, is called hybrid inheritance. When a sub-class is inherited from multiple base-classes and all of its base-classes are inherited from a single base- class, is called hybrid inheritance. e.g. Base class of Y e.g. Base class of Y sub class of X Base class of Z Sub class of Y  Syntax:- w x y z class super { }; class sub1 : visibility-mode super { } ; class sub_2 : visibility-mode super { }; class sub1 : visibility-mode sub_1, visibility_mode sub_2 { } ;

35 Example of Inherited Classes class Shape { protected: int width, height; public: void setDims (int a, int b){ width=a; height=b;} }; class Rectangle: public Shape { public: int area ( ) { return (width * height); } }; class Triangle: public Shape { public: int area ( ) { return (width * height/2); } }; class Square: public Rectangle { public: void setDims (int a){ width=a; height=a;} };

36 More on Inheritance Syntax class derivedClass : protected baseClass { …}; // Effect: ???? class derivedClass : private baseClass { …}; // Effect: ????

37 Inheritance and Constructors and Destructors When an object of derived class is created, first the base class constructor is called, followed by the derived class constructor. When an object of a derived class expires, first the derived class destructor is invoked, followed by the base class destructor. For example: Class x{ }; }; Class y:public x { }; }; Void main() { y ob1; }; Qn : What will be the order of constructor and destructor invocation?

38 Answer Constructor x Constructor y Destructor y Destructor x

39 Passing Arguments to the Base class constructor Class Base Class Base {int a; {int a; float b; public: Base (int i, float j) {a=I;b=j;}}; Class Derived : public Base {public: Derived ( int p, float q) : Base (p, q) Derived ( int p, float q) : Base (p, q) {}{}{}{}}; Here even derived constructor does not need a parameter for itself, yet it accepts parameters for base constructor and then invokes base class constructor with these parameters.

40 Passing Arguments to the Base class constructor and itself Class Base Class Base {int a; {int a; float b; public: Base (int i, float j) {a=I;b=j;}}; Class Derived : public Base {int x; float y; public: Derived (int I, float j, int p, float q) : Base (p, q) Derived (int I, float j, int p, float q) : Base (p, q) { x=I; y=j; } y=j; }}; Here derived constructor need a parameter for itself and base class also so it accepts parameters for base constructor and itself also.

41 Qn What should we do to make a private member to be inheritable in derived class? Qn What should we do to make a private member to be inheritable in derived class? Ans It can be done by two methods- Ans It can be done by two methods- 1 by making the visibility mode of the private members as public. 1 by making the visibility mode of the private members as public. 2 by making the visibility mode of the private members as protected. 2 by making the visibility mode of the private members as protected.


Download ppt "INHERITANCE : Extending Classes. Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of."

Similar presentations


Ads by Google