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

Module-3

Module 3 covers the concepts of inheritance and polymorphism in C++. It explains various types of inheritance in C++, including single, multiple, multilevel, hierarchical, and hybrid inheritance, along with examples and syntax. The module also introduces polymorphism, emphasizing its two types: compile-time and run-time, and provides real-life analogies to illustrate the concept.
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)
13 views

Module-3

Module 3 covers the concepts of inheritance and polymorphism in C++. It explains various types of inheritance in C++, including single, multiple, multilevel, hierarchical, and hybrid inheritance, along with examples and syntax. The module also introduces polymorphism, emphasizing its two types: compile-time and run-time, and provides real-life analogies to illustrate the concept.
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/ 39

Module 3 – Inheritance & Polymorphism

DAYANANDA SAGAR COLLEGE OF ENGINEERING


Shavige Malleshwara Hills, Kumaraswamy Layout, Bengaluru-560078
(An Autonomous Institution affiliated to VTU, Belagavi)

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Module 3
Inheritance
Inheritance is a process in which one object acquires all the properties and behaviors of its parent
object automatically. the class which inherits the members of another class is called derived class
and the class whose members are inherited is called base class. The derived class is the
specialized class for the base class.

Advantage of C++ Inheritance


Code reusability: Now you can reuse the members of your parent class. So, there is no need to
define the member again. So less code is required in the class.

Types of Inheritance
C++ supports five types of inheritance:

• Single inheritance
• Multiple inheritance
• Hierarchical inheritance
• Multilevel inheritance
• Hybrid inheritance

Department of CSE, DSCE Page 1


Module 3 – Inheritance & Polymorphism

Derived Classes
A Derived class is defined as the class derived from the base class.

The Syntax of Derived class:

class derived_class_name :: visibility-mode base_class_name


{
// body of the derived class.
}

Where,

derived_class_name: It is the name of the derived class.

visibility mode: The visibility mode specifies whether the features of the base class are publicly
inherited or privately inherited. It can be public or private.

base_class_name: It is the name of the base class.

• When the base class is privately inherited by the derived class, public members of the base
class becomes the private members of the derived class. Therefore, the public members of the
base class are not accessible by the objects of the derived class only by the member functions
of the derived class.
• When the base class is publicly inherited by the derived class, public members of the base class
also become the public members of the derived class. Therefore, the public members of the
base class are accessible by the objects of the derived class as well as by the member functions
of the base class.

Note:
• In C++, the default mode of visibility is private.
• The private members of the base class are never inherited.

C++ Single Inheritance

Department of CSE, DSCE Page 2


Module 3 – Inheritance & Polymorphism

Single inheritance is defined as the inheritance in which a derived class is inherited from the only
one base class.

Where 'A' is the base class, and 'B' is the derived class.

C++ Single Level Inheritance Example: Inheriting Fields


When one class inherits another class, it is known as single level inheritance. Let's see the example
of single level inheritance which inherits the fields only.

#include <iostream>
using namespace std;
class Account {
public:
float salary = 60000;
};
class Programmer: public Account {
public:
float bonus = 5000;
};
int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}

Output:

Department of CSE, DSCE Page 3


Module 3 – Inheritance & Polymorphism

Salary: 60000
Bonus: 5000

In the above example, Employee is the base class and Programmer is the derived class.

C++ Single Level Inheritance Example: Inheriting Methods


Let's see another example of inheritance in C++ which inherits methods only.

#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking...";
}
};
int main(void) {
Dog d1;
d1.eat();
d1.bark();
return 0;
}

Output:

Eating...
Barking...

Department of CSE, DSCE Page 4


Module 3 – Inheritance & Polymorphism

Let's see a simple example.

#include <iostream>
using namespace std;
class A
{
int a = 4;
int b = 5;
public:
int mul()
{
int c = a*b;
return c;
}
};

class B : private A
{
public:
void display()
{
int result = mul();
std::cout <<"Multiplication of a and b is : "<<result<< std::endl;
}
};
int main()
{
B b;
b.display();

return 0;
}

