Unit 4

You might also like

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

Object Oriented Programming

(Virtual Function & Polymorphism)


Polymorphism
Compile time Run time
1. Function overloading 1. Virtual functions
2. Constructor overloading 2. Pure virtual function/abstract class
3. Operator overloading
COMPILE-TIME POLYMORPHISM
1. Function Overloading
● Two or more functions can have the same name but different parameters

● When a function name is overloaded with different jobs it is called


Function Overloading

● In Function Overloading, “Function” name should be the same and the


arguments should be different.
1. Function Overloading (Contd)

● The parameters should follow any one or more than one of the following
conditions for Function overloading:
○ Parameters should have a different type
■ add(int a, int b)
■ add(double a, double b)
1. Function Overloading (Contd)
○ Parameters should have a different number
■ add(int a, int b)
■ add(int a, int b, int c)
○ Parameters should have a different sequence of parameters.
■ add(int a, double b)
■ add(double a, int b)
1. Function Overloading (Contd)
#include <iostream> return 0;
void print(int i) { }
cout << " Here is integer: " << i << endl;
} Output:
void print(double f) { Here is integer: 14
cout << " Here is float: " << f << endl; Here is float: 10.1
} Here is character*: hundred
void print(char const *c) {
cout << " Here is character*: " << c << endl;
}
int main() {
print(14);
print(10.10);
print(“hundred");
2. Constructor Overloading
● We can have more than one constructor in a class with same name, as long as
each has a different list of arguments. This concept is known as Constructor
Overloading.

● Overloaded constructors essentially have the same name (exact name of the
class) and different by number and type of arguments.

● A constructor is called depending upon the number and type of arguments


passed.

● While creating the object, arguments must be passed to let compiler know,
which constructor needs to be called.
2. Constructor Overloading (Contd.)
#include <iostream> } };
class construct
{ int main()
public: {
float area; // Constructor Overloading with two different
// Constructor with no parameters constructors of class name
construct() construct o;
{ construct o2( 5, 21);
area = 0; o.disp();
} o2.disp();
// Constructor with two parameters return 1;
construct(int a, int b) }
{
area = a * b; Output:
} 0
void disp() { 105
cout<< area<< endl;
3. Operator overloading
● C++ has the ability to provide the operators with a special meaning for a data
type, this ability is known as operator overloading.

● For example, we can overload an operator ‘+’ in a class like String so that we
can concatenate two strings by just using +.

● Other example classes where arithmetic operators may be overloaded are


Complex Numbers, Fractional Numbers, Big Integer, etc.

● Operator overloading is a compile-time polymorphism. It is an idea of giving


special meaning to an existing operator in C++ without changing its original
meaning.
3. Operator overloading (Contd.)
class Avg In this example, we have 3 objects
{ “a1”, “a2” and “a3” of type “class
Avg”. Here we are trying to add two
………….. objects “a1” and “a2”, which are of
}; user-defined type i.e. of type “class
int main() Avg” using the “+” operator. This is
{ not allowed, because the addition
Avg a1,a2,a3; operator “+” is predefined to
a3= a1 + a2; operate only on built-in data types
return 0; (variables). But here, “class Avg” is
} a user-defined type, so the compiler
generates an error. This is where the
concept of “Operator overloading”
comes in.
3. Operator overloading (Contd.)
● If the user wants to make the operator “+” to add two class objects, the user has
to redefine the meaning of the “+” operator such that it adds two class objects.
This is done by using the concept “Operator overloading”.

● So the main idea behind “Operator overloading” is to use c++ operators with
class variables or class objects.

● Redefining the meaning of operators really does not change their original
meaning, instead they have been given additional meaning along with their
existing ones.
RUN-TIME POLYMORPHISM
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.

● Virtual functions ensure that the correct function is called for an object, regardless of the
type of reference (or pointer) used for function call.

● They are mainly used to achieve Runtime polymorphism

● Functions are declared with a virtual keyword in base class.

● The resolving of function call is done at Run-time.


Rules for Virtual Functions
1. Virtual functions cannot be static and also cannot be a friend function of another class.

2. Virtual functions should be accessed using pointer or reference of base class type to achieve
run time polymorphism.

3. The prototype of virtual functions should be same in base as well as derived class.

4. They are always defined in base class and overridden in derived class. It is not mandatory for
derived class to override (or re-define the virtual function), in that case base class version of
function is used.

5. A class may have virtual destructor but it cannot have a virtual constructor.
void print()
#include <iostream> {
using namespace std; cout << "print derived class" << endl;
}
class base void show()
{ {
public: cout << "show derived class" << endl;
virtual void print() }
{ };
cout << "print base class" << endl; int main()
} {
base* bptr;
void show() derived d;
{ bptr = &d;
cout << "show base class" << endl; // virtual function, binded at runtime
} bptr->print();
}; // Non-virtual function, binded at compile time
class derived : public base bptr->show();
{ }
public:
Pure Virtual Functions and Abstract Classes
in C++
● Sometimes implementation of all function cannot be provided in a base
class because we don’t know the implementation. Such a class is called
abstract class.

● A pure virtual function (or abstract function) in C++ is a virtual function


for which we don’t have implementation, we only declare it. A pure
virtual function is declared by assigning 0 in declaration.
#include<iostream>
void output()
using namespace std; {
class base cout<<d;
{ }
};
int b;
public: int main()
virtual void input()=0; {
base *p;
derived d1;
virtual void output() p=&d1;
{} p->input();
p->output();
}; return 0;
}
class derived:public base
{
int d;
public:
void input()
{
cout<<"enter the value of d";
cin>>d;
}
#include<iostream>
using namespace std;

class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};

// This class inherits from Base and implements fun()


class Derived: public Base
{
int y;
public:
void fun() { cout << "fun() called"; }
};

int main(void)
{
Derived d;
d.fun();
return 0;
}
Some Interesting Facts:
● A class is abstract if it has at least one pure virtual function.
● We can have pointers and references of abstract class type.
● If we do not override the pure virtual function in derived class, then
derived class also becomes abstract class.
● An abstract class can have constructors.
#include<iostream> class derived:public base int main()
using namespace std; { {
class base int d; base *p;
{ derived d1;
int b; public: p=&d1;
public: void input() p->input();
base() { p->output();
{ cout<<"enter the value of d"; return 0;
b=10; cin>>d; }
} }
virtual void
input()=0; void output()
{
void output()
{ cout<<d;
}
cout<<b; };
}
};
1. Early binding (binding means converting variables into
address)
• Compile time polymorphism
• Address associated to function call
• By default early binding happens in c++

2. Late binding
• Run time polymorphism
• achieved by declaring virtual function

You might also like