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

Inheritance 2

C++ allows classes to inherit from base classes in three access modes: public, protected, and private. Public inheritance makes base class public members public in the derived class and protected members protected. Protected inheritance makes base public and protected members protected in the derived class. Private inheritance makes base public and protected members private in the derived class. Function overriding occurs when a derived class defines a function with the same name as a base class function. When called on a derived class object, the derived class function overrides and executes instead of the base class function. The base class function can still be accessed using the scope resolution operator.

Uploaded by

Jatin Garg
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)
21 views

Inheritance 2

C++ allows classes to inherit from base classes in three access modes: public, protected, and private. Public inheritance makes base class public members public in the derived class and protected members protected. Protected inheritance makes base public and protected members protected in the derived class. Private inheritance makes base public and protected members private in the derived class. Function overriding occurs when a derived class defines a function with the same name as a base class function. When called on a derived class object, the derived class function overrides and executes instead of the base class function. The base class function can still be accessed using the scope resolution operator.

Uploaded by

Jatin Garg
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/ 17

C++ Public,

Protected and
Private Inheritance
In C++ inheritance, we can derive a child class from the base class in different
access modes. For example,
class Base {
.... ... ....
};

class Derived : public Base {


.... ... ....
};

Notice the keyword public in the code

class Derived : public Base

This means that we have created a derived class from the base class in public
mode. Alternatively, we can also derive classes in protected or private modes.
These 3 keywords (public, protected, and private) are known as access
specifiers in C++ inheritance.

public, protected and private inheritance in C++


public, protected, and private inheritance have the following features:
 public inheritance makes public members of the base class public in
the derived class, and the protected members of the base class
remain protected in the derived class.
 protected inheritance makes the public and protected members of the
base class protected in the derived class.
 private inheritance makes the public and protected members of the
base class private in the derived class.

Note: private members of the base class are inaccessible to the derived
class.
class Base {
public:
int x;
protected:
int y;
private:
int z;
};

class PublicDerived: public Base {


// x is public
// y is protected
// z is not accessible from PublicDerived
};

class ProtectedDerived: protected Base {


// x is protected
// y is protected
// z is not accessible from ProtectedDerived
};

class PrivateDerived: private Base {


// x is private
// y is private
// z is not accessible from PrivateDerived
};

// C++ program to demonstrate the working of public inheritance

#include <iostream>
using namespace std;

class Base {
private:
int pvt = 1;

protected:
int prot = 2;

public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};

class PublicDerived : public Base {


public:
// function to access protected member from Base
int getProt() {
return prot;
}
};

int main() {
PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
return 0;
}

Output

Private = 1
Protected = 2
Public = 3

Here, we have derived PublicDerived from Base in public mode.


As a result, in PublicDerived:
 prot is inherited as protected.
 pub and getPVT() are inherited as public.
 pvt is inaccessible since it is private in Base.
Since private and protected members are not accessible from main(), we
need to create public functions getPVT() and getProt() to access them:

// Error: member "Base::pvt" is inaccessible


cout << "Private = " << object1.pvt;

// Error: member "Base::prot" is inaccessible


cout << "Protected = " << object1.prot;

Notice that the getPVT() function has been defined inside Base. But
the getProt() function has been defined inside PublicDerived.
This is because pvt, which is private in Base, is inaccessible to PublicDerived.
However, prot is accessible to PublicDerived due to public inheritance.
So, getProt() can access the protected variable from within PublicDerived.

Accessibility in public Inheritance


Accessibility private members protected members public members

Base Class Yes Yes Yes

Derived Class No Yes Yes

Example 2: C++ protected Inheritance


/ C++ program to demonstrate the working of protected inheritance

#include <iostream>
using namespace std;

class Base {
private:
int pvt = 1;

protected:
int prot = 2;

public:
int pub = 3;

// function to access private member


int getPVT() {
return pvt;
}
};

class ProtectedDerived : protected Base {


public:
// function to access protected member from Base
int getProt() {
return prot;
}

// function to access public member from Base


int getPub() {
return pub;
}
};

int main() {
ProtectedDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}

Output

Private cannot be accessed.


Protected = 2
Public = 3

Here, we have derived ProtectedDerived from Base in protected mode.


As a result, in ProtectedDerived:
 prot, pub and getPVT() are inherited as protected.
 pvt is inaccessible since it is private in Base.
As we know, protected members cannot be directly accessed from outside
the class. As a result, we cannot use getPVT() from ProtectedDerived.

That is also why we need to create the getPub() function in ProtectedDerived in


order to access the pub variable.

// Error: member "Base::getPVT()" is inaccessible


cout << "Private = " << object1.getPVT();

// Error: member "Base::pub" is inaccessible


cout << "Public = " << object1.pub;

Accessibility in protected Inheritance


