0% found this document useful (0 votes)
23 views5 pages

Lecture No 3 Inheritence

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

Lecture No 3 Inheritence

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

Lecture no 3

Inheritance and multiple inheritance


Inheritance in C++ is a fundamental concept in Object-Oriented Programming (OOP) that allows one
class (the derived class) to inherit properties and behaviors (data members and member functions) from
another class (the base class). This helps in reusing code, creating relationships between classes, and
modeling real-world hierarchies.

Inheritance allows a class to inherit the properties and behavior from another class

The class that inherits from another class is called derived or child class.

The class being inherited from is called parent and base class.

class derived_class_name : access-specifier base_class_name


{
// body ....
};

where,

 class: keyword to create a new class

 derived_class_name: name of the new class, which will inherit the base class

 access-specifier: Specifies the access mode which can be either of private, public or protected. If
neither is specified, private is taken as default.

 base-class-name: name of the base 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

Example 2: Access the Inherited Members of the Base Class in Derived Class

C++

// C++ program to illustrate how to access the inherited

// members of the base class in derived class

#include <iostream>

using namespace std;

// Base class

class Base {

public:
// data member

int publicVar;

// member method

void display()

cout << "Value of publicVar: " << publicVar;

};

// Derived class

class Derived : public Base {

public:

// Function to display inherited member

void displayMember()

// accessing public base class member method

display();

// Function to modify inherited member

void modifyMember(int pub)

// Directly modifying public member

publicVar = pub;

};

int main()
{

// Create an object of Derived class

Derived obj;

// Display the initial values of inherited member

obj.modifyMember(10);

// Display the modified values of inherited member

obj.displayMember();

return 0;

Output

Value of publicVar: 10

In the above example, we have accessed the public members of the base class in the derived class but
we cannot access all the base class members directly in the derived class. It depends on the mode of
inheritance and the access specifier in the base class.

Types of inheritance

1. Single inheritance
2. Multi-level inheritance
3. Hybrid inheritance
4. Hierarchal inheritance

Multiple Inheritance

In multiple inheritance, a class can inherit from more than one base class. This allows the derived class to
combine functionalities from different base classes, making it more versatile. However, it can also lead to
complexities like the diamond problem (discussed below).

In C++ programming, a class can be derived from more than one parent.

For example, A class Bat is derived from base classes Mammal and Winged Animal. It makes sense

because bat is a mammal as well as a winged animal.


#include <iostream>

using namespace std;

class Father {

public:

void height() {

cout << "Tall height from Father" << endl;

};

class Mother {

public:

void eyes() {

cout << "Brown eyes from Mother" << endl;

};

class Child : public Father, public Mother { // Multiple inheritance

};

int main() {

Child child;

child.height();

child.eyes();

return 0;

Output
Tall height from Father

Brown eyes from Mother

=== Code Execution Successful ===

Here, the Child class inherits from both father and Mother, gaining their characteristics (methods).

3. The Diamond Problem

In multiple inheritance, if two base classes have a common ancestor, this can lead to ambiguity known as
the diamond problem. When the derived class tries to access a member of the common base class, it
might not know which version of the base class to use.

You might also like