Output:

Department of CSE, DSCE Page 5


Module 3 – Inheritance & Polymorphism

Multiplication of a and b is : 20

In the above example, class A is privately inherited. Therefore, the mul() function of class 'A'
cannot be accessed by the object of class B. It can only be accessed by the member function of
class B.

How to make a Private Member Inheritable


The private member is not inheritable. If we modify the visibility mode by making it public, but
this takes away the advantage of data hiding.

C++ introduces a third visibility modifier, i.e., protected. The member which is declared as
protected will be accessible to all the member functions within the class as well as the class
immediately derived from it.

Visibility modes can be classified into three categories:

• Public: When the member is declared as public, it is accessible to all the functions of the program.
• Private: When the member is declared as private, it is accessible within the class only.
• Protected: When the member is declared as protected, it is accessible within its own class as well
as the class immediately derived from it.

Visibility of Inherited Members

Base class visibility Derived class visibility

Public Private Protected

Private Not Inherited Not Inherited Not Inherited

Department of CSE, DSCE Page 6


Module 3 – Inheritance & Polymorphism

Protected Protected Private Protected

Public Public Private Protected

C++ Multilevel Inheritance


Multilevel inheritance is a process of deriving a class from another derived class.

C++ Multi Level Inheritance Example


When one class inherits another class which is further inherited by another class, it is known as
multi level inheritance in C++. Inheritance is transitive so the last derived class acquires all the
members of all its base classes.

Let's see the example of multi level inheritance in C++.

Department of CSE, DSCE Page 7


Module 3 – Inheritance & Polymorphism

#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking..."<<endl;
}
};
class BabyDog: public Dog
{
public:
void weep() {
cout<<"Weeping...";
}
};
int main(void) {
BabyDog d1;
d1.eat();
d1.bark();
d1.weep();
return 0;
}

Output:

Eating...
Barking...

Department of CSE, DSCE Page 8


Module 3 – Inheritance & Polymorphism

Weeping...

C++ Multiple Inheritance


Multiple inheritance is the process of deriving a new class that inherits the attributes from two or
more classes.

Syntax of the Derived class:

class D : visibility B-1, visibility B-2, ?


{
// Body of the class;
}

Let's see a simple example of multiple inheritance.

#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a(int n)
{
a = n;

Department of CSE, DSCE Page 9


Module 3 – Inheritance & Polymorphism

}
};

class B
{
protected:
int b;
public:
void get_b(int n)
{
b = n;
}
};
class C : public A,public B
{
public:
void display()
{
std::cout << "The value of a is : " <<a<< std::endl;
std::cout << "The value of b is : " <<b<< std::endl;
cout<<"Addition of a and b is : "<<a+b;
}
};
int main()
{
C c;
c.get_a(10);
c.get_b(20);
c.display();

return 0;
}

Output:

Department of CSE, DSCE Page 10


Module 3 – Inheritance & Polymorphism

The value of a is : 10
The value of b is : 20
Addition of a and b is : 30

In the above example, class 'C' inherits two base classes 'A' and 'B' in a public mode.

Ambiguity Resolution in Inheritance


Ambiguity can be occurred in using the multiple inheritance when a function with the same name
occurs in more than one base class.

Let's understand this through an example:

#include <iostream>
using namespace std;
class A
{
public:
void display()
{
std::cout << "Class A" << std::endl;
}
};
class B
{
public:
void display()
{
std::cout << "Class B" << std::endl;
}
};
class C : public A, public B
{
void view()
{

Department of CSE, DSCE Page 11


Module 3 – Inheritance & Polymorphism

display();
}
};
int main()
{
C c;
c.display();
return 0;
}

Output:

error: reference to 'display' is ambiguous


display();

• The above issue can be resolved by using the class resolution operator with the function. In the
above example, the derived class code can be rewritten as:

class C : public A, public B


{
void view()
{
A :: display(); // Calling the display() function of class A.
B :: display(); // Calling the display() function of class B.

}
};

