0% found this document useful (0 votes)
1K views22 pages

Understanding C++ Inheritance Concepts

This document discusses inheritance in C++. It defines inheritance as a concept that allows a class to inherit features from another existing class, called the base class. The new class that inherits is called the derived class. The derived class has all features of the base class and can add new features. Inheritance promotes code reusability and reduces time and effort. The general format for implementing inheritance in C++ is shown. The document also discusses public, private, and protected access specifiers and provides an example of inheritance in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views22 pages

Understanding C++ Inheritance Concepts

This document discusses inheritance in C++. It defines inheritance as a concept that allows a class to inherit features from another existing class, called the base class. The new class that inherits is called the derived class. The derived class has all features of the base class and can add new features. Inheritance promotes code reusability and reduces time and effort. The general format for implementing inheritance in C++ is shown. The document also discusses public, private, and protected access specifiers and provides an example of inheritance in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
  • Introduction
  • C++ Inheritance Overview
  • Features of Inheritance
  • Implementation Format
  • Access Specifiers
  • Types of Inheritance

WEL COME

PRAVEEN M JIGAJINNI
PGT (Computer Science)
MTech[IT],MPhil ([Link]), MCA, MSc[IT], PGDCA, ADCA,
Dc. Sc. & Engg.
email: praveenkumarjigajinni@[Link]
C++ Inheritance
C++ Inheritance
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. 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.
C++ Inheritance

Inheritance is the process by which new classes called


derived classes are created from existing classes called
base classes.

The derived classes have all the features of the base


class and the programmer can choose to add new
features specific to the newly created derived class.
C++ Inheritance

Features or Advantages of Inheritance:

Reusability:
Inheritance helps the code to be reused in many
situations.

The base class is defined and once it is compiled, it


need not be reworked.

Using the concept of inheritance, the programmer can


create as many derived classes from the base class as
needed while adding specific features to each derived
class as needed.
C++ Inheritance

Features or Advantages of Inheritance:


Saves Time and Effort:

The above concept of reusability achieved by inheritance


saves the programmer time and effort. The main code
written can be reused in various situations as needed.

Increases Program Structure which results in


greater reliability.
C++ Inheritance

General Format for implementing the concept of Inheritance:

class derived_classname: access specifier


baseclassname

For example, if the base class is MyClass and the derived


class is sample it is specified as:

class sample: public MyClass

The above makes sample have access to both public and


protected variables of base class MyClass
C++ Inheritance
Reminder about public, private and protected access
specifiers:

1 If a member or variables defined in a class is private, then


they are accessible by members of the same class only and
cannot be accessed from outside the class.

2 Public members and variables are accessible from outside


the class.

3 Protected access specifier is a stage between private and


public. If a member functions or variables defined in a class
are protected, then they cannot be accessed from outside
the class but can be accessed from the derived class.
Type of Inheritance

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.

We hardly use protected or private inheritance,


but public inheritance is commonly used. While
using different type of inheritance, following rules are
applied:
Type of Inheritance

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.
Type of Inheritance

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
C++ Inheritance
Inheritance Example:

class MyClass

{ public:
MyClass(void) { x=0; }
void f(int n1)
{ x= n1*5;}
void output(void) { cout<<x; }
private:
int x;
};
C++ Inheritance
Inheritance Example:

class sample: public MyClass


{ public:
sample(void) { s1=0; }
void f1(int n1)
{ s1=n1*10;}
void output(void)
{ MyClass::output(); cout << s1; }
private:
int s1;
};
C++ Inheritance
Inheritance Example:

int main(void)
{ sample s;
s.f(10);
[Link]();
s.f1(20);
[Link]();
}

The output of the above program is


50
200
Types of Inheritance
1. Single class Inheritance:

Single inheritance is the one where you have a


single base class and a single derived class.

Class Employee It is a Base class (super)

Class Manager it is a sub class (derived)


Types of Inheritance
2. Multilevel Inheritance:
In Multi level inheritance, a class inherits its
properties from another derived class.

Class A it is a Base class (super) of B

it is a sub class (derived) of A


Class B
and base class of class C

Class C derived class(sub) of class B


Types of Inheritance
3. Multiple Inheritances:

In Multiple inheritances, a derived class inherits


from multiple base classes. It has properties of
both the base classes.

Class A Class B Base class

Class C Derived class


Types of Inheritance
4. Hierarchical Inheritance:

In hierarchical Inheritance, it's like an inverted tree.


So multiple classes inherit from a single base
class. It's quite analogous to the File system in a
unix based system.

Class A

Class B Class D Class C


Types of Inheritance
5. Hybrid Inheritance:
In this type of inheritance, we can have mixture of
number of inheritances but this can generate an
error of using same name function from no of
classes, which will bother the compiler to how to
use the functions.
Therefore, it will generate errors in the program.
This has known as ambiguity or duplicity.
Ambiguity problem can be solved by using virtual
base classes
Types of Inheritance
5. Hybrid Inheritance:

