Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ INHERITANCE Upated

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

C++ Inheritance

ACSC 328: OOP(C++) C++ INHERITANCE


Inheritance
The capability of a class to derive properties and
characteristics from another class is
called Inheritance.
Inheritance is one of the most important feature of
Object Oriented Programming.
We group the "inheritance concept" into two
categories:
Sub class/derived class (child) - the class that inherits
from another class
Super class/base class (parent) - the class being
inherited from
To inherit from a class, use the : symbol.
ACSC 328: OOP(C++) C++ INHERITANCE
Why and when to use inheritance?
Consider a group of vehicles. You need to create classes for Bus, Car and Truck.
The methods fuelAmount(), capacity(), applyBrakes() will be same for all of the three classes.
If we create these classes avoiding inheritance then we have to write all of these functions in
each of the three classes as shown in below figure:

ACSC 328: OOP(C++) C++ INHERITANCE


Why and when to use inheritance?...
You can clearly see that above process results in
duplication of same code 3 times.
This increases the chances of error and data
redundancy.
To avoid this type of situation, inheritance is used.
 If we create a class Vehicle and write these three
functions in it and inherit the rest of the classes from
the vehicle class, then we can simply avoid the
duplication of data and increase re-usability.
Look at the below diagram in which the three classes
are inherited from vehicle class:
Using inheritance, we have to write the functions only one time instead of three times as we have inherited
rest of the three classes from base class(Vehicle).

ACSC 328: OOP(C++) C++ INHERITANCE


Without inheritance

ACSC 328: OOP(C++) C++ INHERITANCE


Using inheritance

ACSC 328: OOP(C++) C++ INHERITANCE


Syntax:
class subclass_name : access_mode base_class_name
{
//body of subclass
};
Here, subclass_name is the name of the sub class
access_mode is the mode in which you want to inherit this sub class for example: public, private etc.
base_class_name is the name of the base class from which you want to inherit the sub class.
Note: A derived class doesn’t inherit access to private data members. However, it does inherit a full
parent object, which contains any private members which that class declares.

ACSC 328: OOP(C++) C++ INHERITANCE


//Base class
class Parent
{
Example
public:
int id_p;
};

// Sub class inheriting from Base Class(Parent)


class Child : public Parent
{
public:
int id_c;
};

//main function
int main()
{

Child obj1;

// An object of class child has all data members


// and member functions of class parent
obj1.id_c = 7;
obj1.id_p = 91;
cout << "Child id is " << obj1.id_c << endl;
cout << "Parent id is " << obj1.id_p << endl;

return 0;
ACSC 328: OOP(C++) C++ INHERITANCE
Modes of Inheritance
Public: When the member is declared as public, it is accessible to all the functions
of the program.
•There are no restrictions on accessing public members.
•The public members of a class can be accessed from anywhere in the program
using the direct member access operator (.) with the object of that class.
Private: Access is limited to within the class definition.
•This is the default access modifier type if none is formally specified.
•They are not allowed to be accessed directly by any object or function outside the
class.
Protected: When the member is declared as protected, it is accessible within its
own class as well as the class immediately derived from it.
Access is limited to within the class definition and any class that inherits from the
class.

ACSC 328: OOP(C++) C++ INHERITANCE


// C++ Implementation to show that a derived class
// doesn’t inherit access to private data members.
// However, it does inherit a full parent object
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 // 'private' is default for classes
{
// x is private
// y is private
// z is not accessible from D
};
ACSC 328: OOP(C++) C++ INHERITANCE
Types of inheritance
Single
Multiple
Multilevel
Hierarchical
Hybrid

ACSC 328: OOP(C++) C++ INHERITANCE


Single Inheritance
In single inheritance, a class is allowed to inherit from only one class. i.e. one sub class is
inherited by one base class only.

C++ INHERITANCE
Single inheritance
Syntax:

class subclass_name : access_mode base_class


{
//body of subclass
};

ACSC 328: OOP(C++) C++ INHERITANCE


// C++ program to explain Single inheritance
#include <iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
Output:
This is a Vehicle
// sub class derived from a single base classes
class Car: public Vehicle{

};

// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}

ACSC 328: OOP(C++) C++ INHERITANCE


int main()
#include <iostream> {
using namespace std; B b;
class A b.display();
{ return 0;
int a = 4; }
int b = 5;
public:
int mul()
{
int c = a*b;
return c;
}
};

class B : private A
{
public:
void display()
{
int result = mul();
cout <<"Multiplication of a and b is : "<<result<< endl;
}
};
ACSC 328: OOP(C++) C++ INHERITANCE
Multiple Inheritance
Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. i.e
one sub class is inherited from more than one base classes.

ACSC 328: OOP(C++) C++ INHERITANCE


Multiple Inheritance
Syntax:

class subclass_name : access_mode base_class1, access_mode base_class2, ....


{
//body of subclass
};

ACSC 328: OOP(C++) C++ INHERITANCE


