Object Oriented Programming With C++ (203105337) : Prof. Payal Desai, Assistant Professor

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Object Oriented Programming with

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.

For example: S.getdata();


Here S is an object and getdata() is a member function. So, ‘this’ pointer will point or set to
the address of object S.

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

1876 Address 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.

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.

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.

Rule for Abstract Class:-


- As we have seen that any class that has a pure virtual function is an abstract class.
- We cannot create the instance of abstract class. For example: If I have written this line
Animal obj; in the above program, it would have caused an compilation error.
- We can create pointer and reference of base abstract class points to the instance of child
class.
- Abstract class can have constructors .
- If the derived class has not implemented the pure virtual function of parent class
then that derived class becomes abstract.
Abstract Class Example
#include<iostream> void sound() {
using namespace std; cout<<"Woof"<<endl;
class Animal{ }
public: };
//Pure Virtual Function int main(){
virtual void sound() = 0; Dog obj;
//Normal member Function obj.sound();
obj.sleeping();
void sleeping() {
return 0;
cout<<"Sleeping";
}
}
};
class Dog: public Animal{
public:
Virtual Base Class
Virtual base classes are used in virtual inheritance .For preventing multiple “instances” of a
given class appearing in an inheritance hierarchy when using multiple inheritances.
Consider the situation where we have one class Grant Parent .This class is Grant Parent  is
inherited by two other classes Parent1 and Parent2. Both these class are inherited into
another in a new class child as shown in fig.

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.

Image source : Google


Function Overloading
When there are multiple functions with same name but different parameters then these
functions are said to be overloaded.

Functions can be overloaded by changing number of arguments or/and changing the type


of arguments.

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.

Overloading of functions with different return type is not allowed.


Function Overloading Example
#include <iostream.h> void func(int x, int y)
using namespace std; {
class Geeks cout << "value of x and y is " << x << ",
{ " << y << endl;
public: }
void func(int x) };
{
cout << "value of x is " << x << endl; int main()
} {
void func(double x) Geeks obj1;
{ obj1.func(7);
cout << "value of x is "<< x << endl; obj1.func(9.132);
} obj1.func(85,64);
return 0;
}
Operator Overloading
C++ also has options to overload operators.

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.

Templates can be used in two different ways:


• Function Templates
• Class Templates
Function Templates
• Function templates are those functions which can handle different data type without
separating code for each of them.
• Example:
#include <iostream>
using namespace std;
template <class T>
T Large(T n1, T n2){
If(n1>n2){
return n1;
}
else{
return n2;
}}
int main() {
cout << Large(10, 20)<< endl;
cout<<Large(“hello”,”world”) << endl;
cout<<Large(10.5,15.2) << endl;
}
Class Templates
• Class templates are used where we have multiple copies of code for different data
types with the same logic.
• Example:
#include <iostream> int main()
using namespace std; {
template <class T> Calculator<int> intCalc(2, 1);
class Calculator{ Calculator<float> floatCalc(2.4, 1.2);
public: intCalc.displayResult();
T num1, num2; floatCalc.displayResult();
Calculator(T n1, T n2){ return 0;
num1 = n1; }
num2 = n2;
}
void displayResult(){
cout << "Addition is: " << add() << endl;
}
T add() { return num1 + num2; }
www.paruluniversity.ac.in

You might also like