Batch) .: Name: Aditya Bhalchandra Patil. Branch: Computer Technology. Class: II CM (2 Roll No: 186136
Batch) .: Name: Aditya Bhalchandra Patil. Branch: Computer Technology. Class: II CM (2 Roll No: 186136
Batch) .: Name: Aditya Bhalchandra Patil. Branch: Computer Technology. Class: II CM (2 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.
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
}
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();
}
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
#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;
}
#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;
}
Example:
#include <iostream>
class Distance {
public:
Distance()
this->feet = 0;
this->inch = 0;
Distance(int f, int i)
this->feet = f;
this->inch = i;
}
Distance operator+(Distance& d2)
Distance d3;
return d3;
};
int main()
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;
}