Object Oriented
Programming using C++
Inheritance
INHERITANCE
Inheritance
Inheritance is a phenomenon of acquiring
properties from predecessor.
In object oriented systems, it means
acquiring the properties of base class to the
derived class.
Hence the derived class gets specialized with
the properties of base class as well as its own
properties.
It involves the concept of code reusability.
Inheritance
Inheritance is the process by which new classes
called derived classes are created from existing
classes called base classes.
The derived classes have all the features of the
base class and the programmer can choose to
add new features specific to the newly created
derived class.
The idea of inheritance implements the is a
relationship. For example, mammal IS-A animal,
dog IS-A mammal hence dog IS-A animal as well
and so on.
Types of inheritance:
single level
multi level
Base Class A
Derived Class B
Class A
Intermediate
derived class B
Derived Class C
Types of inheritance:
Multiple inheritance
Hierarchical inheritance.
Base class A Base class B
Derived Class
C
Class A
Derived class B
Derived
class C
Derived class
D
Types of inheritance:
Hybrid inheritance
Class A
Intermediate
Derived class B
Intermediate
Derived class C
Derived class D
Class A
Class B
Class C
Class D
FEATURES /ADVANTAGES OF
INHERITANCE
Reusability of Code
Saves Time and Effort
Faster development, easier maintenance and
easy to extend
Capable of expressing the inheritance
relationship and its transitive nature which
ensures closeness with real world problems
Syntax for inheritance:
class Base-class-name
{
___ //
___ // definition of the class
};
class derived-class name : visibility-mode
base-class-name
{
….. //
….. // members of derived class
};
Visibility Mode
Here visibility mode can be public, private
or protected.
Private is the default mode. They are visible
to member functions within its class.
Protected visible to member functions of
its own class and derived class.
Public visible to all functions in the
program.
When a class is inherited in any
of these visibility modes, the
visibility of its members varies.
The visibility of inherited members is
as shown:
Single level
In this kind of inheritance, there is a single
base class and a single derived class.
Eg: class base
{
// class declaration
};
class derived : public base
{
//derive class declaration
};
Here, base class base is inherited into the derived class derived.
Multi level Inheritance
In this type of inheritance
there is more than one
levels of inheritance and
each derived class serves as
the base class for the other
derived class.
Eg. class base1
{
//base1 declaration
};
class base2: public base1
{
//base2 declaration
};
class derived : public base2
{
//derived class
declaration
};
Here, base1 is a base class that
is inherited into the derived
class base2, which in turn is
inherited into the derived
class derived.
Question to implement
A class A{
char name[] …….
int roll_no; ……
+void get();
+void show(); };
B class B: public A
float m1,m2,m3; {
+void take(); …..
+void put(); };
C
class C: public B
float percentage; {
+void cal_per(); …..
+void disp_result(); };
Multiple Inheritance –
In this kind of Inheritance, there are many base classes and
one derived class from these base classes.
Eg. Class base1{};
class base2{};
Class base 3{};
class derived : public base1, protected base2, public base3
{
// class declaration
};
Here, base1, base2 and base3 are base classes that are
inherited into the derived class derived.
Hierarchical Inheritance
In this type of Inheritance, there
is one base class and many
derived classes from this base
class
Eg. class base
{
// class declaration
};
class der1 : public base
{
// class declaration
};
class der2 : public base
{
// class declaration
};
Here, der1 and der2 are derived
classes that are derived from
the base class base.
Hybrid Inheritance
This is a combination of one or more
than one type of Inheritance, like a
combination of multiple and multilevel
inheritance.
Virtual Base Classes
In hybrid Inheritance, that is a combination of
multiple and hierarchical inheritance, there may
arise a case when the features of ancestor base
class are inherited via multiple paths in to the
ultimate derived class, resulting in deriving
duplicate set of members.
This duplication of inherited members can be
avoided in C++ by inheriting the ancestor base
class as virtual base class in to any derived class.
Hybrid inheritance
Class A
Intermediate
Derived class B1
Intermediate
Derived class B2
Derived class C
Class A
{
// (ancestor) class declaration
};
Class B1: virtual public A
{
//(intermediate derived) class
declaration
};
Class B2: virtual public A
{
//(intermediate derived) class
declaration
};
Class C: public B1, public B2
{// only one copy of the
members of class A is inherited
};
Syntax:
Class A
{
// (ancestor) class declaration
};
Class B1: virtual public A
{
//(intermediate derived) class declaration
};
Class B2: virtual public A
{
//(intermediate derived) class declaration
};
Class C: public B1, public B2
{// only one copy of the members of class A is inherited
};
Constructors in derived class:
• Whenever we have parameterized
constructors in Base class, it becomes
necessary to define a parameterized
constructor in derived class so that on the
creation of objects of derived class, we can
pass the parameters to the base constructors
through the derived constructor.
The format for the derived class constructor in this case
is a bit different as shown below:
class derived: <visibility mode> <base class name>
{
int A, B; //declaration of data members of class Derived
Public:
derived( arg 1, arg 2, arg 3…. arg N): base 1( arg a,arg b,,…,arg
d), base 2( arg e, arg f,… arg k),…base N( arg l, arg m…,arg p)
{
A= arg 1;
B= arg 2;
}
};
Abstract Classes
• Is one that is used to create objects.
• An abstract class is designed only to act as a
base class(to be inherited by other classes).

