0% found this document useful (0 votes)
7 views8 pages

Lecture 15 To 19

Inheritance is a key concept in object-oriented programming that allows a new class (derived class) to inherit properties and methods from an existing class (base class), promoting code reuse and easier maintenance. There are various forms of inheritance, including single, multiple, hierarchical, multilevel, and hybrid inheritance, each defining different relationships between classes. Access specifiers (public, protected, private) determine the visibility of base class members in derived classes, and constructors of base classes are automatically called when creating derived class objects.

Uploaded by

Saleem Bhatti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Lecture 15 To 19

Inheritance is a key concept in object-oriented programming that allows a new class (derived class) to inherit properties and methods from an existing class (base class), promoting code reuse and easier maintenance. There are various forms of inheritance, including single, multiple, hierarchical, multilevel, and hybrid inheritance, each defining different relationships between classes. Access specifiers (public, protected, private) determine the visibility of base class members in derived classes, and constructors of base classes are automatically called when creating derived class objects.

Uploaded by

Saleem Bhatti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Inheritance

One of the most important concepts in object-oriented programming is that of inheritance.


Inheritance allows us to define a class in terms of another class, which makes it easier to create
and maintain an application. This also provides an opportunity to reuse the code functionality
and fast implementation time.

When creating a class, instead of writing completely new data members and member functions,
the programmer can designate that the new class should inherit the members of an existing class.
This existing class is called the base class, and the new class is referred to as the derived class.
The classes that has is a relationship between them are candidates of inheritance. For example,
mammal IS-A animal [mammal should be base, animal should derive from mammal], dog IS-A
mammal hence dog IS-A animal as well and so on.
A derived class inherits all base class methods with the following exceptions:

 Private data members and member functions of base class


 Constructors, destructors and copy constructors of the base class.
 Overloaded operators of the base class.
 The friend functions of the base class.

Forms of Inheritance
Single Inheritance: It is the inheritance hierarchy wherein one derived class inherits from one
base class.
Multiple Inheritance: It is the inheritance hierarchy wherein one derived class inherits from
multiple base class(es)
Hierarchical Inheritance: It is the inheritance hierarchy wherein multiple subclasses inherit
from one base class.
Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class for
other classes.
Hybrid Inheritance: The inheritance hierarchy that reflects any legal combination of other four
types of inheritance.

In order to derive a class from another, we use a colon (:) in the declaration of the derived class
using the following format :
classderived_class: memberAccessSpecifierbase_class
{
...
};
Following example further explains concept of inheritance :

class Shape
{
protected:
float width, height;
public:
voidset_data (float a, float b)
{
width = a;
height = b;
}
};
class Rectangle: public Shape
{
public:
float area ()
{
return (width * height);
}
};

class Triangle: public Shape


{
public:
float area ()
{
return (width * height / 2);
}
};

int main ()
{
Rectangle rect;
Triangle tri;
rect.set_data (5,3);
tri.set_data (2,5);
cout<<rect.area() <<endl;
cout<<tri.area() <<endl;
return 0;
}

output :
15
5
The object of the class Rectangle contains :
width, height inherited from Shape becomes the protected member of Rectangle.
set_data() inherited from Shape becomes the public member of Rectangle
area is Rectangle’s own public member.

The object of the class Triangle contains :


width, height inherited from Shape becomes the protected member of Triangle.
set_data() inherited from Shape becomes the public member of Triangle
area is Triangle’s own public member
set_data () and area() are public members of derived class and can be accessed from outside class
i.e. from main()
Type of Inheritance:
When deriving a class from a base class, the base class may be inherited through public,
protected orprivate inheritance. The type of inheritance is specified by the access-specifier as
explained above.
We hardly use protected or private inheritance, but public inheritance is commonly used.
While using different type of inheritance, following rules are applied:
 Public Inheritance: When deriving a class from a 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 publicand protected members of the base class.
 Protected Inheritance: When deriving from a protected base
class, public and protectedmembers 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.

Access specifiers in the base class


Private protected public
The member is
private inheritance The member is private. The member is private.
inaccessible.
protected The member is The member is The member is
inheritance inaccessible. protected. protected.
The member is The member is
public inheritance The member is public.
inaccessible. protected.

class A
{
public:
int x;
protected:
int y;
private:
int z;
};

class B :public A
{
// x is public
// y is protected
// z is not accessible from B
};
class C :protected A
{
// x is protected
// y is protected
// z is not accessible from C
};

class D :private A
{
// x is private
// y is private
// z is not accessible from D
};

Constructor and inheritance


The compiler automatically call a base class constructor before executing the derived class
constructor. The compiler’s default action is to call the default
constructor in the base class. If you want to specify which of several base class constructors
should be called during the creation of a derived class object.
In these cases, you must explicitly specify which base class constructor should be called by the
compiler. This is done by specifying the arguments to the selected base class constructor in the
definition of the derived class constructor.

class Rectangle
{
private :
float length;
float width;
public:
Rectangle ()
{
length = 0;
width = 0;
}

Rectangle (float len, float wid)


{
length = len;
width = wid;
}

float area()
{
return length * width ;
}
};

class Box : public Rectangle


{
private :
float height;
public:
Box ()
{
height = 0;
}

Box (float len, float wid, float ht) : Rectangle(len, wid)


{
height = ht;
}

float volume()
{
return area() * height;
}
};

int main ()
{
Box bx;
Box cx(4,8,5);
cout<<bx.volume() <<endl;
cout<<cx.volume() <<endl;
return 0;
}

output :
0
160

Overriding Base Class Functions

A derived class can override a member function of its base class by defining a derived class
member function with the same name and parameter list.
It is often useful for a derived class to define its own version of a member function inherited
from its base class. This may be done to specialize the member function to the needs of the
derived class. When this happens, the base class member function is said to be overridden by the
derived class.

class mother
{
public:
void display ()
{
cout<< "mother: display function\n";
}
};

class daughter : public mother


{
public:
void display ()
{
cout<< "daughter: display function\n\n";
}
};

int main ()
{
daughterrita;
rita.display();
return 0;
}
output:
daughter: display function
Gaining Access to an Overridden Function

It is occasionally useful to be able to call the overridden version. This is done by using the scope
resolution operator to specify the class of the overridden member function being accessed.

class daughter : public mother


{
public:
void display ()
{
cout<< "daughter: display function\n\n";
mother::display();
}
};

output:
daughter: display function
mother: display function

You might also like