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

[inheritance] Quick refresher (1)

Inheritance in C++ allows a new class (derived class) to inherit members from an existing class (base class), enhancing code reusability and maintainability. It establishes an 'is a' relationship between classes, with single and multiple inheritance options available. Access specifiers determine member visibility, and derived classes can override inherited functions if needed.

Uploaded by

Gaya Medj
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)
4 views

[inheritance] Quick refresher (1)

Inheritance in C++ allows a new class (derived class) to inherit members from an existing class (base class), enhancing code reusability and maintainability. It establishes an 'is a' relationship between classes, with single and multiple inheritance options available. Access specifiers determine member visibility, and derived classes can override inherited functions if needed.

Uploaded by

Gaya Medj
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/ 4

Inheritance in C++

Inheritance is one of the most important concepts in object-oriented programming.


Inheritance allows a new class to inherit the members of an existing class.
The new class can inherit all the member methods with the following exceptions:

Constructors and destructors.


Friend functions.
Inheritance improves code reusability, maintainability, and implementation time.
The new and existing class are respectively called: the base and the derived class.

Base class

Derived class

Inheritance implements the is a relationship between classes. For example, a student


is a person, a graduate student is a student, hence a graduate student is a person.
The difference between inheritance and composition of classes is as follows:
Inheritance: implements the “is a”, e.g. the car is a vehicle.
Composition: implements the “has a”, e.g. the car has a steering wheel.

Base and Derived Classes


A class can inherit data and functions from one class or multiple base classes. The
first one is called single inheritance and the second is called multiple inheritance.
To define a derived class, we use a class derivation list to specify the base class(es):
class Derived-class access-specifier Base-class

E.g. class Rectangle: public Shape


Derived-class is the name of the new class
Base-class is the name of a previously defined class
Access-specifier is public, protected, or private
If the access-specifier is not used, then it is private by default.
Example: consider a base class Shape and its derived class Rectangle as follows:
#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;

// use setters of the base class


Rect.setWidth(5);
Rect.setHeight(7);

// use function of the derived class


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

return 0;
}
Output: Total area: 35

Access specifiers in base class


The access types can be summarized according to who can access the members:

Access type public protected private

Same class yes yes yes

Derived classes yes yes no

Outside classes yes no no


A derived class can access all the public and protected members of its base class.
Set base-class members private if they should not be accessible to derived classes.
The private data members are still inherited, but we need to add access methods.
Actually, using private data members provide a better protection than protected.

Access specifiers in inheritance


The base class may be inherited through public, protected or private inheritance.
Public inheritance is commonly used unlike protected and private inheritance.
The type of inheritance is specified by the inheritance specification as shown below.

Visibility of members in the derived class after inheritance

Multiple Inheritance
A class can inherit members from more than one class, here is the extended syntax:
class Derived-class access-specifier Base-class A, access-specifier Base-class B,

Access specifier is public, protected, or private and it is given for every base class.
Example:

#include <iostream>

using namespace std;

// Base class Shape


class Shape {
public:
void setWidth(int w) { width = w; }
void setHeight(int h) { height = h; }

protected:
int width;
int height;
};

// Base class PaintCost


class PaintCost {
public:
int getCost(int area) { return area * 70; }
};

// Derived class
class Rectangle: public Shape, public PaintCost {
public:
int getArea() { return (width * height); }
};

int main()

Rectangle Rect;

Rect.setWidth(5);
Rect.setHeight(7);

int area = Rect.getArea();

// Print the area of the object using derived class


cout << "Total area: " << area << endl;

// Print the total cost of painting using base class


cout << "Total paint cost: $" << Rect.getCost(area) << endl;

return 0;
}
Output: Total area: 35
Total paint cost: $2450

Function overriding
If needed, a derived class can redefine an inherited member function.
If the derived class defines a member function which has the same signature (name,
number and type of parameters) as the base class, then the derived class is
overriding this inherited member function.

Reference: https://www.tutorialspoint.com/cplusplus/

You might also like