// C++ program to explain multiple cout << "This is a 4 wheeler Vehicle" << endl;
inheritance }
#include <iostream> };
using namespace std;
// sub class derived from two base classes
// first base class class Car: public Vehicle, public FourWheeler {
class Vehicle {
public: };
Vehicle()
{ // main function
cout << "This is a Vehicle" << endl; int main()
} {
}; // creating object of sub class will
// invoke the constructor of base classes
// second base class Car obj;
class FourWheeler { return 0;
public: }
FourWheeler()
Output:
{ This is a Vehicle
This is a 4 wheeler Vehicle
ACSC 328: OOP(C++) C++ INHERITANCE
#include <iostream>
using namespace std;
class A
{ class C : public A,public B
protected: {
int a; public:
void display()
public:
{
void get_a(int n) cout << "The value of a is : " <<a<< endl;
{ cout << "The value of b is : " <<b<< endl;
a = n; cout<<"Addition of a and b is : "<<a+b;
} }
}; };
class B int main()
{ {
protected: C c;
c.get_a(10);
int b;
c.get_b(20);
public: c.display();
void get_b(int n) return 0;
{ }
b = n;
}
};
ACSC 328: OOP(C++) C++ INHERITANCE
Multilevel Inheritance
In this type of inheritance, a derived class is created from another derived class.

ACSC 328: OOP(C++) C++ INHERITANCE


// C++ program to implement Multilevel Inheritance // sub class derived from the derived base class
#include <iostream> fourWheeler
using namespace std; class Car: public fourWheeler{
// base class public:
class Vehicle car()
{ {
public: cout<<"Car has 4 Wheels"<<endl;
Vehicle() }
{ };
cout << "This is a Vehicle" << endl;
} // main function
}; int main()
{
// first sub_class derived from class vehicle //creating object of sub class will
class fourWheeler: public Vehicle //invoke the constructor of base classes
{ public: Car obj;
fourWheeler() return 0;
{ }
cout<<"Objects with 4 wheels are Output:
This is a Vehicle Objects
vehicles"<<endl; with 4 wheels are vehicles
} Car has 4 Wheels
};
ACSC 328: OOP(C++) C++ INHERITANCE
Hierarchical Inheritance
In this type of inheritance, more than one sub class is inherited from a single base class. i.e.
more than one derived class is created from a single base class.

ACSC 328: OOP(C++) C++ INHERITANCE


#include <iostream>
using namespace std;
// main function
// base class int main()
class Vehicle {
{ // creating object of sub class will
public: // invoke the constructor of base class
Vehicle() Car obj1;
{ Bus obj2;
cout << "This is a Vehicle" << endl; return 0;
} }
};

// first sub class


class Car: public Vehicle
{ Output:
This is a Vehicle This
is a Vehicle
};

// second sub class


class Bus: public Vehicle
{

};

ACSC 328: OOP(C++) C++ INHERITANCE


Hybrid (Virtual) Inheritance
Hybrid Inheritance is implemented by combining more than one type of inheritance. For
example: Combining Hierarchical inheritance and Multiple Inheritance.
Below image shows the combination of hierarchical and multiple inheritance

ACSC 328: OOP(C++) C++ INHERITANCE


// C++ program for Hybrid Inheritance

#include <iostream> // first sub class


using namespace std; class Car: public Vehicle
{
// base class
class Vehicle };
{
public: // second sub class
Vehicle() class Bus: public Vehicle, public Fare
{ {
cout << "This is a Vehicle" << endl;
} };
};
// main function
//base class int main()
class Fare {
{ // creating object of sub class will
public: // invoke the constructor of base class
Fare() Bus obj2;
{ return 0;
cout<<"Fare of Vehicle\n"; }
}
}; Output:
This is a Vehicle
Fare of Vehicle
C++ INHERITANCE
Exercise: To be submitted
a) Figure 1 shows an open cuboid. Write a C++ program that will implement two classes named Wall1
and Wall2 derived from a base class named pool. The base class contains the dimensions of two
sides and a method used to set the values appropriately. The program should determine and output
the surface area of the walls of the cuboid. Each derived class contains a method used to calculate
the area for the two similar walls. (10 marks)

10

C++ INHERITANCE
class pool int main()
{ {
protected: wall1 W1;
int l,w,h; wall2 W2;
public: int area=W1.area1() +W2.area2();
pool() cout<<"SURFACE AREA="<<area;
{ return 0;
l=10; }
w=5;
h=7;
}
};
class wall1: public pool
{
public:
int area1()
{
return (2*(l*h))+(2*(h*w));
}
};
class wall2: public pool
{
public:
int area2()
{
return l*w;
}
};

ACSC 328: OOP(C++) C++ INHERITANCE


int main()
{
class pool wall1 W1(10,5,7);
{ wall2 W2(10,5,7);
protected:
int l,w,h; int area=W1.area1() +W2.area2();
public: cout<<"SURFACE AREA="<<area;
pool(int l,int w, int h) return 0;
{ }
this->l=l;
this->w=w;
this->h=h;
}
};
class wall1: public pool
{
public:
wall1(int l,int w,int h): pool(l,w,h)//inheriting a parameteirized constructtor
{
}
int area1()
{
return (2*(l*h))+(2*(h*w));
}
};
class wall2: public pool
{
public:
wall2(int l,int w,int h): pool(l,w,h)//inheriting a parameteirized constructtor
{
}
int area2()
{
return l*w;
}
};

ACSC 328: OOP(C++) C++ INHERITANCE


class pool
{
protected:
int l,w,h;
public:
void set_val(int l,int w, int h)
{
this->l=l;
this->w=w;
this->h=h;
} int main()
}; {
class wall1: public pool wall1 W1;
{ W1.set_val(10,5,7);
public: wall2 W2;
W2.set_val(10,5,7);
int area1() int area=W1.area1() +W2.area2();
{ cout<<"SURFACE AREA="<<area;
return 0;
return (2*(l*h))+(2*(h*w)); }
}
};
class wall2: public pool
{
public:
int area2()
{
return l*w;
}
};

ACSC 328: OOP(C++) C++ INHERITANCE

You might also like