0% found this document useful (0 votes)
12 views

Lesson06 Inheritance

Uploaded by

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

Lesson06 Inheritance

Uploaded by

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

6 Lesson 6: Inheritance

Inheritance is a concept which is the result of commonality between classes. Due to this
mechanism, we need not repeat the declaration as well as member functions in a class if they
are already present in another class. For example, consider the classes namely “person” and
“employee”. All information that is present in the class person, will be present in the class
employee also. Apart from that, there will be some extra information in class employee due
the fact that an employee is person with some additional information introduced by the
employment status. With inheritance, it is enough only to indicate that information which is
specific to the employee in its class, and then inherit the common information from the class
person.
In a nutshell, an employee is a person with some additional information.
When we are declaring the classes person and employee without applying the concept of
inheritance, the two classes will look as follows:
class person
{
public:
string name, phone, email;
int age, idno;
};

class employee
{
public:
string name, phone,email;
int age, idno, empno;
float salary;
}

Now, without repeating the entire information of class person in class employee, we can
declare the employee class as follows:
class employee: public person
{
public:
int empno;
float salary;
};

Inheritance is the process by which objects of one class acquires the properties and methods
of another class. The class from which properties and methods are inherited is called base
class and the class to which they are inherited is called derived class. Inheritance can be
broadly classified into:
 Single Inheritance
 Multiple Inheritance

1
A. Irungu
 Multilevel Inheritance
 Hierarchical Inheritance
 Hybrid Inheritance

6.1 Single Inheritance


In a single inheritance the derived class is derived from a single base class.

6.2 Multiple Inheritance


In multiple inheritance derived class is derived from more than one base class.

6.3 Multilevel Inheritance


In multilevel inheritance class B is derived from a class A and a class C is derived from the
class B.

2
A. Irungu
6.4 Hierarchical Inheritance
In hierarchical inheritance several classes can be derived from a single base class

6.5 Hybrid inheritance


It is the mixture of one or more above inheritance.

6.6 Base-Class Access Control


When a class inherits another, the members of the base class become members of the derived
class. Class inheritance uses this general form:
class derived_class_name : access base_class_name
{
// body of class
};
The access status of the base-class members inside the derived class is determined by access.
The base class derivation mode can be either public, private, or protected. If no mode is
present, the access specifier is private by default if the derived class is a class.
In summary;
1. If the derived class is a struct, then public is the default in the absence of an explicit
access specifier.
2. When the public derivation mode is used, all public members of the base class become
public members of the derived class, and all protected members of the base become
protected members of the derived class.
3. When the private derivation mode is used, all public and protected members of the
base class become private members of the derived class.

3
A. Irungu
4. When the protected derivation mode is used, all public and protected members of the
base become protected members of the derived class.
5. In all cases, the private elements of the base class remain private to the base and are
not accessible by members of the derived class.
Example:
class Base {
public:
int x;
protected:
int y;
private:
int z;
};

class DerivedA: public Base {


// x is public
// y is protected
// z is not accessible from DerivedA
};

class DerivedB: protected Base {


// x is protected
// y is protected
// z is not accessible from DerivedB
};

class DerivedC: private Base {


// x is private
// y is private
// z is not accessible from DerivedC
};

6.7 Write programs that implement inheritance

Lab Tutorials and exercises


1. Create a class mammal that is derived from the class animal.
2. Create a class bird that is derived from the class animal
3. Create a class bat that is derived from both mammal and bird class.
*something is not right here…we shall find out later on. – The Diamond Problem

#include <iostream>
using namespace std;
class animal
{
private:
int age;
protected:
string race;
public:
int color;

4
A. Irungu
};
class bird: public animal
{

};
class mammal: public animal
{

};

class bat: public mammal, public bird


{

};
int main()
{
bat bxxx;

return 0;
}

5
A. Irungu

You might also like