An ambiguity can also occur in single inheritance.

Consider the following situation:

class A
{
public:
void display()

Department of CSE, DSCE Page 12


Module 3 – Inheritance & Polymorphism

{
cout<<?Class A?;
}
};
class B
{
public:
void display()
{
cout<<?Class B?;
}
};

In the above case, the function of the derived class overrides the method of the base class.
Therefore, call to the display() function will simply call the function defined in the derived class.
If we want to invoke the base class function, we can use the class resolution operator.

int main()
{
B b;
b.display(); // Calling the display() function of B class.
b.B :: display(); // Calling the display() function defined in B class.
}

C++ Hybrid Inheritance


Hybrid inheritance is a combination of more than one type of inheritance.

Department of CSE, DSCE Page 13


Module 3 – Inheritance & Polymorphism

Let's see a simple example:

#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a()
{
std::cout << "Enter the value of 'a' : " << std::endl;
cin>>a;
}
};

class B : public A
{
protected:
int b;
public:

Department of CSE, DSCE Page 14


Module 3 – Inheritance & Polymorphism

void get_b()
{
std::cout << "Enter the value of 'b' : " << std::endl;
cin>>b;
}
};
class C
{
protected:
int c;
public:
void get_c()
{
std::cout << "Enter the value of c is : " << std::endl;
cin>>c;
}
};

class D : public B, public C


{
protected:
int d;
public:
void mul()
{
get_a();
get_b();
get_c();
std::cout << "Multiplication of a,b,c is : " <<a*b*c<< std::endl;
}
};
int main()
{

Department of CSE, DSCE Page 15


Module 3 – Inheritance & Polymorphism

D d;
d.mul();
return 0;
}

Output:

Enter the value of 'a' :


10
Enter the value of 'b' :
20
Enter the value of c is :
30
Multiplication of a,b,c is : 6000

C++ Hierarchical Inheritance


Hierarchical inheritance is defined as the process of deriving more than one class from a base class.

Syntax of Hierarchical inheritance:

class A
{
// body of the class A.
}
class B : public A

Department of CSE, DSCE Page 16


Module 3 – Inheritance & Polymorphism

{
// body of class B.
}
class C : public A
{
// body of class C.
}
class D : public A
{
// body of class D.
}

Let's see a simple example:

#include <iostream>
using namespace std;
class Shape // Declaration of base class.
{
public:
int a;
int b;
void get_data(int n,int m)
{
a= n;
b = m;
}
};
class Rectangle : public Shape // inheriting Shape class
{
public:
int rect_area()
{
int result = a*b;
return result;

Department of CSE, DSCE Page 17


Module 3 – Inheritance & Polymorphism

}
};
class Triangle : public Shape // inheriting Shape class
{
public:
int triangle_area()
{
float result = 0.5*a*b;
return result;
}
};
int main()
{
Rectangle r;
Triangle t;
int length,breadth,base,height;
std::cout << "Enter the length and breadth of a rectangle: " << std::endl;
cin>>length>>breadth;
r.get_data(length,breadth);
int m = r.rect_area();
std::cout << "Area of the rectangle is : " <<m<< std::endl;
std::cout << "Enter the base and height of the triangle: " << std::endl;
cin>>base>>height;
t.get_data(base,height);
float n = t.triangle_area();
std::cout <<"Area of the triangle is : " << n<<std::endl;
return 0;
}

Output:

Enter the length and breadth of a rectangle:


23
20

Department of CSE, DSCE Page 18


Module 3 – Inheritance & Polymorphism

Area of the rectangle is : 460


Enter the base and height of the triangle:
2
5
Area of the triangle is : 5

Polymorphism
The term "Polymorphism" is the combination of "poly" + "morphs" which means many forms. It
is a greek word. In object-oriented programming, we use 3 main concepts: inheritance,
encapsulation, and polymorphism.

Real Life Example of Polymorphism

Let's consider a real-life example of polymorphism. A lady behaves like a teacher in a classroom,
mother or daughter in a home and customer in a market. Here, a single person is behaving
differently according to the situations.

