Sei sulla pagina 1di 5

Part - A

Experiment No.07

A.1 Aim: To understand the below concept of Operator overloading.

1. To overload unary operator.


2. To overload binary operator.

And Type conversions

P1: Write a class number with one integer data member. Write a program to overload unary
operator ++ to increment data member of object of class number.

P2: Create a Box class. Overload the + operator to add two Box objects respectively. Write a
main function to implement it.

P3: Write a program to declare two classes Rupees and Dollar. Declare objects of both the
classes and perform conversion between Rupees and Dollar using any of the conversion method.

A.2 Prerequisites

Sr.No Concepts Program No


1. Overloading unary operator P1
2. Overloading Binary operator P2
3. Type conversion P3

A.3 Outcome:
After successful completion of this experiment students will be able to learn and
implement the concept of operator overloading and type conversion

A.4 Theory
1) Unary and Binary Operator Overloading

Operator overloading means assigning additional meaning to existing C++ operator. We use
special function known as operator function to define an additional task to an operator. General
form of operator function is:

Return type classname:: operator op(argument_list)

Function Body// task defined


}

Here, op is the operator being overloaded. It is preceded by keyword operator.

Operator op is a function name.

Return type is a type of value returned by the specified operation.

Operator functions must beeither member function or friend function. A member function has no
arguments for unary operators and only one for binary operators. Friend function will have only
one arguments for unary operators and two for binary operators.

We can not overload following C++ operators:

Class member access operator (. , .*)


Scope Resolution Operator( :: )
Size operator (sizeof)
Conditional operator(?:)

The process of overloading involves following steps:

Create a class that defines the data type that is to be used in the overloading operation
Declare the operatoe function operator op() in the public part of the class. It may be
either a member function or friend function
Define the operator function to implement the required operations.

To invoke overloaded unary operator we write: op x or x op

To invoke overloaded binary operator we write: x op y

Example:

#include <iostream.h>

#include<conio.h>

class Distance

private:

int feet; // 0 to infinite

int inches; // 0 to 12

public:

// required constructors
Distance(){

feet = 0;

inches = 0;

Distance(int f, int i){

feet = f;

inches = i;

// method to display distance

void displayDistance()

cout << "F: " << feet << " I:" << inches <<endl;
}

// overloaded minus (-) operator

Distance operator- ()

feet = -feet;

inches = -inches;

return Distance(feet, inches);

};

int main()

Distance D1(11, 10), D2(-5, 11);

-D1; // apply negation

D1.displayDistance(); // display D1

-D2; // apply negation

D2.displayDistance(); // display D2

return 0;
2) Type conversion:

When constants and variables of different types are mixed in an expression, C++ applies
automatic type conversion to the operands. The type of data to the right of an assignment
operator is automatically converted to the type of the variable on the left. It is true as long as that
data type is built in data type.

Three types of data conversions are possible:

1. Conversion from basic type to class type


Example:
Class time
{
int hrs,minutes;
public:
.
.
Time(int t)
{ hours=t/60;
Minutes=t%60;
}};
Time t1;
Int duration=85;
T1=duration;

2. Conversion from class type to basic type


C++ allows to define overloaded casting operator that could be used to convert class type
data to basic type.

Example: ( it coverts class type object to double)


Vector :: operator double()
{
Double sum=0;
For(int i=0; i<size; i++)
Sum=sum+v[i]*u[i];
Return sqrt(sum);
}

Operator double can be used as: double length=v1 or double length=double(v1);

3. Conversion from one class type to another class type


Conversion between objects of different classes can be carried out by either a constructor
or a conversion function.

Conversion between objects

Potrebbero piacerti anche