Accessibility private members protected members public members

Base Class Yes Yes Yes

Derived Class No Yes Yes (inherited as protected variables)

Example 3: C++ private Inheritance


/ C++ program to demonstrate the working of private inheritance

#include <iostream>
using namespace std;

class Base {
private:
int pvt = 1;

protected:
int prot = 2;

public:
int pub = 3;

// function to access private member


int getPVT() {
return pvt;
}
};

class PrivateDerived : private Base {


public:
// function to access protected member from Base
int getProt() {
return prot;
}

// function to access private member


int getPub() {
return pub;
}
};

int main() {
PrivateDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}

Output

Private cannot be accessed.


Protected = 2
Public = 3

Here, we have derived PrivateDerived from Base in private mode.


As a result, in PrivateDerived:
 prot, pub and getPVT() are inherited as private.
 pvt is inaccessible since it is private in Base.
As we know, private members cannot be directly accessed from outside the
class. As a result, we cannot use getPVT() from PrivateDerived.
That is also why we need to create the getPub() function in PrivateDerived in
order to access the pub variable.
// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();

// Error: member "Base::pub" is inaccessible


cout << "Public = " << object1.pub;

Accessibility in private Inheritance

Accessibility private members protected members public members

Base Class Yes Yes Yes

Derived Class No Yes (inherited as private variables) Yes (inherited as private variables)

C++ Function Overriding


As we know, inheritance is a feature of OOP that allows us to create derived
classes from a base class. The derived classes inherit features of the base
class.
Suppose, the same function is defined in both the derived class and the based
class. Now if we call this function using the object of the derived class, the
function of the derived class is executed.

This is known as function overriding in C++. The function in derived class


overrides the function in base class.

Example 1: C++ Function Overriding


// C++ program to demonstrate function overriding

#include <iostream>
using namespace std;

class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};

class Derived : public Base {


public:
void print() {
cout << "Derived Function" << endl;
}
};

int main() {
Derived derived1;
derived1.print();
return 0;
}

Output

Derived Function

Here, the same function print() is defined in both Base and Derived classes.
So, when we call print() from the Derived object derived1,
the print() from Derived is executed by overriding the function in Base.
Working of function overriding in
C++

As we can see, the function was overridden because we called the function
from an object of the Derived class.
Had we called the print() function from an object of the Base class, the function
would not have been overridden.

// Call function of Base class


Base base1;
base1.print(); // Output: Base Function
Access Overridden Function in C++
To access the overridden function of the base class, we use the scope
resolution operator ::.
We can also access the overridden function by using a pointer of the base
class to point to an object of the derived class and then calling the function
from that pointer.

Example 2: C++ Access Overridden Function to the Base Class

// C++ program to access overridden function


// in main() using the scope resolution operator ::

#include <iostream>
using namespace std;

class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};

class Derived : public Base {


public:
void print() {
cout << "Derived Function" << endl;
}
};

int main() {
Derived derived1, derived2;
derived1.print();

// access print() function of the Base class


derived2.Base::print();

return 0;
}

Output

Derived Function
Base Function

Here, this statement

derived2.Base::print();

accesses the print() function of the Base class.

Access overridden function using


object of derived class in C++
Example 3: C++ Call Overridden Function From Derived Class

// C++ program to call the overridden function


// from a member function of the derived class

#include <iostream>
using namespace std;

class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};

class Derived : public Base {


public:
void print() {
cout << "Derived Function" << endl;

// call overridden function


Base::print();
}
};

int main() {
Derived derived1;
derived1.print();
return 0;
}

Output

Derived Function
Base Function

In this program, we have called the overridden function inside


the Derived class itself.

class Derived : public Base {


public:
void print() {
cout << "Derived Function" << endl;
Base::print();
}
};

Notice the code Base::print();, which calls the overridden function inside
the Derived class.

Access overridden function inside


derived class in C++

Example 4: C++ Call Overridden Function Using Pointer

// C++ program to access overridden function using pointer


// of Base type that points to an object of Derived class

#include <iostream>
using namespace std;

class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};

class Derived : public Base {


public:
void print() {
cout << "Derived Function" << endl;
}
};

int main() {
Derived derived1;

// pointer of Base type that points to derived1


Base* ptr = &derived1;

// call function of Base class using ptr


ptr->print();

return 0;
}

Output

Base Function

In this program, we have created a pointer of Base type named ptr. This pointer
points to the Derived object derived1.

// pointer of Base type that points to derived1


Base* ptr = &derived1;
When we call the print() function using ptr, it calls the overridden function
from Base.

// call function of Base class using ptr


ptr->print();

This is because even though ptr points to a Derived object, it is actually


of Base type. So, it calls the member function of Base.

In order to override the Base function instead of accessing it, we need to


use virtual functions in the Base class.

You might also like