There are two types of polymorphism in C++:

Department of CSE, DSCE Page 19


Module 3 – Inheritance & Polymorphism

• Compile time polymorphism: The overloaded functions are invoked by matching the type
and number of arguments. This information is available at the compile time and, therefore,
compiler selects the appropriate function at the compile time. It is achieved by function
overloading and operator overloading which is also known as static binding or early
binding. Now, let's consider the case where function name and prototype is same.

class A // base class declaration.


{
int a;
public:
void display()
{
cout<< "Class A ";
}
};
class B : public A // derived class declaration.
{
int b;
public:
void display()
{
cout<<"Class B";
}
};

In the above case, the prototype of display() function is the same in both the base and derived
class. Therefore, the static binding cannot be applied. It would be great if the appropriate function
is selected at the run time. This is known as run time polymorphism.

• Run time polymorphism: Run time polymorphism is achieved when the object's method
is invoked at the run time instead of compile time. It is achieved by method overriding
which is also known as dynamic binding or late binding.

Differences b/w compile time and run time polymorphism.

Department of CSE, DSCE Page 20


Module 3 – Inheritance & Polymorphism

Compile time polymorphism Run time polymorphism

The function to be invoked is known at the The function to be invoked is known at


compile time. the run time.

It is also known as overloading, early binding It is also known as overriding, Dynamic


and static binding. binding and late binding.

Overloading is a compile time polymorphism Overriding is a run time polymorphism


where more than one method is having the where more than one method is having
same name but with the different number of the same name, number of parameters
parameters or the type of the parameters. and the type of the parameters.

It is achieved by function overloading and It is achieved by virtual functions and


operator overloading. pointers.

It provides fast execution as it is known at the It provides slow execution as it is known


compile time. at the run time.

It is less flexible as mainly all the things It is more flexible as all the things execute
execute at the compile time. at the run time.

C++ Runtime Polymorphism Example

Let's see a simple example of run time polymorphism in C++.

// an example without the virtual keyword.

#include <iostream>
using namespace std;
class Animal {
public:
void eat(){
cout<<"Eating...";
}
};
class Dog: public Animal
{
public:
void eat()

Department of CSE, DSCE Page 21


Module 3 – Inheritance & Polymorphism

{ cout<<"Eating bread...";
}
};
int main(void) {
Dog d = Dog();
d.eat();
return 0;
}

Output:

Eating bread...

C++ Run time Polymorphism Example: By using two derived class

Let's see another example of run time polymorphism in C++ where we are having two derived
classes.

// an example with virtual keyword.

#include <iostream>
using namespace std;
class Shape { // base class
public:
virtual void draw(){ // virtual function
cout<<"drawing..."<<endl;
}
};
class Rectangle: public Shape // inheriting Shape class.
{
public:
void draw()
{
cout<<"drawing rectangle..."<<endl;
}
};

Department of CSE, DSCE Page 22


Module 3 – Inheritance & Polymorphism

class Circle: public Shape // inheriting Shape class.

{
public:
void draw()
{
cout<<"drawing circle..."<<endl;
}
};
int main(void) {
Shape *s; // base class pointer.
Shape sh; // base class object.
Rectangle rec;
Circle cir;
s=&sh;
s->draw();
s=&rec;
s->draw();
s=?
s->draw();
}

Output:

drawing...
drawing rectangle...
drawing circle...

Runtime Polymorphism with Data Members

Runtime Polymorphism can be achieved by data members in C++. Let's see an example where we
are accessing the field by reference variable which refers to the instance of derived class.

#include <iostream>
using namespace std;

Department of CSE, DSCE Page 23


Module 3 – Inheritance & Polymorphism

class Animal { // base class declaration.


public:
string color = "Black";
};
class Dog: public Animal // inheriting Animal class.
{
public:
string color = "Grey";
};
int main(void) {
Animal d= Dog();
cout<<d.color;
}

Output:

Black

C++ Overloading (Function and Operator)

