Unit 3
Unit 3
Polymorphism
The word polymorphism means having many forms. In simple words, we can
define polymorphism as the ability of a message to be displayed in more than
one form.
Real life example of polymorphism, a person at the same time can have
different characteristic. Like a man at the same time is a father, a husband,
an employee. So the same person posses different behavior in different
situations. This is called polymorphism.
Polymorphism is considered as one of the important features of Object
Oriented Programming.
In C++ polymorphism is mainly divided into two types:
Compile time Polymorphism
Runtime Polymorphism
Compile time Polymorphism
This type of polymorphism is achieved by function overloading or operator
overloading.
When there are multiple functions with the same name but different
parameters, then the functions are said to be overloaded, hence this is known
as Function Overloading.
Functions can be overloaded by changing the number of arguments or/and
changing the type of arguments.
In simple terms, it is a feature of object-oriented programming providing
many functions that have the same name but distinct parameters when
numerous tasks are listed under one function name. There are certain Rules
of Function Overloading that should be followed while overloading a function.
Function Overloading
When there are multiple functions with the same name but different
parameters, then the functions are said to be overloaded, hence this is known
as Function Overloading.
Functions can be overloaded by changing the number of arguments or/and
changing the type of arguments.
In simple terms, it is a feature of object-oriented programming providing
many functions that have the same name but distinct parameters when
numerous tasks are listed under one function name.
There are certain Rules of Function Overloading that should be followed while
overloading a function.
#include <iostream.h>
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
// This is automatically called
// when '+' is used with between
// two Complex objects
Complex operator+(Complex const& obj)
{
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};
// Driver code
int main()
{
Complex c1(10, 5), c2(2, 4);
class Distance {
public:
int feet, inch;
Distance()
{
this->feet = 0;
this->inch = 0;
}
Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}
// Overloading (+) operator to
// perform addition of two distance
// Call by reference
Distance operator+(Distance& d2)
{
// Create an object to return
Distance d3;
d3.feet = this->feet + d2.feet;
d3.inch = this->inch + d2.inch;
It is less flexible as mainly all the It is more flexible as all the things
things execute at the compile time. execute at the run time.
Pointers
Pointers to objects in C++, especially, provide powerful capabilities for memory
management, allowing developers to allocate and deallocate memory
dynamically.
In C++, a pointer is a variable that holds the memory address of another
variable. Pointers allow for efficient memory utilization, as they enable
dynamic memory allocation, deallocation, and indirect access to objects.
When declaring a pointer, the type of the pointer must match the type of the
variable it will be pointing to.
Pointer variable declaration in C++
datatype *var_name;
int *ptr; // Ptr can direct attention to an address that contains int data.
What Is Pointer To Object In C++?
A pointer to an object in C++ is a variable that holds the memory address of
an object. It allows indirect access to the object, providing a way to
manipulate and interact with the object dynamically.
Pointers to objects are particularly useful when dealing with dynamically
allocated memory, enabling flexible memory management and dynamic
object creation.
In C++, when an object is created, it occupies a specific memory location.
Pointers to objects store the address in memory, where the object is stored
rather than the object itself. By using pointers, programmers can access and
modify the object indirectly through the pointer.
#include <iostream>
class MyClass {
public:
void myFunction() {
std::cout << "Hello from MyClass!" << std::endl;
}
};
int main() {
MyClass obj; // Create an object of MyClass
MyClass* ptr = &obj; // Create a pointer to the object
ptr->myFunction(); // Access functions using the pointer
return 0;
}
Output
Hello from MyClass!