Class A

Class B Class C

Class D
?
Any Questions Please
Thank You
Very Much

Common questions

Powered by AI

In C++ inheritance, the differences among public, protected, and private inheritance relate to how the members of the base class are derived in the new class. With public inheritance, public members of the base class remain public and protected members become protected in the derived class . In protected inheritance, both public and protected members of the base class become protected members of the derived class . Private inheritance causes both public and protected members of the base class to become private in the derived class . In all cases, private members of a base class are inaccessible directly by the derived class but can be accessed through the base class's public or protected functions .

Inheritance in C++ promotes code reusability by allowing new classes, called derived classes, to inherit properties and functionalities from existing classes, referred to as base classes. This means that the code from the base class does not need to be rewritten for the derived class, saving time and effort . Moreover, the hierarchical structure provided by inheritance enhances program organization and reliability, as the foundational code is only defined once and can be extended with new features in the derived classes .

The problem of ambiguity in hybrid inheritance arises when a derived class inherits from multiple base classes, which may have functions with the same name. This confuses the compiler as it cannot determine which function to use, leading to an error . This condition is known as ambiguity. To resolve this issue, virtual base classes can be employed. By declaring a class as a virtual base class, it ensures only one copy of the base class is inherited by all derived classes that ultimately use it, resolving the ambiguity problem .

Multiple inheritance in C++ allows a derived class to inherit from more than one base class, thereby incorporating properties and behaviors from all the involved base classes . A potential complication of this design pattern is the 'Diamond Problem,' where multiple paths exist due to shared ancestry, leading to possible ambiguities or duplications in member functions or variables. This can often introduce errors or unexpected behavior unless carefully managed, often requiring the use of virtual base classes to ensure the proper inheritance chain .

Hierarchical inheritance involves multiple derived classes inheriting from a single base class, forming a tree-like structure. Each derived class can introduce new features based on the shared inheritance of the base class . This structure is analogous to a UNIX file system, where various files or directories can exist under a single parent directory, and each can further contain their specific contents .

Public inheritance is more commonly used because it provides a clear and logical relationship between the base and derived classes. It allows derived classes to access public and protected members of the base class in a straightforward manner, which aligns with the typical need to extend and utilize functionalities of the base class . Protected and private inheritance restrict the access of base class members, which can complicate the usage of the inherited components, thus they are less commonly employed unless specific access control is needed .

Encapsulation in C++ through inheritance is achieved by controlling access to the base class's data members and functions. Using access specifiers (public, protected, and private), a base class can encapsulate its data members such that only certain elements are accessible to derived classes, maintaining integrity and security of the internal state . Public members are accessible from outside, protected members are accessible within derived classes, and private members are restricted, thus providing a controlled exposure of class functionalities .

A derived class might override a base class function to provide a specific implementation that better suits its context or purpose in the application. For example, in a graphical user interface framework, a base class might implement a generic 'draw' function. A derived class representing a specific shape, like a 'Circle', could override this function to provide a drawing routine that accurately renders a circle. This allows extensions of the base functionality to adapt dynamically and behave appropriately for different derived classes .

To prevent reusing the private members of a derived class in further derivations, a programmer can declare those members as private in the derived class itself. When a class member is private, it cannot be accessed or reused by any further derived classes, even though these new classes may still inherit public and protected members from all previous classes. This encapsulation restricts the direct access of private members, ensuring they remain usable only within the class they are defined .

Multilevel inheritance involves a hierarchy in which a derived class is based on another derived class, thus creating multiple levels of inheritance. For instance, if Class B is derived from Class A, and Class C is derived from Class B, it constitutes multilevel inheritance . In contrast, single inheritance involves only one level of derivation where a derived class directly inherits from a single base class. There are no intermediary classes between the base and derived class as in the case of multilevel inheritance .

WEL COME
         PRAVEEN M JIGAJINNI
         PGT (Computer Science)
MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA,
C++ Inheritance
One of the most important concepts in object-oriented 
programming is that of inheritance. Inheritance allows us 
to define a
Inheritance is the process by which new classes called 
derived classes are created from existing classes called 
base classe
Features or Advantages of Inheritance: 
Reusability: 
Inheritance helps the code to be reused in many 
situations. 
The bas
Features or Advantages of Inheritance: 
Saves Time and Effort:
 
The above concept of reusability achieved by inheritance 
s
General Format for implementing the concept of Inheritance: 
class derived_classname: access specifier 
baseclassname 
For ex
Reminder about public, private and protected access 
specifiers: 
1 If a member or variables defined in a class is private, t
When deriving a class from a base class, the base 
class may be inherited through public, protected or 
private inheritance.
Public Inheritance: When deriving a class from 
a public base class, public members of the base 
class become public members

You might also like