Docsity

Complex Number Operations Using Operator Overloading in C++, Slides of Object Oriented Programming

Toggle sidebar
bg1
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++.
pf3

Partial preview of the text

Download Complex Number Operations Using Operator Overloading in C++ and more Slides Object Oriented Programming in PDF only on Docsity!

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++.

Operator Action

  • Addition
  • Subtraction
  • Multiplication / Division % Modulus 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: Over loadable operators
      • / = < > += -= = /= << >> <<= >>= == != <= >= ++ -- % & ^! | ~ &= ^= |= && || %= []  To overload an operator in order to use it with classes we declare operator functions , which are regular functions whose names are the operator keyword followed by the operator sign that we want to overload. The format is:  Type operator operator-symbol (parameters) {/...*/ }  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. Facilities: Linux Operating Systems, GCC, Eclipse framework, VC++. Algorithm:
  1. Start