Pointer and Polymorphism in C++
Pointer and Polymorphism in C++
Pointer and Polymorphism in C++
Concepts of pointer:
Pointer-A pointer variable that holds the address of another pointer
variable is called pointer.
Or
Data type which holds the address of other data types.
The dereference operator (*) helps us get the value that has
been stored in a memory address.
Or
For example:
Example :
#include <iostream>
using namespace std;
int main()
{
int x = 27;
int *ip;
ip = &x;
cout << "Value of x is : ";
cout << x << endl;
cout << "Value of ip is : ";
cout << ip<< endl;
cout << "Value of *ip is : ";
cout << *ip << endl;
return 0;
}
Pointer Arithmetic-
Pointer arithmetic means performing arithmetic operations on
pointers. It refers to the operations that are valid to perform on
pointers.
Following are the arithmetic operations valid on pointers
in C++:
1. Incrementing and Decrementing Pointers.
2. Addition of Constant to Pointers.
3. Subtraction of Constant from Pointers.
4. Subtraction of Two Pointers of the Same Type.
5. Comparison of Pointers.
Example-
Pointer increment-
Int main()
{
Int a=10;
Int *ptr=&a;
Cout<<ptr;
Ptr++;
Cout<<ptr;
}
Pointer to objects-
A pointer to an object acts the same as Pointer to a
variable. But here, in place of the address of the variable,
address of the object is stored. In the main function, when an
object is created to a class, a pointer variable is declared in the
same manner as we declared for the variable, and it will store
the object's address. For creating a pointer to an object, we
should not use data type for the Pointer. Instead, we need
to use the class name for the object pointer. If we want to
use a member function in the class using the Pointer in the
main function, then we need to use the -> symbol,
as shown in the below example.
Example:
using namespace std;
class Rectangle
{
private:
int length;
int breadth;
public:
Rectangle(int l, int b)
{
length=l;
breadth=b;
}
int getArea()
{
return 2*length*breadth;
}
};
int main()
// creating an object of
Rectangle
Rectangle var1(10,30);
//creating a pointer for the object
using class name as data type
Rectangle* ptr = &var1;
//calling the member
function using -> symbol
int area = ptr->getArea();
cout<<"Area of rectangle is: "<<area;
return 0;
}
This pointer-
Example-
Class test
{ private: int a;
Public:
void set_a(int a)
{ this->a=a; }
Void print_a()
{ cout<<”a=”<<a;
}
};
Int main()
{ text xobj;
Int a=5;
Xobj.set_a(a);
Xobj.print_a();
}
Pointer to derived class
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 a pointer to the derived type.
In this pointer base class is owned by the base class but points
to the derived class object. Similarly, it works with derived class
pointer, values are changed.
Example #1
#include<iostream.h>
class base
public:
int n1;
void show()
{
cout<<”\nn1 = “<<n1;
};
public:
int n2;
void show()
cout<<”\nn1 = “<<n1;
cout<<”\nn2 = “<<n2;
};
int main()
base b;
base *bptr; //base pointer
bptr->show();
derive d;
cout<<”\n”;
bptr->show();
return 0;
Output:
n1 = 23
n1=63
Introduction to polymorphism: -
For example,
Types of Polymorphism
Compile-time Polymorphism
Runtime Polymorphism
function overloading –
Two functions having the same name if they have different
parameters .(either types or number of arguments).
OR
class Test
{
public:
// Function with
1 int parameter
void func(int x)
{
cout << "value of x is " << x << endl;
}
// Driver code
int main()
{
test obj1;
// Function being called depends on the
parameters passed func() is called with int value
obj1.func(7);
// func() is called
with double value
obj1.func(9.132);
// func() is called
with 2 int values
obj1.func(85, 64);
Operator Overloading
Class demo
{ int x;
Public: void getdata()
{ cout<<”enter n:”;
Cin>>x;
}
Void putdata()
{ cout<<x;}
Void operator++()
{ x=x+1; }
};
int main()
{ demo aa;
aa.getdata();
cout<<”original value=”;
aa.putdata();
++aa;
Cout<<”value after increment=”;
aa.putdata();
}
Class demo
{ int x;
Public: void getdata()
{ cout<<”enter n:”;
Cin>>x;
}
Void putdata()
{ cout<<x;}
demo operator+(demo bb)
{ demo cc;
cc.a=a+bb.a;
return cc; }
};
int main()
{ demo aa,bb,cc;
aa.getdata();
bb.getdata();
cc=aa+bb;
aa.putdata();
bb.putdata();
cc.putdata();
}
Or
Syntax
Example-
#include <iostream>
class Shape {
};
float a;
public:
Square(float l) {
a = l;
float cal_Area() {
};
float r;
public:
Circle(float x) {
r = x;
float cal_Area() {
return 3.14 * r * r;
};
float l;
float b;
public:
Rectangle(float x, float y) {
l = x;
b = y;
float cal_Area() {
};
Shape * shape;
Square s(3.4);
Circle c(7.8);
shape = & s;
shape = & r;
shape = & c;
std::cout << "The area of rectangle is: " << a2 << std::endl;
std::cout << "The area of circle is: " << a3 << std::endl;
return 0;
It is achieved by function
It is achieved by virtual
overloading and operator
functions and pointers.
overloading.
Compile Time Polymorphism Run time Polymorphism