This document discusses inheritance in C++. It begins with an introduction to inheritance and its basic concepts like base class, parent class, and child class. It then provides an example of a parent class for cars and how child classes for police and player cars could inherit from it. The document also covers public, private, and protected inheritance and how visibility of members changes depending on the inheritance type. It explains that private members cannot be inherited while protected and public members have different access levels based on the inheritance specifier. Finally, it ends by welcoming questions.
2. Agenda of the presentation
• Basics of inheritance
• Private v/s public inheritance
• Glimpse at an example
3. Inheritance
In Object Oriented Programming, Inheritance is the process by which objects of
one class acquire the properties and functionality of objects of another class.
It supports the concept of hierarchical classification.
5. What in the need of inheritance in c++?
One reason to use inheritance is that it allows you to reuse code from a previous
project but gives you the flexibility to slightly modify it if the old code doesn’t do
exactly what you need for the new project.
It doesn’t make sense to start every new project from scratch since some code will
certainly be repeated in several programs and you should strive to build on what you did
previously.
Moreover, it is easy to make an error if we try to modify the original class, but we are
less likely to make an error if we leave the original alone and only add to it.
Another reason for using inheritance is if the project requires the use of several classes
which are very similar but slightly different.
6. What is a class?
A class is an expanded concept of a data structure: instead of holding only data, it
can hold both data and functions.
Base Class
A base class is a class that is created with the intention of deriving other
classes from it.
Parent Class
A parent class is the closest class that we derived from to create the one we are referencing
as the child class.
Child Class
A child class is a class that was derived from another, that will now be the parent class to it.
7. Example of a Parent Class
As an example, suppose you are creating a game, something using different cars,
and you need specific type of car for the policemen and another type for the
player(s). Both car types share similar properties. The major difference (on this
example case) would be that the policemen type would have sirens on
top of their cars and the players' cars will not.
8. What is a private class?
Private members of a class are accessible only from within other members of the
same class or from their friends.
What is a protected class?
Protected members are accessible from members of their same class and from
their friends, but also from members of their derived classes.
What is a public class?
Finally, public members are accessible from anywhere the object is visible.
9. This is our base class or parent class whatever you prefer
Class A
{
public:
int apub;
private:
int aprv; // this won't be accessible to child/sub classes
protected:
int apro;
}
Basic thumb rule in inheritance. You cannot inherit private members.
10. Public visibility mode during Inheritance
Public inheritance will inherit protected variables & functions.
Class B : public A // this is inheritance with private specifier
{
public:
int bpub; // also has access to aprv
private:
int bprv; // no access to apub
protected:
int bpro; // also has access to apro
}
here in class B you can use apub, bpub, apro, bpro but you cannot
access aprv because aprv is private variable of class A.
11. Private visibility mode during Inheritance
private members of class are are not accessible but protected & public will
become private members of class B
So anything that is public or protected in Class A will become private in class
B
Class B : private A // this is inheritance with private specifier
{
public:
int bpub;
private:
int bprv; // also has access to apub,apro but now apub and apro are private
memebers of this class
protected:
int bpro;
}
12. Protected visibility mode during Inheritance
protected inheritance will inherit protected as well as public variables
& methods of base class (class A) with visibilty modifier as protected
Class B : protected A
{
public:
int bpub;
private:
int bpri
protected:
int bpro; // also has access to apub,apro
}
13. Feel free to ask if there are any questions or queries
Thank you
Prepared by : Pooja K. Doshi