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

Polymorphism in C

Polymorphism in C++ allows functions and operators to be used in multiple ways, enhancing code reusability. It is categorized into compile-time polymorphism, which includes function and operator overloading, and runtime polymorphism, which involves function overriding and virtual functions. The document provides examples of each type, illustrating how they operate within C++ programming.

Uploaded by

rincejohn80
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)
4 views

Polymorphism in C

Polymorphism in C++ allows functions and operators to be used in multiple ways, enhancing code reusability. It is categorized into compile-time polymorphism, which includes function and operator overloading, and runtime polymorphism, which involves function overriding and virtual functions. The document provides examples of each type, illustrating how they operate within C++ programming.

Uploaded by

rincejohn80
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/ 12

Polymorphism in C++

Polymorphism in C++ is using the same functions or operators


multiple times in multiple ways. It plays an important role in
increasing your code reusability.

What is Polymorphism in C++?


Polymorphism is made up of two words, poly means more than one
and morphs means forms. Therefore, polymorphism means the ability
to take more than one form. This property makes the same entities
such as functions, and operators perform differently in different
scenarios. You can perform the same task in different ways. This
makes polymorphism in C++ an important OOP concept.

Types of Polymorphism in C++


There are two types of polymorphism in the C++ programming
language. These are:

 Compile time Polymorphism


 Runtime Polymorphism
Compile time Polymorphism in C++
In this, the compiler at the compilation stage knows which functions
to execute during the program execution. It matches the overloaded
functions or operators with the number and type of parameters at
the compile time.

The compiler performs compile-time polymorphism in C++ in two


ways:

 Function Overloading
 Operator Overloading

Function Overloading in C++


We already saw how to use the same function with different numbers
and types of parameters for different tasks in the section, C++Function
Overloading.

You are also aware of inheritance in C++ very well. Therefore, we will
now implement the concept of function overloading with parent and
derived classes in C++.

Example of Function Overloading in C++

#include <iostream>

using namespace std;

class DotNetTricks {

public:

void print(int x) {
cout << "DotNetTricks was established in " << x << endl;

};

class ScholarHat : public DotNetTricks {

public:

void print(string message) {

cout << "ScholarHat was founded by the DotNetTricks founder, " <<
message << endl;

};

int main() {

DotNetTricks obj1;

ScholarHat obj2;

obj1.print(2015);

obj2.print("Shailendra Chauhan"); // Calls the overloaded


print(string) in Derived class

return 0;

}
 The print() function with different parameter types is declared in
both the classes, DotNetTricks and ScholarHat.
 The object obj1 of the parent class, DotNetTricks calls the print()
function of the parent class with an int argument.
 The object obj2 of the child class, ScholarHat calls the overloaded
print() function in the derived class with a string argument.
 Hence, we can say that the child class, ScholarHat has overloaded
the print() function of the parent class, DotNetTricks.

Output
DotNetTricks was established in 2015

ScholarHat was founded by the DotNetTricks founder, Shailendra


Chauhan

Operator Overloading in C++


Operator overloading in C++ is basically function overloading, where
different operator functions have the same symbol but different
operands. And, depending on the operands, different operator functions
are executed. The operands here are the user-defined data types like
objects or structures and not the basic data types.
Example of Operator Overloading in C++

#include <iostream>

#include <string>

using namespace std;

// Class to implement operator overloading for concatenating the


strings

class MyString {

public:

char s1[25], s2[25];

// Parameterized Constructor

MyString(char str1[], char str2[])

// Initialize the string to class object

strcpy(this->s1, str1);

strcpy(this->s2, str2);

// + Overload Operator to concatenate the string


void operator+()

cout << "\nConcatenation: " << strcat(s1, s2);

};

int main()

char str1[] = "Welcome to";

char str2[] = "ScholarHat";

// Declaring and initializing the class with the above two strings

MyString overload(str1, str2);

// Call operator function

+overload;

return 0;

In the above code, we have overloaded the + operator in the MyString


class, allowing you to concatenate two MyString objects.
We have concatenated str1 and str2 objects using the + operator, and
the result is stored in the result variable.

Output
Welcome to ScholarHat

Runtime Polymorphism in C++


In this, the compiler at the compilation stage does not know the
functions to execute during the program execution. The function call
is not resolved during compilation, but it is resolved in the run time
based on the object type. It means the function is invoked by seeing
which object is calling it; the parent class object or the derived class
object. This is also known as dynamic binding or late binding.

Runtime Polymorphism in C++ is achieved in two


ways:

 Function Overriding
 Virtual Functions

Function Overriding in C++


In this, the method with the same name, type, and number of
parameters is defined in both the base and derived classes. When the
object of the derived class calls this method, the method in the
derived class gets executed instead of the base class. Therefore, we
can say that the method of the derived class has overridden the
method of the base class.

Example of Function Overriding in C++

#include <iostream>
using namespace std;
class DotNetTricks {
public:
void print() {
cout << "Welcome to DotNetTricks" << endl;
}
};

class ScholarHat : public DotNetTricks {


public:
void print() {
cout << "Welcome to ScholarHat" << endl;
}
};

int main() {
ScholarHat obj1;

// Call the print() function of the ScholarHat class


obj1.print();
return 0;
}

In the above code, the base class Scholarhat overrides the print()
function of the parent class, DotNetTricks. When you create an object
of ScholarHat and call its print() function, it executes the version of the
function defined in the ScholarHat class.

Output
Welcome to ScholarHat
We can even access the overridden function of the base class using the
scope resolution operator :: and the pointer of the base class.

Virtual Function in C++


When we use the pointer of the base class to override the function of
the base class in the derived class, it is not done. Therefore virtual
functions in C++ come into play. If you declare a method in the base
class as virtual and use a pointer of the base class to call that method,
it calls the method of the derived class and not the base class. Thus, the
virtual function in the base class helps the derived class method to
override that function.
Example of Virtual Function in C++

#include <iostream>

using namespace std;

class DotNetTricks {

public:

virtual void print() {

cout << "Welcome to DotNetTricks" << endl;

};

class ScholarHat : public DotNetTricks {

public:

void print() {

cout << "Welcome to ScholarHat" << endl;

};

int main() {

ScholarHat sobj1;

// Pointer of the Base type that points to sobj1


DotNetTricks* ptr = &sobj1;

// Calls the member function of the Derived class

ptr->print();

return 0;

The above C++ code in C++ Editor creates an object of ScholarHat


and uses a pointer of the base class type (DotNetTricks) to
demonstrate function overriding. When ptr->print()is called, it will
invoke the print() function of the ScholarHat class due to the virtual
print() function in the DotNetTricks class.

Output
Welcome to ScholarHat
Difference between Runtime and Compile time
polymorphism

You might also like