OOP Group A1
OOP Group A1
OOP Group A1
----------------------------------------------------------------------------------------------------------------
Assignment No: 1
----------------------------------------------------------------------------------------------------------------
Title of the Assignment: Arithmetic operations on complex numbers using
operator overloading.
----------------------------------------------------------------------------------------------------------------
Problem Statement:
1. 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.
----------------------------------------------------------------------------------------------------------------
Prerequisite:
1. Object Oriented Programming
---------------------------------------------------------------------------------------------------------------
Objective of the Assignment: To learn the concept of constructor, default constructor,
operator overloading using member function and friend function.
---------------------------------------------------------------------------------------------------------------
Outcomes:
1. Students should understand different types of Constructors.
Department of Computer Engineering Subject : OOPCGL
Operator Overloading
It is a specific case of polymorphism where different operators have different implementations
depending on their arguments. In C++ the overloading principle applies not only to functions, but to
operators too. That is, of operators can be extended to work not just with built-in types but also classes.
A programmer can provide his or her own operator to a class by overloading the built-in operator to
perform some specific computation when the operator is used on objects of that class.
With C++ feature to overload operators, we can design classes able to perform operations using
standard operators. Here is a list of all the operators that can be overloaded:
+ - * / = <> += -= *= /= <<>>
~ &= ^= |= && || %= []
• The operator keyword declares a function specifying what operator-symbol means when applied to
instances of a class. This gives the operator more than one meaning, or "overloads" it. The compiler
distinguishes between the different meanings of an operator by examining the types of its operands.
Syntax:
//function body
where,
1. Create a class that define a data types that is used in the overloading operation 2. Declare the operator
function operator op() in the public part of the class.
e.g.
A statement like
Complex No 1:
Real Part :5
Imaginary part :4
Complex No 2:
Real Part :3
Imaginary part :4
Facilities:
Algorithm:
Input:
Complex numbers with real and imaginary values for two complex numbers.
Example :
Conclusion:
Hence, we have studied the concept of operator overloading.
Questions:
1. What is operator overloading?
Department of Computer Engineering Subject : OOPCGL