Sie sind auf Seite 1von 10

Assignment =04

Name :-Nazarene
Semester:-3rd(2nd year)
Branch:-computer science engg
Ans-:-There are two types of polymorphism.
1-:compile time polymorphism.
2-:Run time polymorphism.
Compile time polymorphism:
The overloaded functions are invoked by matching the type and number of
arguments.
 This information is available at the compile time and, therefore, compiler selects the
appropriate
function at the compile time
 It is achieved by function overloading and operator overloading which is also known
as static binding
or early binding.
Now, let's consider the case where function name and prototype is same.
// program of compile time polymorphism-:
class A
{
int a;
public:
void display()
{
cout<< "Class A ";
}
};
class B : public A
{
int b;
public:
void display()
{
cout<<"Class B";
2-: Runtime Polymorphism-:
Function overriding is an example of Runtime polymorphism.
-:Function Overriding: When child class declares a method, which is already
present in the parent class then this is called function overriding, here child
class overrides the parent class.
In case of function overriding we have two definitions of the same function,
one is parent class and one in child class. The call to the function is determined
at runtime to decide which definition of the function is to be called, that the
reason it is called runtime polymorphism.
#include <iostream.h>
#include<conio.h>
class A
{
public: int main()
void display() {
{ A.obj1;
cout<<"Super Class Function"<<endl; obj.display();
} B.obj2;
}; obj.display();
class B: public A return (0);
{ }
public: void display() output
{ super class
function
Ans-:
→Compile time polymorphism: This type of polymorphism is
achieved by function overloading or operator overloading.
◦ Overloading: Overloading is where more than one methods share the same
name with different parameters or signature and different return type.
◦ The call is resolved by the compiler.
◦ It is also known as Static binding, Early binding and overloading as well.
◦ It provides fast execution because known early at compile time.
◦ Compile time polymorphism is less flexible as all things execute at
compile time.
 → Runtime polymorphism: This type of polymorphism is achieved
by Function Overriding.
◦ Function overriding: Overriding is having same method with same
parameters or signature, but associated within a class & its subclass.
◦ It is also known as Dynamic binding, Late binding and overriding as well.
◦ It is achieved by virtual functions and pointers.
◦ It provides slow execution as compare to early binding because it is
known at runtime.
◦ Run time polymorphism is more flexible as all things execute at run time.
 Compile time polymorphism -- method overloading
 Run time polymorphism -- method overriding
 Ans-:
 In C++ we can overload some operators like +, -, [], -> etc. But we
cannot overload any operators in it. Some of the operators cannot be
overloaded. These operators are like below
 ? “.”
 Member access or dot operator.
 ? “? : ”
 Ternary or conditional operator.
 ? “::”
 Scope resolution operator.
 ? “.*”
 Pointer to member operator.
 ? “sizeof”
 The object size operator.
 ? “typeid”
 Object type operator.

 These operators cannot be overloaded because if we overload them it


will make serious programming issues.
 For an example the sizeof operator returns the size of the object or
datatype as an operand.
 This is evaluated by the compiler. It cannot be evaluated during
runtime. So we cannot overload it.
 Ans-:
 In same situation there is no need for a particular function in
the base class in such situation the body of the virtual function
can be removed from base class.
 A class containing pure virtual function cannot be use to
declare any object of its own although you cannot create
instance of base class you can create class pointer and
reference to an abstract class. such class are called abstract
base class.
 The main objective of an abstract base class are :=
1)To provide same features to the derived class.
2)To create a frame work upon which new classes can be built.
3)To create a from work upon which new classes can be built .
4)To provide a class library with predefined flow of control for
the programming to extend the reusable class.
5)To provide interface and to act as a base class for other
classes.
int x;
public:
virtual void function=0;

int get x()


{
return x;
}
};
Class derived :public base
{
Int y; d.fun();
Public: return(0);
Void fun() }
{
Cout <<“fun()called”; output
=fun()called.
};
Int main (void)
{
Derived d;
1. Answer:-
 A pure virtual function is a function declared in base class that
has no definition relative to base class .
 The pure virtual function is one without a body and equal to
zero in its declaration
 The declaration in pure virtual function has assignment to zero
notion indicates that there is no defined for the function.
 Virtual function defined inside base class normally as a frame
work for frame design of class hierarchy .
 These function can be overridden by the method in the derived
class virtual function defined by the null body and has no
definition are doing nothing function or dummy function and
in c ++,they are called as pure virtual function.
 When a virtual function mode a pure ,any derived class must
provide us own definition . if a virtual function is not defined
by a derived class.
 The definition of the function in the derived class over side the
definition of the function in the base class .this provides actual
operations performed by the function.
 when both derived and base class use
the same function name:
 The function in base class must be declared as virtual
using keywords virtual ,then c ++ programming
language determines which function to use at run
time based on the type of object pointer by the
pointer of the base class rather than the pointer type

Das könnte Ihnen auch gefallen