0% found this document useful (0 votes)
24 views4 pages

OOPPRAC1

Uploaded by

Ishan Bhagwat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

OOPPRAC1

Uploaded by

Ishan Bhagwat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

❖ Title/Problem Statement: Implement a class Complex which represents the Complex

Number data type. Implement the following operations:


1. Constructor (including a default constructor which creates the complex number 0+0i).
2. Overloaded operator+ to add two complex numbers.
3. Overloaded operator* to multiply two complex numbers.
4. Overloaded << and >> to print and read Complex Numbers.

❖ Objectives: To learn the concept of Operator overloading.

❖ Software Requirements: 64-bit Open source Linux or its derivative, Open Source
C++ Programming tool like G++/GCC.
❖ Hardware Requirements: Pentium IV, 512 RAM.

❖ Theory- Concept:
You can redefine or overload most of the built-in operators available in C++. Thus a programmer
can use operators with user-defined types as well.
Overloaded operators are functions with special names the keyword operator followed by the
symbol for the operator being defined. Like any other function, an overloaded operator has a
return type and a parameter list.
Syntax:
Return_type operator op (arg-list)
Eg. complex operator+ (complex obj);

Operators which cannot be overloaded


1. Class member access operator ( . )
2. Scope resolution operator( :: )
3. Pointer to member operator (.*)
4. Conditional operator(?:)
5. Size of operator(sizeof)
Operator functions can be either member functions or friend functions.
In member functions being operator function, function takes no argument in case of unary
operator and one argument in case of binary operator
In friend functions being operator function, function takes one argument in unary operator and
two arguments in Binary Operator
1. Unary Operator:
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--. Following example explain how minus (-) operator can be
overloaded for prefix as well as postfix usage.

#include <iostream>
using namespace std;

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. Binary Operator:
Overloading with a single parameter is called binary operator overloading. Similar to unary
operators, binary operators can also be overloaded. Binary operatorsrequire two operands,
and they are overloaded by using member functions and friend functions.
Example:
using namespace std;
class temp
{
complex operator + (complex c2)
{
complex c3;
c3.x = x + c2.x;
c3.y = y + c2.y;
return c3;
}
};

❖ Algorithm:
1. Start.
2. Create class complex with data members real and imag, and member functions operator
+, operator * for overloading operators +,*,friend functions for << and >> to print and
read Complex Numbers.
3. Inside class define default constructor to initialize variables to 0+0i.
4. Define operator overloaded functions to add, multiply,<< and >> two complex numbers.
5. Create 3 objects c1, c2, c3 in main().
6. Get choice from user to get complex numbers, addition, multiplication and quit.
7. If choice 1 then call the operator overloading function >> and get two complex numbers.
Display complex numbers by calling operator overloading function << If choice 2 then
c3=c1+c2; to invoke the overloaded + operator. Call overloaded << operator to display
the result.
8. If choice 3 then c3=c1*c2; to invoke the overloaded * operator. Call overloaded <<
operator to display the result.
9. If choice 4 then exit.
10. Stop.

❖ Input: Two complex numbers.


❖ Output: Display two input complex numbers with their addition and multiplication.
❖ Conclusion:
Hence, we have successfully studied how to read, print, add and multiply two complex
numbers using >> , <<, +, * operators respectively using operator overloading functions.

You might also like