If we create two or more members having the same name but different in number or type of
parameter, it is known as C++ overloading. In C++, we can overload:

o methods,
o constructors, and
o indexed properties

It is because these members have parameters only.

Types of overloading in C++ are:

Department of CSE, DSCE Page 24


Module 3 – Inheritance & Polymorphism

• Function overloading
• Operator overloading

C++ Function Overloading

Function Overloading is defined as the process of having two or more function with the same
name, but different in parameters is known as function overloading in C++. In function
overloading, the function is redefined by using either different types of arguments or a different
number of arguments. It is only through these differences compiler can differentiate between the
functions.

The advantage of Function overloading is that it increases the readability of the program because
you don't need to use different names for the same action.

C++ Function Overloading Example

Let's see the simple example of function overloading where we are changing number of arguments
of add() method.

// program of function overloading when number of arguments vary.

#include <iostream>
using namespace std;
class Cal {
public:
static int add(int a,int b){
return a + b;
}
static int add(int a, int b, int c)

Department of CSE, DSCE Page 25


Module 3 – Inheritance & Polymorphism

{
return a + b + c;
}
};
int main(void) {
Cal C; // class object declaration.
cout<<C.add(10, 20)<<endl;
cout<<C.add(12, 20, 23);
return 0;
}

Output:

30
55

Let's see the simple example when the type of the arguments vary.

// Program of function overloading with different types of arguments.

#include<iostream>
using namespace std;
int mul(int,int);
float mul(float,int);

int mul(int a,int b)


{
return a*b;
}
float mul(double x, int y)
{
return x*y;
}
int main()

Department of CSE, DSCE Page 26


Module 3 – Inheritance & Polymorphism

{
int r1 = mul(6,7);
float r2 = mul(0.2,3);
std::cout << "r1 is : " <<r1<< std::endl;
std::cout <<"r2 is : " <<r2<< std::endl;
return 0;
}

Output:

r1 is : 42
r2 is : 0.6

Function Overloading and Ambiguity

When the compiler is unable to decide which function is to be invoked among the overloaded
function, this situation is known as function overloading.

When the compiler shows the ambiguity error, the compiler does not run the program.

Causes of Function Overloading:

• Type Conversion.
• Function with default arguments.
• Function with pass by reference.

• Type Conversion:

Department of CSE, DSCE Page 27


Module 3 – Inheritance & Polymorphism

Let's see a simple example.

#include<iostream>
using namespace std;
void fun(int);
void fun(float);
void fun(int i)
{
std::cout << "Value of i is : " <<i<< std::endl;
}
void fun(float j)
{
std::cout << "Value of j is : " <<j<< std::endl;
}
int main()
{
fun(12);
fun(1.2);
return 0;
}

The above example shows an error "call of overloaded 'fun(double)' is ambiguous". The fun(10)
will call the first function. The fun(1.2) calls the second function according to our prediction. But,
this does not refer to any function as in C++, all the floating point constants are treated as double
not as a float. If we replace float to double, the program works. Therefore, this is a type conversion
from float to double.

• Function with Default Arguments

Let's see a simple example.

#include<iostream>
using namespace std;
void fun(int);
void fun(int,int);
void fun(int i)

Department of CSE, DSCE Page 28


Module 3 – Inheritance & Polymorphism

{
std::cout << "Value of i is : " <<i<< std::endl;
}
void fun(int a,int b=9)
{
std::cout << "Value of a is : " <<a<< std::endl;
std::cout << "Value of b is : " <<b<< std::endl;
}
int main()
{
fun(12);

return 0;
}

The above example shows an error "call of overloaded 'fun(int)' is ambiguous". The fun(int a, int
b=9) can be called in two ways: first is by calling the function with one argument, i.e., fun(12) and
another way is calling the function with two arguments, i.e., fun(4,5). The fun(int i) function is
invoked with one argument. Therefore, the compiler could not be able to select among fun(int i)
and fun(int a,int b=9).

• Function with pass by reference

Let's see a simple example.

