Batch) .: Name: Aditya Bhalchandra Patil. Branch: Computer Technology. Class: II CM (2 Roll No: 186136

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

Name: Aditya Bhalchandra Patil.

Branch: Computer Technology.


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

4 Marks Question.
Q1.How polymorphism implemented in c++?
The word polymorphism means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form.
Real life example of polymorphism, a person at the same time can have different characteristic.
Like a man at the same time is a father, a husband, an employee. So the same person posses
different behavior in different situations. This is called polymorphism.
Polymorphism is considered as one of the important features of Object Oriented Programming.

In C++ polymorphism is mainly divided into two types:


• Compile time Polymorphism
• Runtime Polymorphism

Compile time polymorphism: This type of polymorphism is achieved by function overloading or


operator overloading.
Runtime polymorphism: This type of polymorphism is achieved by Function Overriding.
Example
#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {real = r; imag = i;}
Complex operator + (Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};

int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
}

Q2.What is need of polymorphism?


Object-Oriented Programming or shortly, OOP, is a constitutional part of modern application
architecture. OOP comprises four fundamental concepts: A PIE: Abstraction, Polymorphism,
Inheritance, and Encapsulation.
Polymorphism is made of two parts: poly (multiple) and morph(form).
To make it simpler:
In the world of OOP and objects, every single object that has more than one shape or can be
assigned to more than one class is called a polymorphic object.
Hence, polymorphism is used to reduce the complexitiy of a program. Polymorphism is used
when we need an occurrence of an entity that has single name and many forms which acts
differently in different situations or circumstances.

Q3.What is function overloading ?How it can be achieved?


Function overloading is a feature in C++ where two or more functions can have the same name
but different parameters.
Function overloading can be considered as an example of polymorphism feature in C++.
Following is a simple C++ example to demonstrate function overloading.
#include <iostream>
using namespace std;
void print(int i) {
cout << " Here is int " << i << endl;
}
void print(double f) {
cout << " Here is float " << f << endl;
}
void print(char const *c) {
cout << " Here is char* " << c << endl;
}
int main() {
print(10);
print(10.10);
print("ten");
return 0;
}

Q4.What is function overloading with number of parameter ?Explain with example.


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 (int, float) which is different from the function myfuncn(float
a, int b) parameter list (float, int). Function overloading is a compile-time polymorphism.
For example:
These two functions have different parameter type:

sum(int num1, int num2)


sum(double num1, double num2)
These two have different number of parameters:

sum(int num1, int num2)


sum(int num1, int num2, int num3)

These two have different sequence of parameters:

sum(int num1, double num2)


sum(double num1, int num2)

Q5.What is function overloading with different types of parameter? Explain with


example.
Refer to answer of que no 4.

Q6.What is runtime polymorphism? How it can be achieved?


Runtime polymorphism is also known as dynamic polymorphism or late binding. In runtime
polymorphism, the function call is resolved at run time.

In contrast, to compile time or static polymorphism, the compiler deduces the object at run
time and then decides which function call to bind to the object. In C++, runtime polymorphism
is implemented using function overriding.

Function overriding is the mechanism using which a function defined in the base class is once
again defined in the derived class. In this case, we say the function is overridden in the derived
class.

We should remember that function overriding cannot be done within a class. The function is
overridden in the derived class only. Hence inheritance should be present for function
overriding.

The second thing is that the function from a base class that we are overriding should have the
same signature or prototype i.e. it should have the same name, same return type and same
argument list.
Example:

#include <iostream>
using namespace std;
class Base
{
public:
void show_val()
{
cout << "Class::Base"<<endl;
}
};
class Derived:public Base
{
public:
void show_val() //function overridden from base
{
cout << "Class::Derived"<<endl;
}
};
int main()
{
Base b;
Derived d;
b.show_val();
d.show_val();
}

Q7.Explain how static polymorphism can be achieved?


static polymorphism is also known as early binding and compile-time polymorphism.

Function overloading is an example of static polymorphism. More than one function with same
name, with different signature in a class or in a same scope is called function overloading

Another example of static polymorphism is Operator overloading. Operator overloading is a


way of providing new implementation of existing operators to work with user-defined data
types.
Example:

#include <bits/stdc++.h>
using namespace std;
class Geeks
{
public:
void func(int x)
{
cout << "value of x is " << x << endl;
}
void func(double x)
{
cout << "value of x is " << x << endl;
}
{
cout << "value of x and y is " << x << ", " << y << endl;
}
};

int main() {

Geeks obj1;
obj1.func(7);
obj1.func(9.132);
obj1.func(85,64);
return 0;
}

Q8.How unary operator overload? Explain with example.


The unary operators operate on a single operand and following are the examples of Unary
operators −

• The increment (++) and decrement (--) operators.


• The unary minus (-) operator.
• The logical not (!) operator.
The unary operators operate on the object for which they were called and normally, this
operator appears on the left side of the object, as in !obj, -obj, and ++obj but sometime they can
be used as postfix as well like obj++ or obj--.
Example:

#include<iostream>
using namespace std;
class IncreDecre
{
int a, b;
public:
void accept()
{
cout<<"\n Enter Two Numbers : \n";
cout<<" ";
cin>>a;
cout<<" ";
cin>>b;
}
void operator--() //Overload Unary Decrement
{
a--;
b--;
}
void operator++() //Overload Unary Increment
{
a++;
b++;
}
void display()
{
cout<<"\n A : "<<a;
cout<<"\n B : "<<b;
} };
int main()
{
IncreDecre id;
id.accept();
--id;
cout<<"\n After Decrementing : ";
id.display();
++id;
++id;
cout<<"\n\n After Incrementing : ";
id.display();
return 0;
}
Q9. How unary operator overload by using friend function ?Explain.

• Friend function using operator overloading offers better flexibility to the class.
• These functions are not a members of the class and they do not have 'this' pointer.
• When you overload a unary operator you have to pass one argument.
• When you overload a binary operator you have to pass two arguments.
• Friend function can access private members of a class directly.

Example:

#include<iostream>
using namespace std;
class UnaryFriend
{
int a=10;
int b=20;
int c=30;
public:
void getvalues()
{
cout<<"Values of A, B & C\n";
cout<<a<<"\n"<<b<<"\n"<<c<<"\n"<<endl;
}
void show()
{
cout<<a<<"\n"<<b<<"\n"<<c<<"\n"<<endl;
}
void friend operator-(UnaryFriend &x); //Pass by reference
};
void operator-(UnaryFriend &x)
{
x.a = -x.a; //Object name must be used as it is a friend function
x.b = -x.b;
x.c = -x.c;
}
int main()
{
UnaryFriend x1;
x1.getvalues();
cout<<"Before Overloading\n";
x1.show();
cout<<"After Overloading \n";
-x1;
x1.show();
return 0;
}

Q10. How binary operator overload? Explain with example.


In binary operator overloading function, there should be one argument to be passed. It is
overloading of an operator operating on two operands.

Example:

#include <iostream>

using namespace std;

class Distance {

public:

int feet, inch;

Distance()

this->feet = 0;

this->inch = 0;

Distance(int f, int i)

this->feet = f;

this->inch = i;

}
Distance operator+(Distance& d2)

Distance d3;

d3.feet = this->feet + d2.feet;

d3.inch = this->inch + d2.inch;

return d3;

};

int main()

Distance d1(8, 9);

Distance d2(10, 2);

Distance d3;

d3 = d1 + d2;

cout << "\nTotal Feet & Inches: " << d3.feet << "'" << d3.inch;

return 0;

Q11. How binary operator overload by using friend function? Explain with example.
In this approach, the operator overloading function must precede with friend keyword, and
declare a function class scope. Keeping in mind, friend operator function takes two parameters
in a binary operator, varies one parameter in a unary operator. All the working and
implementation would same as binary operator function except this function will be
implemented outside of the class scope.
Example:

#include <iostream>
using namespace std;
class Distance {
public:
int feet, inch;
Distance()
{
this->feet = 0;
this->inch = 0;
}
Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}
friend Distance operator+(Distance&, Distance&);
};
Distance operator+(Distance& d1, Distance& d2) // Call by reference
{
Distance d3;
d3.feet = d1.feet + d2.feet;
d3.inch = d1.inch + d2.inch;
return d3;
}
int main()
{
Distance d1(8, 9);
Distance d2(10, 2);
Distance d3;
d3 = d1 + d2;
cout << "\nTotal Feet & Inches: " << d3.feet << "'" << d3.inch;
return 0;
}

You might also like