0% found this document useful (0 votes)
32 views21 pages

1493266679p6m35 Etext

The document discusses pointers, virtual functions, and polymorphism in C++. It defines pointers as variables that hold memory addresses and can be used to access and manipulate other variables. It describes how pointers can reference objects and derived class objects. It then explains polymorphism and how virtual functions allow objects to exhibit polymorphic behavior at runtime. The document provides examples to illustrate pointers, pointers to objects, pointers to derived classes, and virtual functions.

Uploaded by

Muskan Roy 16
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)
32 views21 pages

1493266679p6m35 Etext

The document discusses pointers, virtual functions, and polymorphism in C++. It defines pointers as variables that hold memory addresses and can be used to access and manipulate other variables. It describes how pointers can reference objects and derived class objects. It then explains polymorphism and how virtual functions allow objects to exhibit polymorphic behavior at runtime. The document provides examples to illustrate pointers, pointers to objects, pointers to derived classes, and virtual functions.

Uploaded by

Muskan Roy 16
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/ 21

1

Module - 35

Pointers, Virtual Function, Polymorphism


Table of Contents

1. Introduction
2. Pointers
3. Pointers to Objects
4. Pointers to Derived Classes
5. Polymorphism
6. Virtual functions
7. Virtual Destructor
8. Abstract class and Pure Virtual Function
9. Summary

C and C++ Programming


Electronic Science
35. Pointers, Virtual Function, Polymorphism
2

Learning outcome –
After studying this module, you will be able to:
1. Study about Pointers
2. Learn about Pointer to Objects
3. Study about Pointers to Derived Classes
4. Polymorphism
5. Learn about Virtual Functions
6. Study about Virtual Destructor
7. Learn about Abstract class and Pure Virtual Function

1. Introduction
The memory of computer is planned as a sequential group of bytes. Every byte in the memory of
the computer has a unique address. In the same way, each and every variable in the program is
accumulated at a unique address. A pointer is a variable that holds the memory address,
generally the location of another variable in the memory. The C++ programming language
permits to have pointers to objects. The pointers that are pointing to the objects are referred to as
Object Pointers. The polymorphism is the capability of objects to take dissimilar forms. The
ability to show the behavior of the variable depending on the condition is a great ability in any
programming language. In previous units, the polymorphism concept is studies by using the
concepts of function overloading and operator overloading. C++ supports a method identified as
virtual function to accomplish run-time polymorphism.

2. Pointe rs
A pointer is a variable that tries to hold a memory address, generally the location of another
variable in memory. Pointers try to provide another approach to contact other data objects. Some
of the arithmetic operators that can be used with pointers are ++, --, +, -, += and -=. At every
instance, when a pointer is incremented by 1, then it tries to point to the memory location of the
next element of the respective base type.
Example:
Suppose, if “ptr” is a pointer of character data type, then “ptr++” will try to increment “ptr” by 1
byte, on the other hand, if “ptr” is an integer pointer, then “ptr++” will try to increment by 4
bytes.

Program to illustrate the usage of arithmetic operations on pointers


#include<iostream>
using namespace std;
int main()
{
int numbers[]={57,74,22,18,90} ;
int *iptr;
int i;
cout<<"Integer array values are "<<endl;
for(i=0;i<5;i++)
C and C++ Programming
Electronic Science
35. Pointers, Virtual Function, Polymorphism
3

cout<<numbers[i]<<endl;
iptr=numbers;
cout<<"Value of iptr = "<<*iptr<<endl;
iptr++;
cout<<"Value of iptr ++ "<<*iptr<<endl;
iptr--;
cout<<"Value of iptr -- "<<*iptr<<endl;
iptr=iptr+2;
cout<<"Value of iptr +2 "<<*iptr<<endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Integer array values are
57
74
22
18
90
Value of iptr = 57
Value of iptr ++ 74
Value of iptr -- 57
Value of iptr +2 22

3. Pointe r to Objects
A pointer can position to objects like that of simple data types and arrays. Sometimes, there may
be a situation, that, how many objects are to be created in the program. So, the solution for such
type of situations is that, the new operator is to be used to create the objects in the program,
when the program is running, because, the new operator tries to returns a pointer to the nameless
objects. In general, it is probable for a pointer to position to a class object. It is known as object
pointer. The Object pointers are of use in creating objects at run time. It is also possible that the
public members of class can be accessible by using the object pointers.

Program to implement the usage of pointer to objects


#include <iostream>
using namespace std;
class first
{
int i;
public:
first(int j)
{
i=j;
}

C and C++ Programming


Electronic Science
35. Pointers, Virtual Function, Polymorphism
4

int get_values()
{
return i;
}
};

int main()
{
first f(88), *fp;
fp = &f;
cout << "Value is "<<fp->get_values();
return 0;
}
When the above code is compiled and executed, it produces the following result:
Value is 88

Program to demonstrate the usage of object pointers


#include <iostream>
using namespace std;
class polygon
{
protected:
int width, height;
public:
void setvalues (int wt, int ht)
{
width=wt;
height=ht;
}
void show()
{
cout<<"Width is "<<width<<" and Height is "<<height<<endl;
}
};
int main ()
{
polygon p1;
polygon *pptr = &p1;
pptr->setvalues (10,8);
pptr->show( );
return 0;
}
When the above code is compiled and executed, it produces the following result:
Width is 10 and Height is 8

C and C++ Programming


Electronic Science
35. Pointers, Virtual Function, Polymorphism
5

Program to illustrate the usage of pointers to objects


#include <iostream>
#include <string>
using namespace std;
class student
{
private:
int seatno;
string name_of_student;
public:
student():seatno(0),name_of_student("")
{
}
student(int sno, string st): seatno(sno),name_of_student(st)
{
}
void accept()
{
cout<<"enter seat no"<<endl;
cin>>seatno;
cout<< “enter name of the student"<<endl;
cin>>name_of_student;
}
void display()
{
cout<<"Seat no is "<<seatno<<" and name of the student is "
<<name_of_student<<endl;
}
};
int main ()
{
student *pstu=new student;
(*pstu).accept();
(*pstu).display();
delete pstu;
return 0;
}
When the above code is compiled and executed, it produces the following result:
enter seat no
10001
enter name of the student
Deepali
Seat no is 10001 and name of the student is Deepali
C and C++ Programming
Electronic Science
35. Pointers, Virtual Function, Polymorphism
6

Program to demonstrate the usage of array of pointers to objects


#include <iostream>
using namespace std;
class First
{
private:
int age;
int weight;
public:
First()
{
age = 1;
weight=5;
}
~First()
{
}
int Get_Age() const
{
return age;
}
int Get_Weight() const
{
return weight;
}
void Set_Age(int a)
{
age = a;
}
};
int main()
{
First * fobject[10];
int i;
First * fobjectpointer;
for (i = 0; i < 10; i++)
{
fobjectpointer = new First;
fobjectpointer->Set_Age(2*i +1);
C and C++ Programming
Electronic Science
35. Pointers, Virtual Function, Polymorphism
7

fobject[i] = fobjectpointer;
}
for (i = 0; i < 10; i++)
cout << "# " << i+1 << ": " << fobject[i]->Get_Age() << endl;
for (i = 0; i < 10; i++)
{
delete fobject[i];
fobject[i] = NULL;
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
# 1: 1
# 2: 3
# 3: 5
# 4: 7
# 5: 9
# 6: 11
# 7: 13
# 8: 15
# 9: 17
# 10: 19

4. Pointe rs to Derived Class


The pointers cannot only be used on objects of the base class, but they can also be used on
objects of derived class also. Since, the pointers to the objects of base class are type compatible
to the pointers to the objects of the derived class, therefore, it is possible that a distinct pointer
variable can be made to point to the objects that belong to different classes. Even though, C++
language permits a pointer of the base class to position to any object that is derived from the base
class, but the pointer cannot be used straightforwardly to access all the members of the derived
class.

Example:
class base_class
{

};
class derived_class : public base_class
{

};
Then, it is possible to write:

C and C++ Programming


Electronic Science
35. Pointers, Virtual Function, Polymorphism
8

base_class *bp1;
derived_class dobj;
bp1 = &dobj;
base_class *bp2 = new derived_class;

Program to implement the usage of pointer to derived class


#include <iostream>
using namespace std;
class Base
{
int a;
public:
void setx(int m)
{
a = m;
}
int geta()
{
return a;
}
};

class Derived : public Base


{
int b;
public:
void setb(int n)
{
b = n;
}
int getb()
{
return b;
}
};

int main()
{
Base *bp;
Base b1;
Derived d1;
bp = &b1;
bp->seta(20);
cout << "Base class object a is " << bp->geta() << endl;
bp = &d1;
bp->seta(40);
C and C++ Programming
Electronic Science
35. Pointers, Virtual Function, Polymorphism
9

d1.setb(16);
cout << "Derived class object a is " << bp->geta() << endl;
cout << "Derived class object b is " << d1.getb() << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Base class object a is 20
Derived class object a is 40
Derived class object b is 16

5. Polymorphism
The Polymorphism is an amalgamation of two Greek words, i.e. poly and morphism. Poly means
“many” and morphism means “form”. The functionality of this aspect resemble by means of its
name. The two forms of polymorphism that is supported by C++ language are Compile-time
Polymorphism and Run-time Polymorphism.

Polymorphism

Compile-time Polymorphis m Run-time Polymorphism

Function ove rloading Ope rator Ove rloading Virtual Function

The process of Operator overloading is accomplished by permitting the operators to work on the
user defined data type with the same style as that of built- in data types. With the help of function
overloading, it is possible to write dissimilar functions by using same function name but with
dissimilar argument lists. The function would carry out different operation depending on the
parameter list in the function call. The overloaded member functions are chosen for invoking by
matching the number of arguments and kind of arguments. This information is identified to the
compiler at the compile time itself and as a result the choice of the suitable function is prepared
at the compile time only. It is must to use the scope resolution operator (::) to state the class
while invoking the functions with the objects of the derived class. But it would be good only if
the suitable member function could be chosen while the program is running. With the help of
inheritance and virtual functions, C++ determines which edition of that function to call. This is
determined at run-time and therefore, it is known as run-time polymorphism. In run-time
polymorphism, the function is related with a particular class to a large extent later on after the
compilation and as a result it is also known as late binding or dynamic binding.

6. Virtual Functions
C and C++ Programming
Electronic Science
35. Pointers, Virtual Function, Polymorphism
10

In the process of Late Binding, the function call is determined at runtime. Therefore, the
compiler decides the object’s type at runtime, and then it connects the task of function call. Late
Binding is also called as Dynamic Binding or Runtime Binding. A Virtual Function is a member
function that is declared within the base class, which is redefined or overrided in the derived
class, and which informs the compiler to carry out Late Binding on this function. When the same
function name is to be used in the base class and derived class, then the function in base class can
be declared as virtual with the help of the virtual keyword prior to the normal declaration. When
a function is declared as virtual, it helps in determining which function is to be used at run time
based on the object’s type that is pointed to by the base pointer. By making the base pointer to
spot to derived objects, it is possible to implement dissimilar versions of the virtual function. The
main advantage of virtual keyword is that it tries to verify if a member function of a class can be
over-ridden in its derived classes or not. The advantage of containing virtual function is that it is
possible to access the function that is defined in derived class by using the pointer variable of
base class. The virtual function tries to apply the “one interface, multiple methods” idea that
bring about polymorphism.

Syntax:
virtual return_type function_name()
{
…..
…..
}
Example:
virtual void display()
{
…..
…..
}

When functions are declared as virtual, the compiler tries to add a data member secretly to the
class. This data member is referred to as a virtual pointer (VPTR). A table called virtual table
(VTBL) holds pointers to all the functions that have been declared as virtual in a class, or any
other classes that are inherited. At any time, a call to a virtual function is made in the C++
program, the compiler tries to generate the code that is used to treat VPTR as the starting address
of an array of pointers to functions. The code of the function call basically indexes into this array
and calls the function that is located at the indexed addresses. The binding of function call at all
times needs these dynamic indexing activities which always take place at run-time. If a call to a
virtual function is made, while take care of the object that is defined in question, as a member of
its base class, then the accurate derived class function will be called. As a result, dynamic
binding is accomplished with the help of virtual functions.

When a function is made active through a pointer to the base class, then:
a) If the function is not defined as virtual, then, a Static Binding is performed at compilation
time. The method of the base class method gets activated.

C and C++ Programming


Electronic Science
35. Pointers, Virtual Function, Polymorphism
11

b) If the function is defined as virtual, then, a Dynamic Binding will be performed at


runtime. The method of the right class will be activated.

Characteristics of Virtual Functions


(1) A virtual function must be defined in the class in which it first appears, i.e., nothing but
the base class. It is must that it should be defined as a public member function with the
keyword virtual prior the definition.
(2) A virtual function cannot be defined as a static member.
(3) A virtual function might possibly be defined as an inline function.
(4) A virtual function might be declared as a friend of one more class.
(5) Apart from the similar name, the prototypes of the virtual function in the base class and
the derived classes must go with accurately in terms of type of arguments, number of
arguments. If any variation is found, then, the compiler will try to capture it as an
overloaded function and the virtual function will drop the status of a virtual function.
(6) A constructor function cannot be defined as a virtual function.
(7) A destructor function may perhaps be defined as a virtual function.
(8) It is possible that definition of the virtual function in the base class can be overridden by
the relevant definitions in the derived classes. In case, if in the derived class, if the
function is not redefined, then the base class definition is only raised.
(9) The virtual functions can be inherited to any depth of hierarchy.
(10) A class that includes the virtual function is referred to as Polymorphic c lass.

Program to implement the usage of virtual function


#include <iostream>
using namespace std;
class Base
{
public:
virtual void display()
{
cout<<"Base class display"<<endl;
}
};

class Derived : public Base


{
public:
void display()
{
cout<<"Derived class display"<<endl;
}
};

C and C++ Programming


Electronic Science
35. Pointers, Virtual Function, Polymorphism
12

int main()
{
Base bp;
Derived dp;
Base *bptr;
bptr=&bp;
bptr->display();

bptr=&dp;
bptr->display();
return 0;
}
When the above code is compiled and executed, it produces the following result:
Base class display
Derived class display

Program to illustrate the usage of virtual function


#include <iostream>
using namespace std;
class Figure
{
protected:
double width, height;
public:
void setvalues(double d1, double d2)
{
width = d1;
height = d2;
}
virtual double area_of_figure()
{
return 0;
}
};

class Rectangle: public Figure


{
public:
double area_of_figure ()
{
return (width * height);
}
};

C and C++ Programming


Electronic Science
35. Pointers, Virtual Function, Polymorphism
13

int main ()
{
Figure *fp;
Rectangle rt;
fp = &rt;
fp -> setvalues (5,4);
cout << "Area of rectangle is "<<fp -> area_of_figure() << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Area of rectangle is 20

Program to demonstrate the use of virtual function


#include<iostream>
using namespace std;
class Base
{
public:
int i1;
Base(int x1)
{
i1 = x1;
}
virtual void myvirtualfunction()
{
cout << "\nBase class version of my virtual function()"<<endl;
cout << i1 <<endl;
}
};

class Derived1 : public Base


{
public:
Derived1(int x1) : Base(x1)
{
}
void myvirtualfunction()
{
cout << "Derived1 class version of my virtual function() "<<endl;
cout << i1*i1 <<endl;
}
};

C and C++ Programming


Electronic Science
35. Pointers, Virtual Function, Polymorphism
14

class Derived2 : public Base


{
public:
Derived2(int x2) : Base(x2)
{
}
};

int main()
{
Base *b1;
Base b2(10);
Derived1 derived1(10);
Derived2 derived2(10);
b1 = &b2;
b1->myvirtualfunction();
b1 = &derived1;
b1->myvirtualfunction();
b1 = &derived2;
b1->myvirtualfunction();
return 0;
}
When the above code is compiled and executed, it produces the following result:
Base class version of my virtual function()
10
Derived1 class version of my virtual function()
100
Base class version of my virtual function()
10

7. Virtual Destructor
The Destructor in the Base class can be Virtual. Every time, when upcasting is performed, then,
it is must that that destructor of the base class to make virtual for appropriate devastation of the
object when the program exits. In general, the constructors can never be virtual, but destructors
can be virtual.
Program to implement the usage of upcasting without virtual destructor
#include <iostream>
using namespace std;
class base1
{
public:
~base1()
{
cout << "destructing base1 "<<endl;
}
C and C++ Programming
Electronic Science
35. Pointers, Virtual Function, Polymorphism
15

};
class derived1 : public base1
{
public:
~derived1()
{
cout << "destructing derived1 "<<endl;
}
};

int main()
{
base1 *bp1 = new derived1;
delete bp1;
return 0;
}
When the above code is compiled and executed, it produces the following result:
destructing base1

Program to implement the usage of upcasting with virtual destructor


#include <iostream>
using namespace std;
class base1
{
public:
virtual ~base1()
{
cout << "destructing base1 "<<endl;
}
};
class derived1 : public base1
{
public:
~derived1()
{
cout << "destructing derived1 "<<endl;
}
};
int main()
{
base1 *bp1 = new derived1;
delete bp1;
return 0;
}
When the above code is compiled and executed, it produces the following result:
C and C++ Programming
Electronic Science
35. Pointers, Virtual Function, Polymorphism
16

destructing derived1
destructing base1

8. Abstract class and Pure Virtual Function


Abstract Class is a class which contains as a minimum of one Pure Virtual function in it.
Abstract classes are mainly used to make available an Interface for its sub classes. It is must for
the classes that inherit an Abstract Class to make available the description to the pure virtual
function, otherwise, even the classes will too turn out to be abstract class.

Characteristics of Abstract Class


 It is possible to create the pointers and references of the Abstract class, but instantiation
of an Abstract class cannot be performed.
 The Abstract class may be able to contain normal functions and variables along with a
pure virtual function.
 The Abstract classes are mostly used for the process of Upcasting, so that it is possible
for the derived classes to use its interface.
 It is must for the classes that inherit an Abstract Class to execute all pure virtual
functions, otherwise, they will become Abstract as well.

Pure Virtual Function


A pure virtual function is a function that is declared in a base class but it does not have
definition, but it is defined in the derived class. The pure virtual function begins
with virtual keyword and ends with =0.
Syntax:
virtual type Identifier (type parameters, —,—) = 0 ;

Example:
virtual void area()=0;

Program to perform addition and subtraction operations using the concept of pure virtual
function
#include<iostream>
using namespace std;
class baseclass
{
protected:
int n1;
int n2;
int result;
public:
void set(int x1, int y1)
{
n1=x1;

C and C++ Programming


Electronic Science
35. Pointers, Virtual Function, Polymorphism
17

n2=x2;
}
virtual void operation() = 0;
int get()
{
return result;
}
};
class addition : public baseclass
{
public:
void operation()
{
result=n1+n2;
}
};
class subtraction : public baseclass
{
public:
void operation()
{
result=n1-n2;
}
};
int main()
{
int number1, number2;
baseclass *bcp;
addition aoper;
subtraction soper;
cout<<"\nEnter two numbers ";
cin>>number1>>number2;
bcp=&aoper;
bcp->set (number1,number2);
bcp->operation();
cout<<"\nResult of Addition is "<<bcp->get ();
bcp=&soper;
bcp->set (number1,number2);
bcp->operation();
cout<<"\nResult of Subtraction: "<<bcp->get ();
return 0;
}
When the above code is compiled and executed, it produces the following result:
Enter two numbers 23 10
Result of Addition is 33
C and C++ Programming
Electronic Science
35. Pointers, Virtual Function, Polymorphism
18

Result of Subtraction: 13

Program to implement the usage of pure virtual function


#include <iostream>
using namespace std;
class Figure
{
protected:
double width, height;
public:
void setvalues(double d1, double d2)
{
width = d1;
height = d2;
}
virtual double area_of_figure() = 0;
};

class Rectangle: public Figure


{
public:
double area_of_figure()
{
return (width * height);
}
};

class Triangle: public Figure


{
public:
double area_of_figure()
{
return (width * height)/2;
}
};

int main ()
{
Figure *fp;

C and C++ Programming


Electronic Science
35. Pointers, Virtual Function, Polymorphism
19

Rectangle rt;
fp = &rt;
fp ->setvalues (8,7);
cout << "Area of Rectangle is " << fp -> area_of_figure() << endl;
Triangle tr;
fp = &tr;
fp -> setvalues (10,5);
cout << "Area of Triangle is " <<fp->area_of_figure() << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Area of Rectangle is 56
Area of Triangle is 25

9. Summary

 Pointers try to provide another approach to contact other data objects.

 Some of the arithmetic operators that can be used with pointers are ++, --, +, -, += and -=.

 A pointer can position to objects like that of simple data types and arrays.

 The Object pointers are of use in creating objects at run time.

 The pointers cannot only be used on objects of the base class, but they can also be used
on objects of derived class also.

 Even though, C++ language permits a pointer of the base class to position to any object
that is derived from the base class, but the pointer ca nnot be used straightforwardly to
access all the members of the derived class.

 The Polymorphism is an amalgamation of two Greek words, i.e. poly and morphism.

 The two forms of polymorphism that is supported by C++ language are Compile-time
Polymorphism and Run-time Polymorphism.

 In run-time polymorphism, the function is related with a particular class to a large extent
later on after the compilation and as a result it is also known as late binding or dynamic
binding.

C and C++ Programming


Electronic Science
35. Pointers, Virtual Function, Polymorphism
20

 Late Binding is also called as Dynamic Binding or Runtime Binding.

 A Virtual Function is a member function that is declared within the base class, which is
redefined or overrided in the derived class, and which informs the compiler to carry
out Late Binding on this function.

 The virtual function tries to apply the “one interface, multiple methods” idea that bring
about polymorphism.

 A table called virtual table (VTBL) holds pointers to all the functions that have been
declared as virtual in a class, or any other classes that are inherited.

 If the function is not defined as virtual, then, a Static Binding is performed at compilation
time. The method of the base class method gets activated.

 If the function is defined as virtual, then, a Dynamic Binding will be performed at


runtime. The method of the right class will be activated.

 A constructor function cannot be defined as a virtual function.

 A destructor function may perhaps be defined as a virtual function.

 A class that includes the virtual function is referred to as Polymorphic class.

 Every time, when upcasting is performed, then, it is must that that destructor of the base
class to make virtual for appropriate devastation of the object when the program exits.

 Abstract Class is a class which contains as a minimum of one Pure Virtual function in it.

 Abstract classes are mainly used to make available an Interface for its sub classes.

 The Abstract classes are mostly used for the process of Upcasting, so that it is possible
for the derived classes to use its interface.

 A pure virtual function is a function that is declared in a base class but it does not have
definition, but it is defined in the derived class.

 The pure virtual function begins with virtual keyword and ends with =0.

C and C++ Programming


Electronic Science
35. Pointers, Virtual Function, Polymorphism
21

C and C++ Programming


Electronic Science
35. Pointers, Virtual Function, Polymorphism

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy