Oopsunit 2
Oopsunit 2
Oopsunit 2
Overloaded functions are often selected based on prototype of the function. It matches no. of
passing parameters, return type, type of passing parameters. Compile time polymorphism can
be of function or operator overloading. The concept of selecting appropriate overloaded
function by the compiler is known as EARLY BINDING. Early binding is also called as STATIC
BINDING.
If the object to the function is called at run time then it is called run time polymorphism.The
concept is called LATE BINDING. It is also known as DYNAMIC BINDING.
1. Operator overloading allows you to redefine the way operator works for user-defined types
only (objects, structures). It cannot be used for built-in types (int, float, char etc.).
2. Two operators = and & are already overloaded by default in C++. For example: To copy objects
of same class, you can directly use = operator. You do not need to create an operator function.
3. Operator overloading cannot change the precedence and associatively of operators. However,
if you want to change the order of evaluation, parenthesis should be used.
4. There are 4 operators that cannot be overloaded in C++. They are :: (scope
resolution), . (member selection), .* (member selection through pointer to function)
and ?: (ternary operator).
Q.What is operator overloading? Explain unary and binary operator
overloading.
Ans:To define an additional task to an operator,we must specify what it means in relation to
the class to which the operator is applied.This is done with the help of a special function,called
operator function,which describes the task.
Syntax:
Return_type classname::operator op(arglist)
{
Function body;
}
1.UNARY OPERATOR OVERLOADING:
The unary operators operate on a single operand and following are the examples of Unary
operators −
#include<iostream>
using namespace std;
class complex {
int a, b, c;
public:
void getvalue() {
cout << "Enter the Two Numbers:";
cin >> a>>b;
}
void operator++() {
a = ++a;
b = ++b;
}
void display() {
cout << a << "+\t" << b << "i" << endl;
}
};
void main() {
complex obj;
obj.getvalue();
obj++;
}
#include<iostream>
class complex
{ private:
int real,imag;
public:
void getvalue()
cin>>real>>imag;
complex temp;
temp.real=real+obj.real;
temp.imag=imag+obj.imag;
return(temp);
void display()
cout<<real<<"+"<<"("<<imag<<")"<<"i"<<"\n";
};
int main()
{ complex c1,c2,c3,c4;
c1.getvalue();
c2.getvalue();
c3 = c1+c2;
cout<<"Result is:\n";
c3.display();
return 0;
1. The first and basic rule of operator overloading is: we can overload unary operator as
only unary operator, it cannot be overload as binary operator and vice versa.
2. We cannot overload those operators that are not a part of C++ language like ‘$’.
3. We can perform operator overloading in only user defined classes. We cannot change
the operators existing functionality.
4. Using operator overloading we cannot change the presidency and associatively of
operators.
5. There are some operators cannot be overloaded.
int a;
a=s;
public:
double length;
friend void printWidth( Box box );
void setWidth( double wid );
};
This situation frequently comes up when overloading the inserter << and extractor >> operators
for a class, to allow easy output of an object to a stream, or input of an object from a stream.
Because the object appears on the right side of these operators, the overloaded operator
functions cannot be class member functions; they must be functions defined outside the class.
It’s possible that these functions could get everything they need by accessing only public
members of the class. In that case, the functions don’t need to be declared as friends of the
class, because they don’t need access to private members. But these functions typically need to
access at least one private member, so to gain that access, they need to be declared as friends
of the class.
Unlike other operations that can be understood as external to the left hand side operator, the
assignment is an operation that is semantically bound to the left hand side: modify this
instance to be equal to the right hand side instance (by some definition of equal), so it makes
sense to have it as an operation of the class and not an external operation. On the other hand,
other operators as addition are not bound to a particular instance: is a+b an operation
of a or b or none of them? -- a and bare used in the operation, but the operation acts on
the result value that is returned.
Q10.What is inheritance? What are the types of inheritance? Give an
example of each.
Ans: The capability of a class to derive properties and characteristics from another class is
called Inheritance. Inheritance is one of the most important feature of Object Oriented
Programming.
Derived Class: The class that inherits properties from another class is called Sub class or
Derived Class.
Base Class:The class whose properties are inherited by sub class is called Base Class or Super
class.
Syntax:
int main()
{ Car obj;
return 0;
}
Output:
This is a vehicle
Syntax:
class subclass_name : access_mode base_class1, access_mode base_class2, ....
{
//body of subclass
};
#include <iostream>
class Vehicle {
public:
Vehicle()
}
};
class FourWheeler {
public:
FourWheeler()
{
}
};
};
int main()
{ Car obj;
return 0;
Output:
This is a Vehicle
This is a 4 wheeler Vehicle
int main()
{ Car obj;
return 0;
}
output:
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
4.Hierarchical Inheritance: In this type of inheritance, more than one sub class is
inherited from a single base class. i.e. more than one derived class is created from a
single base class.
#include <iostream>
using namespace std;
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class Car: public Vehicle
{
};
// second sub class
class Bus: public Vehicle
{
};
int main()
{ Car obj1;
Bus obj2;
return 0;
}
Output:
This is a Vehicle
This is a Vehicle
#include <iostream>
using namespace std;
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class Fare
{
public:
Fare()
{
cout<<"Fare of Vehicle\n";
}
};
class Car: public Vehicle
{
};
class Bus: public Vehicle, public Fare
{
};
int main()
{ Bus obj2;
return 0;
}
Output:
This is a Vehicle
Fare of Vehicle