Why and When To Use Inheritance?: Class Subclass - Name: Access - Mode Base - Class - Name (//body of Subclass)
Why and When To Use Inheritance?: Class Subclass - Name: Access - Mode Base - Class - Name (//body of Subclass)
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).
// Base class
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
return 0;
}
When the above code is compiled and executed, it produces the following result −
Total area: 35
When deriving a class from a base class, the base class may be inherited
through public, protected or private 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 public and protected members of
the base class.
Protected Inheritance − When deriving from a protected base
class, public and protected members 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.
#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
double length;
double height;
public:
double calculateArea() {
return length * height;
}
};
int main() {
// create an object of Wall class
Wall wall1(10.5, 8.6);
return 0;
}
Dear Sir,
Greetings from the Department of Computer Engineering, PCCoE, Pune !!
Thanks a lot for providing your valuable suggestions and approvals for Honors in
Deep Learning, Minors in Data Science, Project, and internship guidelines.
#include <iostream>
class Wall {
private:
double length;
double height;
public:
length = len;
height = hgt;
double calculateArea() {
};
int main() {
return 0;
#include <iostream>
class CRectArea
private:
int length;
int breadth;
public:
CRectArea (int,int);
int areaofrect ()
int length1()
return length;
int breadth1()
return breadth;
};
CRectArea::CRectArea(int x, int y)
length = x;
breadth = y;
}
int main ()
return 0;
Practice session- 1. Create a class named 'Rectangle' with two data members- length and breadth and
a function to calculate the area which is 'length*breadth'. The class has three constructors which are :
1 - having no parameter - values of both length and breadth are assigned zero. 2 - having two
numbers as parameters - the two numbers are assigned as length and breadth respectively. 3 -
having one number as parameter - both length and breadth are assigned that number. Now, create
objects of the 'Rectangle' class having none, one and two parameters and print their areas.
2. Create a class to print an integer and a character using two functions having the same name but
different sequence of the integer and the character parameters.