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

Name: Aditya Bhalchandra Patil.

Branch: Computer Technology


Class: II CM ( 2nd Batch ) Roll No: 186136

OOP Assignment No 7
1) Define polymorphism.
Ans. Polymorphism means ability to represent more than one form.
Polymorphism is the technique of using same thing for different purpose.
Polymorphism is considered as one of the important features of OOP.
Polymorphism means having multiple form of one thing.
2) Can logical operator can be overloaded?
Ans. No, logical operator cannot be overloaded.
3) Enlist advantages of Polymorphism.
Ans. I) Same interface could be used for creating methods with
different implementations.
ii) Reduces the volume of work in terms of distinguishing and handling
various objects.
iii) Supports building extensible systems.
iv) Complete implementation can be replaced by using same method
signatures.

4) What is overriding?
Ans. Function overriding is a feature that allows us to have a same
function in child class which is already present in the parent class. A
child class inherits the data members and member functions of parent
class, but when you want to override a functionality in the child class
then you can use function overriding. It is like creating a new version of
an old function, in the child class.
Function Overriding Example:-
To override a function you must have the same signature in child class. By
signature I mean the data type and sequence of parameters. Here we don’t
have any parameter in the parent function so we didn’t use any parameter in
the child function.
#include <iostream>

using namespace std;

class BaseClass {

public:

void disp(){

cout<<"Function of Parent Class";

};

class DerivedClass: public BaseClass{

public:

void disp() {

cout<<"Function of Child Class";

};

int main() {

DerivedClass obj = DerivedClass();

obj.disp();

return 0;

}
5) What is overloading?

Ans. Function overloading is a C++ programming feature that allows us to have


more than one function having same name but different parameter list, when I
say parameter list, it means the data type and sequence of the parameters, for
example the parameters list of a function myfuncn(int a, float b) is which is
different from the function myfuncn(float a, int b) parameter list (float,int).
Function overloading is a compile-time polymorphism.
Now that we know what is parameter list lets see the rules of overloading: we
can have following functions in the same scope.

sum(int num1, int num2)


sum(int num1, int num2, int num3)
sum(int num1, double num2)

Ex.

#include <iostream>
using namespace std;
class Addition {
public:
int sum(int num1,int num2) {
return num1+num2;
}
int sum(int num1,int num2, int num3) {
return num1+num2+num3;
}
};
int main(void) {
Addition obj;
cout<<obj.sum(20, 15)<<endl; cout<<obj.sum(81, 100, 10);
return 0;

6) What is difference between overloading and overriding?


Ans. 1) Function Overloading happens in the same class when we declare
same functions with different arguments in the same class. Function
Overriding is happens in the child class when child class overrides parent
class function.
2) In function overloading function signature should be different for all
the overloaded functions. In function overriding the signature of both the
functions (overriding function and overridden function) should be same.
3) Overloading happens at the compile time thats why it is also known as
compile time polymorphism while overriding happens at run time which
is why it is known as run time polymorphism.
4) In function overloading we can have any number of overloaded
functions. In function overriding we can have only one overriding
function in the child class.

7) What is static binding?


Ans. In Early binding function call is resolved at compile time. Hence,
now compiler determines the type of object at compile time, and then
binds the function call. Early Binding is also static or compile-time
binding.
Ex. #include<iostream.h>
Class base
{
Public:
Void display()
{
Cout<<”base class”;
}
};
Class dereive: public base
{
Public:
Void display()
{
Cout<<”derived class”;
}
};
Int main()
{
Derive d;
Base *p;
P=&d;
p->display();
return 0;
}

8) What is dynamic binding?


Ans. In Late binding function call is resolved at runtime .Hence , now
compiler determines the type of object at runtime, and then binds the
function call. Late binding is called Dynamic binding or runtime binding.
Ex.
#include<iostream.h>
Class base
{
Public:
Virtual void display()
{
Cout<<”base class”;
}
};
Class derive:public base
{
Public: void display()
{
Cout<<”derived class”;
}
};
Int main()
{
Derive d;
Base *p;
P=&d;
p->display();
base s;
p=&s;
p->display();
return 0;
}

9) Difference between static binding and dynamic binding

Ans. Static binding Dynamic binding


1) Event occurs at compile time 1) Event occurs at run time is
is “static binding” ”Dynamic binding”

2) All function needed to call a 2) All information need to call a


function is known at compile function come to know at run
time. time.

3) Efficiency 3) Flexiblility
4) Fast execution 4)slow execution

5) Early binding 5)late binding

Ex. overloaded function call, Ex. Virtual function in c++,


overloaded operators overridden methods in java.

10. Enlist types of polymorphism.


Ans. Types of polymorphism:-
1) Compile-time/static/early binding
Ex. i) Function overloading.
ii) Operator overloading.
2) Run – time/dynamic/late binding
Ex. i)virtual function.

11) Which operators cannot be overloaded?


Ans. You cannot overload following operators:
i) Sizeof operator.
ii) Conditional operator(? :)
iii) Scope resolution operator(::)
iv) Class member access operator(.,.*)

12) Which operators cannot be overloaded with friend function?


Ans. i)Assignment operator =
ii)function call operator ()
iii)subscriping operator []
iv)class member access operator ->

13) Enlist any 4 unary operators which can be overloaded.


Ans. i) The increment (++)
ii) decrement (--) operators.
iii)The unary minus (-) operator.
iv)The logical not (!) operator
14) What is need of polymorphism?
Ans. Polymorphism is one of the most important and most valuable
concepts of programming.
Polymorphism literally means "many forms".
which means the data can be represented in multiple forms.
Even though classes are derived or inherited from the same parent class,
each derived class will have its own behavior.
Polymorphism is a concept linked to inheritance and assures that derived
classes have the same functions even though each derived class performs
different operations.

You might also like