CPP Unit 5
CPP Unit 5
Base class
✓ A base class can be defined as a normal C++ class, which may consist of
some data and functions.
✓ The existing class whose properties are inherited by other/new class is
called the Parent or Base or Superclass.
Derived class
✓ The class which inherits properties of other class is called Child or
Derived or Sub class.
✓ In other words, a new class which acquires properties from base class is
called derived class.
✓ A derived class can be defined by class in addition to its own detail.
✓ Derived class is also known as a child class or sub class.
Advantage of Inheritance
✓ You can reuse the methods and data of the existing class. The existing
class code will be already tested and debugged.
✓ You can extend the existing class by adding new data and new methods
✓ You can modify the existing class by overloading its methods with your
own implementations
✓ As the existing code is reused, it leads to less development and
maintenance costs.
✓ Inheritance helps to reduce code redundancy and supports code
extensibility.
Visibility Modes
✓ The visibility mode is optional. It may be either public , private,
protected.
✓ The default visibility mode is private.
✓ Visibility mode specifies whether the features of the base
✓ class are privately
derived, publicly
derived or
protected.
✓ Examples:
class B : public A {
Members of B }
class B : private A{
Members of B }
// private derivation
class B : A
{ Members of B }
✓ Syntax:
class derived: private base{
//Members of derived class;
};
Types of Inheritance
1. Single inheritance
2. Multilevel inheritance
3. Multiple inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
Single Inheritance
✓ A derived class with only one base class is called single
inheritance.
✓ Implement an “is-a” relationship
✓ In single level inheritance there is only one base class and
one derived class.
✓ In figure, class A is called Base class and class B is called
as Derived class.
✓ Syntax Class base {
Member of base class
};
Class derive: public/private/protected base{
Member of derive class
};
Multilevel Inheritance
✓ When a class is derived from another derived class i.e.,
derived class act as a base class, such type of inheritance
is known as multilevel inheritance.
✓ In multilevel inheritance, the class is derived from another
derived class.
✓ Example - Class A {
------- };
Class B : public A {
------- };
Class C: public B {
------- };
Multiple Inheritance
✓ When two or more base classes are used from
derivation of a class, it is called as multiple
inheritance.
✓ In multiple inheritance, one derived class is derived
from more than one base class.
✓ Example - Class A {
------- };
Class B {
------- };
Class C: public B , public A {
------- };
Hierarchical Inheritance
✓ A class which contain only one base class and multiple derive class is
called Hierarchical Inheritance.
✓ In other word, when a single base
class is used for derivation of two or
more classes it is known as
hierarchical inheritance.
✓ In hierarchical inheritance, more than
one derived class is derived from a
single base class.
✓ Syntax - Class base1 {Member of
base class1};
Class derive1: public/private/protected base1{
Member of derive class1
};
Class derive2: public/private/protected base1
{
Member of derive class2
};
Hybrid Inheritance
✓ It is the combination of more than one type of Inheritance is called
Hybrid Inheritance.
✓ Hybrid Inheritance is combination of
Hierarchical and Multilevel Inheritance.
✓ Syntax - Class base{
Member of base class
};
Class derive1:public/private/protected base{
Member of derive1 class
};
Class derive2{
Member of derive2 class
};
Pointer – Introduction
✓ Every variable is a memory location and every memory location has its
address defined which can be accessed using ampersand (&) operator
which denotes an address in memory.
✓ A pointer is a variable whose value is the address of another variable.
✓ Like any variable or constant, you must declare a pointer before you can
work with it.
✓ The general form of a pointer variable declaration is:
✓ Syntax- datatype *var-name;
✓ Example: int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character
Pointer to Objects
✓ Pointer is a variable which stores the address of another variable.
This Pointer
✓ "A this pointer is automatically passed to a function when it is called.”
✓ C++ uses unique keyword called this to represent an object that
invokes/calls a member function.
✓ The this pointer is a pointer accessible only within the nonstatic member
functions of a class, struct, or union type.
✓ Static member functions don't have a this pointer.
✓ Eg: if we call max function using object A then A.max() will set the
pointer this to the address of the object A.
✓ Applications:
• this pointer is used to represent an object that invokes a member
function.
• Access data member with this pointer like, this->x=50;
• this pointer is to return the object it points to like return *this;
Virtual Function
✓ When we use the same function name in both the base and derived
classes, the function in base class is declared as virtual using the keyword
virtual preceding its normal declaration.
✓ A virtual member function in a base class expects to be overridden in a
derived class
destructors.
8. While a base pointer can point to any type of the derived object, the reverse is
not true.
9. When a base pointer points to a derived class, incrementing or
decrementing it will not make it to point to the next object of the derived class.
10. If a virtual function is defined in the base class, it need not be
necessarily redefined in the derived class.
Abstract Class
✓ Abstract class is a class which has at least one pure virtual function.
✓ A class from which we never want to create objects is called an abstract
class.
✓ Such class exist only as
a parent for derived
classes.
✓ If we try to create
object of such base
class then compiler
would generate error.
✓ The main objective is to provide some traits to the derived classes and to
create base pointer to achieve run time polymorphism.
✓ Abstract classes are mainly used for Upcasting, which means that its
12 | P a g e 186_Shraddha Keshao Bonde_N2
CM3104