UNIT-5 Polymorphism
C++ Pointers
Pointers are symbolic representations of addresses. They enable programs to simulate call-by-
reference as well as to create and manipulate dynamic data structures. Iterating over elements in
arrays or other data structures is one of the main use of pointers.
The address of the variable you’re working with is assigned to the pointer variable that points to the
same data type (such as an int or string).
Syntax:
datatype *var_name;
int *ptr; // ptr can point to an address which holds int data
How to use a pointer?
Define a pointer variable
Assigning the address of a variable to a pointer using the unary operator (&) which returns
the address of that variable.
Accessing the value stored in the address using unary operator (*) which returns the value of
the variable located at the address specified by its operand.
// C++ program to illustrate Pointers
#include <bits/stdc++.h>
using namespace std;
void pointers()
int var = 20;
// declare pointer variable
int* ptr;
// note that data type of ptr and var must be same
ptr = &var;
// assign the address of a variable to a pointer
cout << "Value at ptr = " << ptr << "\n";
cout << "Value at var = " << var << "\n";
cout << "Value at *ptr = " << *ptr << "\n";
// Driver program
int main()
pointers();
return 0;
OUTPUT :
Value at ptr = 0x7ffe454c08cc
Value at var = 20
Value at *ptr = 20
This pointer :
Every object in C++ has access to its own address through an important pointer called this pointer.
The this pointer is an implicit parameter to all member functions. Therefore, inside a member
function, this may be used to refer to the invoking object.
Friend functions do not have a this pointer, because friends are not members of a class. Only
member functions have a this pointer.
#include <iostream>
using namespace std;
class Box {
public:
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
}
double Volume() {
return length * breadth * height;
}
int compare(Box box) {
return this->Volume() > box.Volume();
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
if(Box1.compare(Box2)) {
cout << "Box2 is smaller than Box1" <<endl;
} else {
cout << "Box2 is equal to or larger than Box1" <<endl;
}
return 0;
}
Polymorphism :
The word polymorphism means having many forms. Typically, polymorphism occurs when there is a
hierarchy of classes and they are related by inheritance.
C++ polymorphism means that a call to a member function will cause a different function to be
executed depending on the type of object that invokes the function.
#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
Shape( int a = 0, int b = 0){
width = a;
height = b;
}
int area() {
cout << "Parent class area :" << width * height << endl;
return width * height;
}
};
class Rectangle: public Shape {
public:
Rectangle( int a = 0, int b = 0):Shape(a, b) { }
int area () {
cout << "Rectangle class area :" << width * height << endl;
return (width * height);
}
};
class Triangle: public Shape {
public:
Triangle( int a = 0, int b = 0):Shape(a, b) { }
int area () {
cout << "Triangle class area :" << (width * height)/2 << endl;
return (width * height / 2);
}
};
// Main function for the program
int main() {
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);
// store the address of Rectangle
shape = &rec;
// call rectangle area.
shape->area();
// store the address of Triangle
shape = &tri;
// call triangle area.
shape->area();
return 0;
}
Virtual Function in C++
A virtual function is a member function which is declared within a base class and is re-
defined(Overridden) by a derived class. When you refer to a derived class object using a pointer or
a reference to the base class, you can call a virtual function for that object and execute the derived
class’s version of the function.
Pure Virtual Functions in C++
A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have an
implementation, we only declare it. A pure virtual function is declared by assigning 0 in the
Virtual function Pure virtual function
A pure virtual function is a member function of
A virtual function is a member function
base class whose only declaration is provided in
of base class which can be redefined by
base class and should be defined in derived class
derived class.
otherwise derived class also becomes abstract.
Classes having virtual functions are not Base class containing pure virtual function becomes
abstract. abstract.
Declaration: virtual
Declaration: virtual
funct_name(parameter_list) {. .
funct_name(parameter_list)=0;
. . .};
Definition is given in base class. No definition is given in base class.
Base class having virtual function can
Base class having pure virtual function becomes
be instantiated i.e. its object can be
abstract i.e. it cannot be instantiated.
made.
If derived class does not redefine virtual function of
If derived class does not redefine virtual
the base class, then no compilation error is
function of the base class, then it does
encountered, but like the base class, derived class
not affect compilation.
also becomes abstract.
declaration.
MCQs :
1. Which is also called as abstract class?
A. virtual function B. pure virtual function C. derived class D. base class
2. What is meant by pure virtual function?
a. Function which does not have definition of its own
b. Function which does have definition of its own
c. Function which does not have any return type
d. Function which does not have any return type & own definition
3. Where the abstract class does is used?
A. base class only B. derived class C. both derived & base class D. virtual class
4. Which of the following is the correct way to declare a pointer?
A. int *ptr B. int ptr C. int &ptr D. All of the above
5. Which is the pointer which denotes the object calling the member function?
A. Variable pointer B. This pointer C. Null pointer D. Zero pointer
6. The this pointer is accessible __________________
A. Within all the member functions of the class B. Only within functions returning void
C. Only within non-static functions D. Within the member functions with zero arguments
7. What is the use of this pointer?
A. When local variable’s name is same as member’s name, we can access member using this
pointer.
B. To return reference to the calling object
C. Can be used for chained function calls on an object
D. All of the above
8. This pointer can be used directly to ___________
A. To manipulate self-referential data structures
B. To manipulate any reference to pointers to member functions
C. To manipulate class references
D. To manipulate and disable any use of pointers
9. Which members can never be accessed in derived class from the base class?
A. Private
B. Protected
C. Public
D. All except private
10. Which of the following is true for pointer to derived class?
A. We can use pointers not only to a base objects but also to the objects of derived classes.
B. We cannot use pointers in the classes.
C. Using pointer in class is form of inheritance
D. All of above.
11. We can access those members of derived class which are inherited from base class by base
class pointer.
A. True
B. False
12. If same message is passed to objects of several different classes and all of those can respond in
a different way, what is this feature called?
A. Inheritance B. Overloading C. Polymorphism D. Overriding