Object Oriented Programming With C++ (203105337) : Prof. Payal Desai, Assistant Professor
Object Oriented Programming With C++ (203105337) : Prof. Payal Desai, Assistant Professor
Object Oriented Programming With C++ (203105337) : Prof. Payal Desai, Assistant Professor
C++(203105337)
Prof. Payal Desai, Assistant Professor
Computer science & Engineering
CHAPTER-6
Polymorphism, Virtual function and Template
This pointer
This pointer represent an object that invokes or calls a member function. It will point to the
object for which member function has been called. It will be automatically passed to a
member function whenever it will be called. It is also known as implicit argument to all
member function.
Suppose ‘a’ is private data member, we can access it only in public member function like as
follows a=50;
We access it in public member function by using ‘this’ pointer like as follows
this->a=50;Both will work same.
This pointer
• Pointer is a variable that holds the address of another variable.
• It can only store address of a data item rather than it’s value.
Syntax :
Datatype *var_name;
a
10 Value of variable a
a*
1876 Address of pointer of a
1880
This Pointer Example
#include <iostream> };
using namespace std; int main()
class sample {
{ sample S;
int a; S.disp(20);
public: sample() return 0;
{ }
a=10;
o/p:-
}
The value of argument a=20
void disp(int a)
The value of data member a=10
{
cout<<"The value of argument a="<<a;
cout<<"\nThe value of data member
a="<<this->a;
}
Virtual Function
It is a run time polymorphism.
If Base class and derived class have same function name ,then base class pointer is
assigned address of derived class object and that pointer will execute base class function.
To execute function of derived class, we have to declare function of base class as virtual.
To declare virtual function just use keyword virtual preceding its normal function
declaration.
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 .
Virtual Function Example
#include <iostream> public:
void print()
using namespace std;
{
class base { cout << "print derived class" << endl;
public: }
virtual void print() void show()
{ {
cout << "print base class" << endl; cout << "show derived class" << endl;
} }
};
int main()
void show()
{
{ base* bptr;
cout << "show base class" << endl; derived d;
} bptr = &d;
}; bptr->print();
class derived : public base { bptr->show();
}
Pure Virtual Function
Sometimes implementation of some functions cannot be provided in a base class
because we don’t know its implementation. Such a class is called abstract class.
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.
For example, let Shape be a base class. We cannot provide implementation of function
draw() in Shape, but we know every derived class must have implementation of draw().
A pure virtual function (or abstract function) in C++ is a virtual function for which we
don’t have any implementation, we just declare it. A pure virtual function is declared by
assigning 0 in declaration.
Pure-Virtual Function Example
#include<iostream> int main(void)
using namespace std; {
Derived d;
class Base d.fun();
{ return 0;
int x; }
public: o/p:- fun() called
virtual void fun() = 0;
int getX() { return x; }
};
class Derived: public Base
{
int y;
public:
void fun() { cout << "fun() called"; }
};
Abstract Class
An abstract class is, conceptually- a class that cannot be instantiated(represented) and is
usually implemented as a class that has one or more pure virtual (abstract) functions.
Grand Parent
Parent1 Parent2
Child
Polymorphism
Polymorphism means the ability to take more than one form.
It allows a single name to be used for more than one related purpose.
It means ability of operators and functions to act differently in different situations.
Function overloading is like practice of declaring the same function with different
signatures.
The same function name will be used with different number of parameters and parameters
of different type.
For example, we can use the operator (‘+’) for string class to concatenate two strings. We
know that this is the addition operator whose task is to add two operands. So a single
operator ‘+’ is when placed between integer operands , adds them and when placed
between string operands, concatenates them.
Operator overloading is the ability to tell the compiler how to perform a certain operation
based on its corresponding operator’s data type.
Function Over-Ridden
If derived class defines same function as defined in its base class, it is known as function
overriding in C++. It is used to achieve runtime polymorphism. It enables you to provide
specific implementation of the function which is already provided by its base class.
void eat()
Eg:- #include <iostream>
{
using namespace std;
cout<<"Eating bread...";
class Animal {
}
public:
};
void eat(){
int main(void) {
cout<<"Eating...";
Dog d = Dog();
}
};
d.eat();
class Dog: public Animal
return 0;
{
}
public:
Templates
A Template is a blueprint or formula for creating a generic class or a function.
Templates allows a function or class to work on many different Data types without
being rewritten for each one.
Templates are not types, but rather they are a placeholder for a type.
At compile time, the compiler makes a copy of the templated code and automatically
“replaces” the placeholders with the concrete type.
A function template starts with the keyword template followed by template
parameters inside < > which is followed by function declaration.