Assignment No: 1
Title: Complex numbers operations using operator overloading
Aim:
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 constructor, default constructor, copy, constructor, parameterized
constructor’s operator overloading using friend function.
Theory:
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.
An Example of Operator Overloading
Complex a(1.2,1.3); //this class is used to represent complex numbers
Complex b(2.1,3); //notice the construction taking 2 parameters for the real and imaginary
part
Complex c = a+b; //for this to work the addition operator must be overloaded
Arithmetic Operators
Arithmetic Operators are used to do basic arithmetic operations like addition, subtraction,
multiplication, division, and modulus.
The following table list the arithmetic operators used in C++.