Beruflich Dokumente
Kultur Dokumente
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.