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

Why and When To Use Inheritance?: Class Subclass - Name: Access - Mode Base - Class - Name (//body of Subclass)

Here are the programs for the practice problems: 1. Rectangle class with different constructors: #include <iostream> using namespace std; class Rectangle { int length, breadth; public: Rectangle() { length = 0; breadth = 0; } Rectangle(int l, int b) { length = l; breadth = b; } Rectangle(int s) { length = s; breadth = s; } int area() { return length*breadth; } }; int main() { Rectangle r1; Rectangle r2(10,5); Rectangle r3(3); cout <

Uploaded by

Swati Jaiswal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Why and When To Use Inheritance?: Class Subclass - Name: Access - Mode Base - Class - Name (//body of Subclass)

Here are the programs for the practice problems: 1. Rectangle class with different constructors: #include <iostream> using namespace std; class Rectangle { int length, breadth; public: Rectangle() { length = 0; breadth = 0; } Rectangle(int l, int b) { length = l; breadth = b; } Rectangle(int s) { length = s; breadth = s; } int area() { return length*breadth; } }; int main() { Rectangle r1; Rectangle r2(10,5); Rectangle r3(3); cout <

Uploaded by

Swati Jaiswal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

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. In other words, t he capability
of a class to derive properties and characteristics from another class is
called Inheritance. 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.

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: 

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).

Implementing inheritance in C++: For creating a sub-class which is


inherited from the base class we have to follow the below syntax. 
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. and 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.

Base and Derived Classes


A class can be derived from more than one classes, which means it can inherit data
and functions from multiple base classes. To define a derived class, we use a class
derivation list to specify the base class(es). A class derivation list names one or
more base classes and has the form −
class derived-class: access-specifier base-class
Where access-specifier is one of public, protected, or private, and base-class is
the name of a previously defined class. If the access-specifier is not used, then it is
private by default.
Consider a base class Shape and its derived class Rectangle as follows −
Live Demo
#include <iostream>

using namespace std;

// 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);

// Print the area of the object.


cout << "Total area: " << Rect.getArea() << endl;

return 0;
}
When the above code is compiled and executed, it produces the following result −
Total area: 35

Access Control and Inheritance


A derived class can access all the non-private members of its base class. Thus
base-class members that should not be accessible to the member functions of
derived classes should be declared private in the base class.
A derived class inherits all base class methods with the following exceptions −

 Constructors, destructors and copy constructors of the base class.


 Overloaded operators of the base class.
 The friend functions of the base class.

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:

// initialize variables with parameterized constructor


Wall(double len, double hgt) {
length = len;
height = hgt;
}

// copy constructor with a Wall object as parameter


// copies data of the obj parameter
Wall(Wall &obj) {
length = obj.length;
height = obj.height;
}

double calculateArea() {
return length * height;
}
};

int main() {
// create an object of Wall class
Wall wall1(10.5, 8.6);

// copy contents of wall1 to wall2


Wall wall2 = wall1;

// print areas of wall1 and wall2


cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();

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.

As per earlier communications, we are sending the syllabus of Honors in Cyber


Security. It is uploaded on the same drive. Kind request you to provide your
valuable suggestions/approvals. You can write in the doc file, that is shared here
(Filename: BoS_suggestions_CyberSecurity).

Please find the gdrive


link: https://drive.google.com/drive/folders/1lAOrR3hG5iV8xNV5IS3511cQe-
m9O6U0?usp=sharing 

Thanks and Warm Regards


 
Dr.K.Rajeswari,
Professor & Head, Department of Computer Engineering,
Pimpri Chinchwad College of Engineering(PCCOE),
PUNE.

#include <iostream>

using namespace std;

class Wall {

private:

double length;

double height;

public:

Wall(double len, double hgt) {

length = len;

height = hgt;

double calculateArea() {

return length * height;


}

};

int main() {

Wall wall1(10.5, 8.6);

Wall wall2(8.5, 6.3);

cout << "Area of Wall 1: " << wall1.calculateArea() << endl;

cout << "Area of Wall 2: " << wall2.calculateArea();

return 0;

#include <iostream>

using namespace std;

class CRectArea

private:

int length;

int breadth;

public:

CRectArea (int,int);
int areaofrect ()

return (length * breadth);

int length1()

return length;

int breadth1()

return breadth;

};

CRectArea::CRectArea(int x, int y)

length = x;

breadth = y;

}
int main ()

CRectArea myrectangle (2,2);

cout<<"The Length of Rectangle :: "<<myrectangle.length1()<<"\n";

cout<<"\nThe Breadth of Rectangle :: "<<myrectangle.breadth1()<<"\n";

cout << "\nThe area of rectangle is :: " << myrectangle.areaofrect()<< endl;

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.

You might also like