#include <iostream>
using namespace std;
void fun(int);
void fun(int &);
int main()
{
int a=10;
fun(a); // error, which f()?
return 0;
}
void fun(int x)

Department of CSE, DSCE Page 29


Module 3 – Inheritance & Polymorphism

{
std::cout << "Value of x is : " <<x<< std::endl;
}
void fun(int &b)
{
std::cout << "Value of b is : " <<b<< std::endl;
}

The above example shows an error "call of overloaded 'fun(int&)' is ambiguous". The first
function takes one integer argument and the second function takes a reference parameter as an
argument. In this case, the compiler does not know which function is needed by the user as there
is no syntactical difference between the fun(int) and fun(int &).

C++ Operators Overloading

Operator overloading is a compile-time polymorphism in which the operator is overloaded to


provide the special meaning to the user-defined data type. Operator overloading is used to overload
or redefines most of the operators available in C++. It is used to perform the operation on the user-
defined data type. For example, C++ provides the ability to add the variables of the user-defined
data type that is applied to the built-in data types.

The advantage of Operators overloading is to perform different operations on the same operand.

Operator that cannot be overloaded are as follows:

• Scope operator (::)


• Sizeof
• member selector(.)
• member pointer selector(*)
• ternary operator(?:)

Syntax of Operator Overloading

return_type class_name : : operator op(argument_list)

Department of CSE, DSCE Page 30


Module 3 – Inheritance & Polymorphism

{
// body of the function.
}

Where the return type is the type of value returned by the function.

class_name is the name of the class.

operator op is an operator function where op is the operator being overloaded, and the operator is
the keyword.

Rules for Operator Overloading

• Existing operators can only be overloaded, but the new operators cannot be overloaded.
• The overloaded operator contains atleast one operand of the user-defined data type.
• We cannot use friend function to overload certain operators. However, the member
function can be used to overload those operators.
• When unary operators are overloaded through a member function take no explicit
arguments, but, if they are overloaded by a friend function, takes one argument.
• When binary operators are overloaded through a member function takes one explicit
argument, and if they are overloaded through a friend function takes two explicit
arguments.

C++ Operators Overloading Example

Let's see the simple example of operator overloading in C++. In this example, void operator ++ ()
operator function is defined (inside Test class).

// program to overload the unary operator ++.

#include <iostream>
using namespace std;
class Test
{
private:
int num;
public:
Test(): num(8){}

Department of CSE, DSCE Page 31


Module 3 – Inheritance & Polymorphism

void operator ++() {


num = num+2;
}
void Print() {
cout<<"The Count is: "<<num;
}
};
int main()
{
Test tt;
++tt; // calling of a function "void operator ++()"
tt.Print();
return 0;
}

Output:

The Count is: 10

Let's see a simple example of overloading the binary operators.

// program to overload the binary operators.

#include <iostream>
using namespace std;
class A
{

int x;
public:
A(){}
A(int i)
{
x=i;
}

Department of CSE, DSCE Page 32


Module 3 – Inheritance & Polymorphism

void operator+(A);
void display();
};

void A :: operator+(A a)
{

int m = x+a.x;
cout<<"The result of the addition of two objects is : "<<m;

}
int main()
{
A a1(5);
A a2(4);
a1+a2;
return 0;
}

Output:

The result of the addition of two objects is : 9

C++ Function Overriding

If derived class defines same function as defined in its base class, it is known as function overriding
in C++. It is used to achieve runtime polymorphism. It enables you to provide specific
implementation of the function which is already provided by its base class.

C++ Function Overriding Example

Let's see a simple example of Function overriding in C++. In this example, we are overriding the
eat() function.

#include <iostream>

Department of CSE, DSCE Page 33


Module 3 – Inheritance & Polymorphism

using namespace std;


class Animal {
public:
void eat(){
cout<<"Eating...";
}
};
class Dog: public Animal
{
public:
void eat()
{
cout<<"Eating bread...";
}
};
int main(void) {
Dog d = Dog();
d.eat();
return 0;
}

Output:

Eating bread...

C++ virtual function


• A C++ virtual function is a member function in the base class that you redefine in a
derived class. It is declared using the virtual keyword.
• It is used to tell the compiler to perform dynamic linkage or late binding on the function.
• There is a necessity to use the single pointer to refer to all the objects of the different
classes. So, we create the pointer to the base class that refers to all the derived objects.
But, when base class pointer contains the address of the derived class object, always

Department of CSE, DSCE Page 34


Module 3 – Inheritance & Polymorphism

executes the base class function. This issue can only be resolved by using the 'virtual'
function.
• A 'virtual' is a keyword preceding the normal declaration of a function.
• When the function is made virtual, C++ determines which function is to be invoked at
the runtime based on the type of the object pointed by the base class pointer.

Late binding or Dynamic linkage

In late binding function call is resolved during runtime. Therefore compiler determines the type of
object at runtime, and then binds the function call.

Rules of Virtual Function

• Virtual functions must be members of some class.


• Virtual functions cannot be static members.
• They are accessed through object pointers.
• They can be a friend of another class.
• A virtual function must be defined in the base class, even though it is not used.
• The prototypes of a virtual function of the base class and all the derived classes must
be identical. If the two functions with the same name but different prototypes, C++ will
consider them as the overloaded functions.
• We cannot have a virtual constructor, but we can have a virtual destructor
• Consider the situation when we don't use the virtual keyword.

#include <iostream>
using namespace std;
class A
{
int x=5;
public:
void display()
{
std::cout << "Value of x is : " << x<<std::endl;

Department of CSE, DSCE Page 35


Module 3 – Inheritance & Polymorphism

}
};
class B: public A
{
int y = 10;
public:
void display()
{
std::cout << "Value of y is : " <<y<< std::endl;
}
};
int main()
{
A *a;
B b;
a = &b;
a->display();
return 0;
}

Output:

Value of x is : 5

In the above example, * a is the base class pointer. The pointer can only access the base class
members but not the members of the derived class. Although C++ permits the base pointer to point
to any object derived from the base class, it cannot directly access the members of the derived
class. Therefore, there is a need for virtual function which allows the base pointer to access the
members of the derived class.

C++ virtual function Example

Let's see the simple example of C++ virtual function used to invoked the derived class in a
program.

#include <iostream>
{

Department of CSE, DSCE Page 36


Module 3 – Inheritance & Polymorphism

public:
virtual void display()
{
cout << "Base class is invoked"<<endl;
}
};
class B:public A
{
public:
void display()
{
cout << "Derived Class is invoked"<<endl;
}
};
int main()
{
A* a; //pointer of base class
B b; //object of derived class
a = &b;
a->display(); //Late Binding occurs
}

Output:

Derived Class is invoked

Pure Virtual Function

• A virtual function is not used for performing any task. It only serves as a placeholder.
• When the function has no definition, such function is known as "do-nothing" function.
• The "do-nothing" function is known as a pure virtual function. A pure virtual
function is a function declared in the base class that has no definition relative to the
base class.
• A class containing the pure virtual function cannot be used to declare the objects of its
own, such classes are known as abstract base classes.

Department of CSE, DSCE Page 37


Module 3 – Inheritance & Polymorphism

• The main objective of the base class is to provide the traits to the derived classes and
to create the base pointer used for achieving the runtime polymorphism.

Pure virtual function can be defined as:

virtual void display() = 0;

Let's see a simple example:

#include <iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0;
};
class Derived : public Base
{
public:
void show()
{
std::cout << "Derived class is derived from the base class." << std::endl;
}
};
int main()
{
Base *bptr;
//Base b;
Derived d;
bptr = &d;
bptr->show();
return 0;
}

Output:

Department of CSE, DSCE Page 38


Module 3 – Inheritance & Polymorphism

Derived class is derived from the base class.

In the above example, the base class contains the pure virtual function. Therefore, the base class
is an abstract base class. We cannot create the object of the base class.

Department of CSE, DSCE Page 39

You might also like