Inheritance

  • 1.
  • 2.
  • 3.
    Inheritance Inheritance is aphenomenon of acquiring properties from predecessor. In object oriented systems, it means acquiring the properties of base class to the derived class. Hence the derived class gets specialized with the properties of base class as well as its own properties. It involves the concept of code reusability.
  • 4.
    Inheritance Inheritance is theprocess by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class. The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.
  • 5.
    Types of inheritance: singlelevel multi level Base Class A Derived Class B Class A Intermediate derived class B Derived Class C
  • 6.
    Types of inheritance: Multipleinheritance Hierarchical inheritance. Base class A Base class B Derived Class C Class A Derived class B Derived class C Derived class D
  • 7.
    Types of inheritance: Hybridinheritance Class A Intermediate Derived class B Intermediate Derived class C Derived class D Class A Class B Class C Class D
  • 8.
    FEATURES /ADVANTAGES OF INHERITANCE Reusabilityof Code Saves Time and Effort Faster development, easier maintenance and easy to extend Capable of expressing the inheritance relationship and its transitive nature which ensures closeness with real world problems
  • 9.
    Syntax for inheritance: classBase-class-name { ___ // ___ // definition of the class }; class derived-class name : visibility-mode base-class-name { ….. // ….. // members of derived class };
  • 10.
    Visibility Mode Here visibilitymode can be public, private or protected. Private is the default mode. They are visible to member functions within its class. Protected visible to member functions of its own class and derived class. Public visible to all functions in the program.
  • 11.
    When a classis inherited in any of these visibility modes, the visibility of its members varies.
  • 12.
    The visibility ofinherited members is as shown:
  • 13.
    Single level In thiskind of inheritance, there is a single base class and a single derived class. Eg: class base { // class declaration }; class derived : public base { //derive class declaration }; Here, base class base is inherited into the derived class derived.
  • 14.
    Multi level Inheritance Inthis type of inheritance there is more than one levels of inheritance and each derived class serves as the base class for the other derived class. Eg. class base1 { //base1 declaration }; class base2: public base1 { //base2 declaration }; class derived : public base2 { //derived class declaration }; Here, base1 is a base class that is inherited into the derived class base2, which in turn is inherited into the derived class derived.
  • 15.
    Question to implement Aclass A{ char name[] ……. int roll_no; …… +void get(); +void show(); }; B class B: public A float m1,m2,m3; { +void take(); ….. +void put(); }; C class C: public B float percentage; { +void cal_per(); ….. +void disp_result(); };
  • 16.
    Multiple Inheritance – Inthis kind of Inheritance, there are many base classes and one derived class from these base classes. Eg. Class base1{}; class base2{}; Class base 3{}; class derived : public base1, protected base2, public base3 { // class declaration }; Here, base1, base2 and base3 are base classes that are inherited into the derived class derived.
  • 17.
    Hierarchical Inheritance In thistype of Inheritance, there is one base class and many derived classes from this base class Eg. class base { // class declaration }; class der1 : public base { // class declaration }; class der2 : public base { // class declaration }; Here, der1 and der2 are derived classes that are derived from the base class base.
  • 18.
    Hybrid Inheritance This isa combination of one or more than one type of Inheritance, like a combination of multiple and multilevel inheritance.
  • 19.
    Virtual Base Classes Inhybrid Inheritance, that is a combination of multiple and hierarchical inheritance, there may arise a case when the features of ancestor base class are inherited via multiple paths in to the ultimate derived class, resulting in deriving duplicate set of members. This duplication of inherited members can be avoided in C++ by inheriting the ancestor base class as virtual base class in to any derived class.
  • 20.
    Hybrid inheritance Class A Intermediate Derivedclass B1 Intermediate Derived class B2 Derived class C Class A { // (ancestor) class declaration }; Class B1: virtual public A { //(intermediate derived) class declaration }; Class B2: virtual public A { //(intermediate derived) class declaration }; Class C: public B1, public B2 {// only one copy of the members of class A is inherited };
  • 21.
    Syntax: Class A { // (ancestor)class declaration }; Class B1: virtual public A { //(intermediate derived) class declaration }; Class B2: virtual public A { //(intermediate derived) class declaration }; Class C: public B1, public B2 {// only one copy of the members of class A is inherited };
  • 22.
    Constructors in derivedclass: • Whenever we have parameterized constructors in Base class, it becomes necessary to define a parameterized constructor in derived class so that on the creation of objects of derived class, we can pass the parameters to the base constructors through the derived constructor.
  • 23.
    The format forthe derived class constructor in this case is a bit different as shown below: class derived: <visibility mode> <base class name> { int A, B; //declaration of data members of class Derived Public: derived( arg 1, arg 2, arg 3…. arg N): base 1( arg a,arg b,,…,arg d), base 2( arg e, arg f,… arg k),…base N( arg l, arg m…,arg p) { A= arg 1; B= arg 2; } };
  • 24.
    Abstract Classes • Isone that is used to create objects. • An abstract class is designed only to act as a base class(to be inherited by other classes).