0% found this document useful (0 votes)
21 views23 pages

Polymorphism C++

The document provides an overview of pointers in C++, including pointer variables, pointers to arrays, and pointers to class members. It explains the 'this' pointer, virtual functions, and pure virtual functions, highlighting their roles in polymorphism and object-oriented programming. Additionally, it illustrates these concepts with code examples demonstrating their usage and behavior.

Uploaded by

aryanchandel1951
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views23 pages

Polymorphism C++

The document provides an overview of pointers in C++, including pointer variables, pointers to arrays, and pointers to class members. It explains the 'this' pointer, virtual functions, and pure virtual functions, highlighting their roles in polymorphism and object-oriented programming. Additionally, it illustrates these concepts with code examples demonstrating their usage and behavior.

Uploaded by

aryanchandel1951
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

polymorphism

Pointers in C++
Pointer variable
• Pointer is a variable that holds a memory address, of another
variable.
int a =
25; a 25 p 1000
int *p; 1000 2000
p = &a;
cout<<"&a:"<<&a; &a:1000
cout<<"p:"<<p; p:1000 *()
cout<<"&p:"<<&p; &p:2000
cout<<"*p:"<<*p; *p:25
*Indicates value
at address
cout<<"*(&a):"<<*(&a); *(&a):25
(*p)++;
cout<<"*p:"<<*p; *p:26
cout<<"a:"<<a; a:26
Pointer to arrays
int main () 0 10 1000
ptr
{
1 20 1002
int arr[5] = {10,20,30,40,50};
int *ptr; 2 30 1004
Also, written as
ptr = arr; 3 40 1006
ptr = &arr[0]; 4 50 1008
for ( int i = 0; i < 5; i++ )
{
cout <<"*(ptr+" << i <<"):";
Output:
cout <<*(ptr + i) << endl;
*(ptr + 0) : 10
}
*(ptr + 1) : 20
return 0;
*(ptr + 2) : 30
}
*(ptr + 3) : 40
*(ptr + 4) : 50
Pointers and objects
• Just like pointers to normal variables and functions, we can have
pointers to class members (variables and methods).
class ABC
{ When accessing
public: members of a class
int a=50; given a pointer to an
}; object, use the
int main() arrow
{ (–>) operator
ABC ob1; instead of the dot
ABC *ptr; operator.
ptr = &ob1;
cout << ob1.a;
cout << ptr->a; // Accessing member with
pointer
Pointers and objects (Cont…)
class demo{
int i;  When a pointer incremented it
public: points to next element of its
demo(int x){ type.
i=x; }  An integer pointer will point to
int getdata(){
return i;}
the next integer.
 The same is true for pointer to
};
int main() objects.
{
demo d[3]={55,66,77};
demo *ptr=d; //similar to
*ptr=&d[0]
for(int i=0;i<3;i++)
{
cout<<ptr->getdata()<<endl;
ptr++;
}
this pointer
class Test
{
this pointer
int mark;  Within member function, the
float spi; members can be accessed directly,
public: without any object or class
void SetData(){ qualification.
mark =
this->mark = 70;
70;  But implicitly members are being
spi == 6.5
this->spi 6.5;
; accessed using this pointer
}
void DisplayData(){
cout << "Mark= "<<mark;
cout << "spi= "<<spi;
}
} ;  When a member function is called,
int main()
it automatically passes a pointer
{
Test o1;
to invoking object.
o1.SetData();

o1.DisplayData();
this pointer(Cont…)
• ‘this’ pointer represent an object that invoke or call a member
function.
• It will point to the object for which member function is called.
• It is automatically passed to a member function when it is called.
• It is also called as implicit argument to all member function.

Note:
 Friend functions can not be accessed using this pointer, because
friends are not members of a class.
 Only member functions have a this pointer.
 A static member function does not have this pointer.
class sample
{
this pointer
int a,b;
public:
(Cont…)
void input(int a,int b){
this->a = a + b;
this->b = a - b; this pointer is used when local
}
variable’s name is same as
void output(){
member’s name.
cout<<"a = "<<a;
cout<<"b = "<<b;
}
};
int main()
{
sample ob1;
int a=5,b=8;
ob1.input(a,b);
ob1.output();
}
Pointer to Derived Class
class Base {
public:
void showBase(){
cout << "Base\n"; }
};
class Derv1 : public Base
{ Derived type casted to
public: base type
void showDerived(){
cout << "Derv1\n"; }
};
int main(){ Base pointer explicitly
Derv1 dv1; casted into derived type
Base* ptr;
ptr = &dv1;
ptr->showBase(); Output:
ptr->showDerived(); // Base
error
((Derv1 *)ptr)->show(); Derv1
}
class base Program
{
public:
int b;
void show()
{
cout<<"\nb="<<b;
}
};
class derived : public base
{
public:
int d;
void show()
{
cout<<"\n b="<<b<<"\n
d="<<d;
}
};
Virtual Function
• A virtual function is a member function that is declared within a
base class and redefined by a derived class.
• To create a virtual function, precede the function's declaration in
the base class with the keyword virtual.
Virtual Function
• When virtual function accessed "normally," it behave just like any
other type of class member function.
• But when it is accessed via a pointer it supports run time
polymorphism.
• Base class and derived class have same function name and base
class pointer is assigned address of derived class object then also
pointer will execute base class function.
• After making virtual function, the compiler will determine which
function to execute at run time on the basis of assigned address to
pointer of base class.
class Base { int main()
public: {
virtual void show(){ Derv1 dv1;
cout << "Base\n"; } Derv2 dv2;
}; Base* ptr;
class Derv1 : public Base ptr = &dv1;
{ ptr-
public: >show();
void show(){ ptr = &dv2;
cout << "Derv1\n"; } ptr-
}; >show();
class Derv2 : public Base }
{
public: Output:
void show(){ Derv1
cout << "Derv2\n"; } Derv2
};
ptr Base
&Derv1 virtual
ptr- show()
>show()
Derv1
ptr
show()
&Derv2
ptr-
>show() Derv2

show()

 When a function is made virtual, C++ determines which function to


use at run time based on the type of object pointed by the base
pointer, rather than the type of pointer .
Rules for virtual base function
1. The virtual functions must be member of any class.
2. They cannot be static members.
3. They are accessed by using object pointers.
4. A virtual function can be a friend of another class.
5. A virtual function in a base class must be defined, even though it
may not be used.
Pure Virtual Function
• A pure virtual function is virtual function that has no definition
within the base class.
Pure virtual functions

• A pure virtual function means ‘do nothing’ function.


• We can say empty function. A pure virtual function has
no definition relative to the base class.
• Programmers have to redefine pure virtual function in
derived class, because it has no definition in base class.
• A class containing pure virtual function cannot be used
to create any direct objects of its own.
• This type of class is also called as abstract class.
Syntax:
virtual void display() = 0;
OR
virtual void display() {}
class Shape{ Program
protected:
float x; This is called abstract class
public:
void getData(){cin >> x;}
virtual float calculateArea() =
0;
};
class Square : public Shape
{
public:
float calculateArea()
{ return x*x; }
};
class Circle : public Shape
{
public:
float calculateArea()
{ return 3.14*x*x; }
};
int main() Program
{
Square s;
Circle c;
cout << "Enter length to calculate the area of a
square:";
s.getData();
cout<<"Area of square: " << s.calculateArea();
cout<<"Enter radius to calculate the area of a
circle: ";
c.getData();
cout << "Area of circle: " << c.calculateArea();

}Output:
Enter length to calculate the area of a square: 10
Area of square: 100
Enter radius to calculate the area of a circle: 9
Area of circle: 254.34
Thank You

You might also like