Download presentation
Presentation is loading. Please wait.
Published byRoderick Godfrey Morgan Modified over 9 years ago
1
Inheritance Inheritance is the capability of one class of things to inherit the capabilities or properties from another class. Consider the example of cars. The class ‘Car’ inherits some of its properties from the class ‘Automobiles’ which inherits some of its properties from another class ‘Vehicles’.
2
VEHICLES CAR AUTOMOBILES (MOTOR DRIVEN) PULLED VEHICLES BUSRIKSHAW CART
3
Why Inheritance? Capability to express the inheritance relationship which makes it ensure the closeness with the real- world models.Capability to express the inheritance relationship which makes it ensure the closeness with the real- world models. Reusability of code. Inheritance allows the addition of additional features to an existing class without modifying it.Reusability of code. Inheritance allows the addition of additional features to an existing class without modifying it. It is transitive in nature. For example, if a new class ‘GraduateStudent’ has been declared as a sub-class of ‘Student’ which itself is a sub-class of ‘Person’ then ‘GraduateStudent’ must also be a ‘Person’ i.e. inheritance is transitive.It is transitive in nature. For example, if a new class ‘GraduateStudent’ has been declared as a sub-class of ‘Student’ which itself is a sub-class of ‘Person’ then ‘GraduateStudent’ must also be a ‘Person’ i.e. inheritance is transitive. Note : A subclass defines only those features that are unique to it.
4
Different Forms of Inheritance Single Inheritance : When a subclass inherits only from one base class, it is known as single inheritance. For Example :Single Inheritance : When a subclass inherits only from one base class, it is known as single inheritance. For Example : X Y Base Class Sub Class
5
Multiple Inheritance : When a sub class inherits from multiple base classes, it is known as multiple inheritance.Multiple Inheritance : When a sub class inherits from multiple base classes, it is known as multiple inheritance. Hierarchical Inheritance : When many sub classes inherits from a single base class, it is known as hierarchical inheritance.Hierarchical Inheritance : When many sub classes inherits from a single base class, it is known as hierarchical inheritance. Multilevel Inheritance : The transitive nature of inheritance is reflected by this form of inheritance. When a subclass inherits from a class that itself inherits from another class, it is known as multilevel inheritance.Multilevel Inheritance : The transitive nature of inheritance is reflected by this form of inheritance. When a subclass inherits from a class that itself inherits from another class, it is known as multilevel inheritance. Hybrid Inheritance : combines two or more forms of inheritance. When a sub class inherits from multiple base classes and all of its base classes inherit from a single base class, this form of inheritance is known as hybrid inheritance.Hybrid Inheritance : combines two or more forms of inheritance. When a sub class inherits from multiple base classes and all of its base classes inherit from a single base class, this form of inheritance is known as hybrid inheritance.
6
Single Inheritance class derived-class-name : visibility mode base-class nameclass derived-class-name : visibility mode base-class name { ://members of derived class://members of derived class };}; The visibility mode controls the visibility and availability of inherited base class members in the derived class. e.g. Class Sub : public Super //public derivation { ://members of Sub (the derived class) }; Note : If no mode specified, private is the default mode.
7
Multiple Inheritance e.g. class Sub : public Super!, private SuperB { ://members of Sub class };
8
Visibility Modes Visibility Mode Inheritable public member becomes (in derived class) Inheritable protected member becomes (in derived class) Private member of base class are not directly accessible to derived class. publicpublicprotected protectedprotectedprotected privateprivateprivate
9
Accessibility of Base Class Members Access Specifier Accessible from own class Accessible from derived class (inheritable) Accessible from objects outside class publicYesYesYes protectedYesYesNo privateYesNono
10
Significance of Visibility Modes Derive publicly when the situation wants to derived class to have all the attributes of the base class, plus some extra attributes.Derive publicly when the situation wants to derived class to have all the attributes of the base class, plus some extra attributes. Derive privately when the derived class requires to use some attributes of the base class and these inherited features are intended not to be inherited further.Derive privately when the derived class requires to use some attributes of the base class and these inherited features are intended not to be inherited further. Derive protectedly when the features are required to be hidden from the outside world and at the same time required to be inheritabe.Derive protectedly when the features are required to be hidden from the outside world and at the same time required to be inheritabe.
11
Inheritance and Constructors & Destructors When an object of a derived class is created, first the base class constructor is invoked, followed by the derived class constructors. When an object of a derived class expires, first the derived class destructor is invoked, followed by the base class destructor.When an object of a derived class is created, first the base class constructor is invoked, followed by the derived class constructors. When an object of a derived class expires, first the derived class destructor is invoked, followed by the base class destructor. Passing Arguments through to a Base Class Constructor :Passing Arguments through to a Base Class Constructor : Derived::Derived(type1 x,type2 y…): Base(x,y)Derived::Derived(type1 x,type2 y…): Base(x,y) { : }
12
Example class Base {class Base { int a;int a; float b;float b; public:public: Base(int I,float j) { a = I; b = j; } :}; class Derived : public Base { public : Derived( int p, float q) : Base (p, q) { } };
13
Inheritance And Access Control Access control in Publicly Derived ClassAccess control in Publicly Derived Class Access control in Privately Derived ClassAccess control in Privately Derived Class Access control in Protectedly Derived ClassAccess control in Protectedly Derived Class Note : A derived class inherits all the members of the base class; however, the derived class has the direct access privilege only to the non-private members of the base class.Note : A derived class inherits all the members of the base class; however, the derived class has the direct access privilege only to the non-private members of the base class. Size of class is 1 byte if it do not have any data member in it.Size of class is 1 byte if it do not have any data member in it. The private members of the base class are visible in the derived class but they are not directly accessible.The private members of the base class are visible in the derived class but they are not directly accessible.
14
Making a private member inheritable By making the visibility mode of the private member as public.By making the visibility mode of the private member as public. By making the visibility mode of the private member as protected.By making the visibility mode of the private member as protected.
15
Constructors in Multiple Inheritance In multiple inheritance the base classes are constructed in the order in which they appear in the declaration of the derived classes.In multiple inheritance the base classes are constructed in the order in which they appear in the declaration of the derived classes. General form :General form : Derived-constructor(arg_list) : base1 (arg_list1),Derived-constructor(arg_list) : base1 (arg_list1), Base2 (arg_list2),Base2 (arg_list2), Base3 (arg_list3),Base3 (arg_list3), :a:a Base4(arg_listN)Base4(arg_listN)
16
Virtual Base Class When two or more objects are derived from a common base class, you can prevent multiple copies of base class from being present in an object derived from those objects by declaring the base class as virtual when it is inherited. This is accomplished by putting the keyword virtual before the base class’ name when it is inherited.When two or more objects are derived from a common base class, you can prevent multiple copies of base class from being present in an object derived from those objects by declaring the base class as virtual when it is inherited. This is accomplished by putting the keyword virtual before the base class’ name when it is inherited. Relationship between classes : When a class inherits from another class, the derived class has is-a relationship with its base class. When a class contains object of another class-type, then containing/enclosing class has has-a relationship with contained/enclosed class.Relationship between classes : When a class inherits from another class, the derived class has is-a relationship with its base class. When a class contains object of another class-type, then containing/enclosing class has has-a relationship with contained/enclosed class.
17
Nesting of Classes When a class contains objects of other class types as its member, it is referred to as Containership or containment or Aggregation.When a class contains objects of other class types as its member, it is referred to as Containership or containment or Aggregation. Example : class X { : }; class Y { : }; class Z { X ob1; Y ob2; };
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.