Unit-5: Darshan Institute of Engineering & Technology For Diploma Studies
Unit-5: Darshan Institute of Engineering & Technology For Diploma Studies
Unit-5: Darshan Institute of Engineering & Technology For Diploma Studies
Compile time:
• Compile time polymorphism is function and operator overloading.
Function Overloading:
• Function overloading is the 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.
Please refer chapter:4 question 05 for more detail…
Operator Overloading:
• Operator overloading is the ability to tell the compiler how to perform a certain operation based on its
corresponding operator’s data type.
• Like + performs addition of two integer numbers, concatenation of two string variables and works
totally different when used with objects of time class.
Please refer chapter:7 for more detail…
Virtual Function:
• Virtual function is a member function of a class, whose functionality can be over-ridden in its derived
classes.
• The whole function body can be replaced with a new set of implementation in the derived class.
• It is declared as virtual in the base class using the virtual keyword.
this->a=50;
• Both will work same.
• For example:
#include <iostream.h>
class sample
{
int a;
public:
sample()
{
a=10;
}
void disp(int a)
{
cout<<"The value of argument a="<<a;
cout<<"\nThe value of data member a="<<this->a;
}
};
int main()
{
sample S;
S.disp(20);
return 0;
}
/* OUTPUT
The value of argument a=20
The value of data member a=10 */
• The most important advantage of ‘this’ pointer is, If there is same name of argument and data
member than you can differentiate it. By using ‘this’ pointer we can access the data member and
without ‘this’ we can access the argument in same function.
• In above example B is base class and D isa derived class from B, then a pointer declared as a pointer
to B and point to the object b.
• We can make ptr to point to the object d as follow
ptr = &d;
• We can access those members of derived class which are inherited from base class by base class
pointer. But we cannot access original member of derived class which are not inherited by base class
pointer.
• We can access original member of derived class which are not inherited by using pointer of derived
class.
• For example:
#include <iostream.h>
class base
{
public:
int b;
void show()
{
cout<<"\nThe value of b"<<b;
}
};
class derived:public base
{
public:
int d;
void show()
{
cout<<"\nThe value of b="<<b
<<"\nThe value of d="<<d;
}
};
int main()
{
base B;
derived D;
base *bptr;
bptr=&B;
cout<<"\nBase class pointer assign address of base class object";
bptr->b=100;
bptr->show();
bptr=&D;
bptr->b=200;
cout<<"\nBase class pointer assign address of derived class object";
bptr->show();
derived *dptr;
dptr=&D;
cout<<"\nDerived class pointer assign address of derived class object";
dptr->d=300;
dptr->show();
return 0;
}
8. The derived class pointer cannot point to the object of base class.
9. When a base pointer points to a derived class, then also it is incremented or decremented only
relative to its base type. Therefore we should not use this method to move the pointer to the next
object.
10. If a virtual function is defined in base class, it need not be necessarily redefined in the derived
class. In such cases, call will invoke the base class.
• We can better understand virtual function by following example:
#include <iostream.h>
class base
{
public:
void disp()
{
cout<<"\nSimple function in base class";
}
virtual void show()
{
cout<<"\nVirtual function of Base class";
}
};
class derived: public base
{
public:
void disp()
{
cout<<"\nSame name with simple function of base class in derived class";
}
void show()
{
cout<<"\nSame name with virtual function of base class in derived class";
}
};
int main()
{
base B;
derived D;
base *bptr;
bptr=&B;
cout<<"\nBase class pointer assign address of base class object";
bptr->disp();
bptr->show();
bptr=&D;
cout<<"\nBase class pointer assign address of derived class object";
bptr->disp();
bptr->show();
return 0;
}
6 Explain pure virtual functions.
• 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.
• A class containing pure virtual function cannot be used to create any direct objects of its own. This
type of class is also called as abstract class.
• For example:
virtual void display() = 0; OR virtual void display() {}