C++ - Chapter 3
C++ - Chapter 3
Polymorphism
2|P a ge
2. The overloaded operator must have at least one operand
that is of user defined type.
3. We cannot change the basic meaning of an operator.
That is to say, We cannot redefine the plus(+) operator to
subtract one value from the other.
4. Overloaded operators follow the syntax rules of the
original operators. They cannot be overridden.
5. There are some operators that cannot be overloaded like
size of operator(sizeof), membership operator(.), pointer to
member operator(.*), scope resolution operator(::),
conditional operators(?:) etc
6. We cannot use “friend” functions to overload certain
operators.However, member function can be used to
overload them. Friend Functions can not be used with
assignment operator(=), function call operator(()),
subscripting operator([]), class member access operator(->)
etc.
7. Unary operators, overloaded by means of a member
function, take no explicit arguments and return no explicit
values, but, those overloaded by means of a friend function,
take one reference argument (the object of the relevent
class).
8. Binary operators overloaded through a member function
take one explicit argument and those which are overloaded
through a friend function take two explicit arguments.
9. When using binary operators overloaded through a
member function, the left hand operand must be an object
of the relevant class.
3|P a ge
10. Binary arithmetic operators such as +,-,* and / must
explicitly return a value. They must not attempt to change
their own arguments.
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
count1.display();
return 0;
}
Output:
Count: 6
Here, when we use ++count1;, the void operator ++ () is
called.
This increases the value attribute for the object count1 by 1.
5|P a ge
Binary Operators Overloading in C++
The binary operators take two arguments and following are
the examples of Binary operators.
You use binary operators very frequently like addition (+)
operator, subtraction (-) operator and division (/) operator.
Example 4: C++ Binary Operator Overloading
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
// Constructor to initialize real and imag to 0
Complex() : real(0), imag(0) {}
void input() {
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag;
}
void output() {
if (imag < 0)
cout << "Output Complex number: " << real <<
imag << "i";
else
cout << "Output Complex number: " << real << "+"
<< imag << "i";
}
};
int main() {
Complex complex1, complex2, result;
return 0;
}
Output:
Enter first complex number:
Enter real and imaginary parts respectively: 9 5
Enter second complex number:
Enter real and imaginary parts respectively: 7 6
Output Complex number: 16+11i
8|P a ge
You can clearly see that above process results in
duplication of same code 3 times.
This increases the chances of error and data redundancy.
To avoid this type of situation, inheritance is used.
If we create a class Vehicle and write these three functions
in it and inherit the rest of the classes from the vehicle
class, then we can simply avoid the duplication of data and
increase re-usability.
Look at the below diagram in which the three classes are
inherited from vehicle class:
10 | P a g e
Private mode: If we derive a sub class from a Private base
class. Then both public member and protected members of
the base class will become Private in derived class.
We can summarize the different access types according to -
who can access them in the following way –
Access public protected private
11 | P a g e
1. Single Inheritance:
In single inheritance, a class is allowed to inherit from only
one class. i.e. one sub class is inherited by one base class
only.
Syntax:
class subclass_name : access_mode base_class
{
//body of subclass
};
12 | P a g e
// C++ program to explain Single inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
Output:
This is a vehicle
13 | P a g e
2.Multiple Inheritance:
Multiple Inheritance is a feature of C++ where a class can
inherit from more than one classes. i.e one sub class is
inherited from more than one base classes.
Syntax:
class subclass_name : access_mode base_class1,
access_mode base_class2, ....
{
//body of subclass
};
Here, the number of base classes will be separated by a
comma (‘, ‘) and access mode for every base class must be
specified.
// C++ program to explain multiple inheritance
#include <iostream>
using namespace std;
14 | P a g e
{
cout << "This is a Vehicle" << endl;
}
};
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
Output:
This is a Vehicle
This is a 4 wheeler Vehicle
15 | P a g e
3.Multilevel Inheritance:
In this type of inheritance, a derived class is created from
another derived class.
// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
16 | P a g e
class fourWheeler: public Vehicle
{ public:
fourWheeler()
{
cout<<"Objects with 4 wheels are vehicles"<<endl;
}
};
// sub class derived from two base classes
class Car: public fourWheeler{
public:
car()
{
cout<<"Car has 4 Wheels"<<endl;
}
};
// main function
int main()
{
//creating object of sub class will
//invoke the constructor of base classes
Car obj;
return 0;
}
output:
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
17 | P a g e
4.Hierarchical Inheritance:
In this type of inheritance, more than one sub class is
inherited from a single base class. i.e. more than one
derived class is created from a single base class.
// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
};
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base class
Car obj1;
Bus obj2;
return 0;
}
Output:
This is a Vehicle
This is a Vehicle
#include <iostream>
using namespace std;
// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
//base class
20 | P a g e
class Fare
{
public:
Fare()
{
cout<<"Fare of Vehicle\n";
}
};
};
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base class
Bus obj2;
return 0;
}
21 | P a g e
Output:
This is a Vehicle
Fare of Vehicle
22 | P a g e
1.Compile time polymorphism:
This type of polymorphism is achieved by function
overloading or operator overloading.
Function Overloading:
When there are multiple functions with same name but
different parameters then these functions are said to be
overloaded.
Functions can be overloaded by change in number of
arguments or/and change in type of arguments.
Operator Overloading:
C++ also provide option to overload operators.
For example, we can make the operator (‘+’) for string
class to concatenate two strings.
We know that this is the addition operator whose task is to
add two operands.
23 | P a g e
So a single operator ‘+’ when placed between integer
operands , adds them and when placed between string
operands, concatenates them.
2.Runtime polymorphism:
This type of polymorphism is achieved by Function
Overriding.
Function overriding on the other hand occurs when a
derived class has a definition for one of the member
functions of the base class.
That base function is said to be overridden.
// C++ program for function overloading
#include <bits/stdc++.h>
int main() {
Geeks obj1;
26 | P a g e
As we can see from the figure that data members/function
of class A are inherited twice to class D.
One through class B and second through class C.
27 | P a g e
When any data / function member of class A is accessed by
an object of class D, ambiguity arises as to which
data/function member would be called? One inherited
through B or the other inherited through C.
This confuses compiler and it displays error.
Syntax for Virtual Base Classes:
Syntax 1:
class B : virtual public A
{
};
Syntax 2:
class C : public virtual A
{
};
Example
#include <iostream>
using namespace std;
class A {
public:
int a;
A() // constructor
{
a = 10;
}
};
int main()
{
D object; // object creation of class d
cout << "a = " << object.a << endl;
return 0;
}
Output:
a = 10
Explanation :The class A has just one data member a which
is public.
This class is virtually inherited in class B and class C.
Now class B and class C becomes virtual base class and no
duplication of data member a is done.
29 | P a g e
3.8 Pointer to Derived class
We can use pointers not only to the base objects but also to
the objects of derived classes.
Pointers to objects of a base class are type-compatible with
pointers to objects of a derived class.
Therefore, a single pointer variable can be made to point to
objects belonging to different classes.
For example, if B is a base class and D is a derived class
from B, then a pointer declared as a pointer to B can also be
a pointer to D.
Consider the following declarations:
B *cptr; //pointer to class B type variable
B b; //base object
D d; //derived object
cptr = &b; //cptr points to object b
We can make cptr to point to the object d as follows:
Cptr = &d //cptr points to object d
This is perfectly valid with C++ because d is an object
derived from the class B.
However, there is a problem in using cptr to access the
public members of the derived class D.
Using cptr, we can access only those members which are
inherited from B and not the members that originally
belong to D.
In case a member of D has the same name as one of the
members of B, then any references to that member by cptr
will always access the base class member.
30 | P a g e
Although C++ permits a base pointer to point to any object
derived from that base, the pointer cannot be directly used
to access all the members of the derived class.
We may have to use another pointer declared as pointer to
the derived type.
Example:
#include<iostream.h>
class base
{
public:
int n1;
void show()
{
cout<<”\nn1 = “<<n1;
}
};
class derive : public base
{
public:
int n2;
void show()
{
cout<<”\nn1 = “<<n1;
cout<<”\nn2 = “<<n2;
}
};
int main()
{
base b;
31 | P a g e
base *bptr; //base pointer
cout<<”Pointer of base class points to it”;
bptr=&b; //address of base class
bptr->n1=44; //access base class via base pointer
bptr->show();
derive d;
cout<<”\n”;
bptr=&d; //address of derive class
bptr->n1=66; //access derive class via base
pointer
bptr->show();
return 0;
}
Output
Pointer of base class points to it
n1 = 44
Pointer of base class points to derive class
n1=66
32 | P a g e
3.9 Virtual functions and Rules for Virtual function
A virtual function is a member function which is declared
within a base class and is re-defined(Overriden) by a
derived class.
When you refer to a derived class object using a pointer or
a reference to the base class, you can call a virtual function
for that object and execute the derived class’s version of the
function.
-Virtual functions ensure that the correct function is called
for an object, regardless of the type of reference (or
pointer) used for function call.
-They are mainly used to achieve Runtime polymorphism
-Functions are declared with a virtual keyword in base
class.
-The resolving of function call is done at Run-time.
33 | P a g e
5.A class may have virtual destructor but it cannot have a
virtual constructor.
C++ virtual function Example
#include <iostream>
{
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
34 | P a g e
3.10 Pure Virtual functions
A pure virtual function (or abstract function) in C++ is a
virtual function for which we don’t have implementation,
we only declare it.
A pure virtual function is declared by assigning 0 in
declaration.
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.
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;
Example:
#include <iostream>
using namespace std;
class Base
{
public:
35 | P a g e
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:
Derived class is derived from the base class.
36 | P a g e