Unit-5: Darshan Institute of Engineering & Technology For Diploma Studies

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Darshan Institute of Engineering & Technology for Diploma Studies Unit-5

1 Write a short note on 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.

Different types of polymorphism are


Polymorphism

Compile time (Early Binding) Run time (Late Binding)

Function Overloading  Operator Overloading  Virtual Functions (Dynamic Binding)

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…

Dynamic Binding (Late Binding):


• Dynamic binding is the linking of a routine or object at runtime based on the conditions at that
moment.
• It means that the code associated with a given procedure call is not known until the time of the call.
• At run-time, the code matching the object under current reference will be called.

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.

1 Dept: CE Programming In C++ (3330702) Nitin Rola


 
Darshan Institute of Engineering & Technology for Diploma Studies Unit-5

2 Explain pointer to object with example.


• Pointer is one of the key aspects of C++ language.
• Pointer refers to another data variable by its memory address.
• A pointer can point any object similar like as normal variable.
• Create pointer object by following syntax:
classname * pointerobjectname;
• Intialize with address of another object by following syntax:
pointername=&objectname;
• For Example
#include <iostream.h>
#include <conio.h>
class trial
{
int a;
public:
void init(int x)
{
a=x;
}
void disp()
{
cout<<"\nvalue of a="<<a;
}
};
void main()
{
clrscr();
trial t;
trial *ptr_t;
ptr_t=&t;
t.init(10);
t.disp();
ptr_t->init(20);
ptr_t->disp();
t.disp();
getch();
}
• OUTPUT
value of a=10
value of a=20
value of a=20

3 Explain ‘this’ pointer with example.


• ‘this’ pointer represent an object that invoke or call a member function.
• It will point to the object for which member function is called.
• It is automatically passed to a member function when it is called.
• It is also called 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 follow
a=50;
• We access it in public member function by using ‘this’ pointer like as follow

2 Dept: CE Programming In C++ (3330702) Nitin Rola


 
Darshan Institute of Engineering & Technology for Diploma Studies Unit-5

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.

4 Explain pointer to derived classes.


• We can use pointers not only to the base objects but also to the objects of derived classes.
• A single pointer variable can be made to point to objects belonging to different classes.
• For example:
B *ptr //pointer to class B type variable
B b; //base object
D d; // derived object
ptr = &b; // ptr points to object b

• 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>

3 Dept: CE Programming In C++ (3330702) Nitin Rola


 
Darshan Institute of Engineering & Technology for Diploma Studies Unit-5

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;
}

5 Explain virtual function with example.


• It is a run time polymorphism.
• Base class and derived class have same function name and base class pointer is assigned address of
derived class object then also 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 uses 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.
• Rules for virtual function
1. The virtual functions must be member of any class.
2. They cannot be static members.
3. They are accessed by using object pointers.
4. A virtual function can be a friend of another class.
5. A virtual function in a base class must be defined, even though it may not be used.
6. If two functions with the same name have different prototypes, C++ considers them as
overloaded functions, and the virtual function mechanism is ignored.
7. We cannot have virtual constructors, but we can have virtual destructors.
4 Dept: CE Programming In C++ (3330702) Nitin Rola
 
Darshan Institute of Engineering & Technology for Diploma Studies Unit-5

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() {}
 

5 Dept: CE Programming In C++ (3330702) Nitin Rola


 

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy