Unit 4
Unit 4
● 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.
● 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 +.
● 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.
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.
class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};
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