0% found this document useful (0 votes)
2 views

class_complex_1

The document describes the implementation of a Complex class in C++ to represent and manipulate complex numbers, including operations for addition and multiplication through operator overloading. It provides constructors for initializing complex numbers and includes input/output functionalities. The code demonstrates encapsulation and object-oriented programming principles, with applications in engineering, mathematics, and graphics.

Uploaded by

yashtamboli2005
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

class_complex_1

The document describes the implementation of a Complex class in C++ to represent and manipulate complex numbers, including operations for addition and multiplication through operator overloading. It provides constructors for initializing complex numbers and includes input/output functionalities. The code demonstrates encapsulation and object-oriented programming principles, with applications in engineering, mathematics, and graphics.

Uploaded by

yashtamboli2005
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

PRACTICAL 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.
*/
#include <iostream>
using namespace std;
class complex
{
float realp, imagp;
public:
complex() : realp(0), imagp(0) {} // Default constructor with initializer list
complex(float x, float y); // Parameterized constructor
complex operator+(const complex &) const; // Addition operator
complex operator*(const complex &) const; // Multiplication operator
friend istream &operator>>(istream &, complex &);
friend ostream &operator<<(ostream &, const complex &);
};
complex::complex(float x, float y) : realp(x), imagp(y) {} // Parameterized
constructor definition
istream &operator>>(istream &din, complex &c)
{
cout << "Enter real part of complex number: ";
din >> c.realp;
cout << "Enter imaginary part of complex number: ";
din >> c.imagp;
return din;
}
ostream &operator<<(ostream &dout, const complex &c)
{
dout << c.realp;
if (c.imagp >= 0)
dout << " + " << c.imagp << "i";
else
dout << " - " << -c.imagp << "i";
dout << endl;
return dout;
}
complex complex::operator+(const complex &c) const
{
complex temp;
temp.realp = realp + c.realp;
temp.imagp = imagp + c.imagp;
return temp;
}
complex complex::operator*(const complex &c) const
{
complex mul;
mul.realp = (realp * c.realp) - (imagp * c.imagp);
mul.imagp = (imagp * c.realp) + (realp * c.imagp);
return mul;
}
int main()
{
complex c1(1.2, 2.2), c2, c3;
cout << "Complex number 1 is: " << c1;
cout << "Enter complex number 2:\n";
cin >> c2;
cout << "Complex number 2 is: " << c2;
c3 = c1 + c2;
cout << "\nAddition of two complex numbers is: " << c3;
c3 = c1 * c2;
cout << "\nMultiplication of two complex numbers is: " << c3;
return 0;
}
/*
Output:
Complex number 1 is: 1.2 + 2.2i
Enter complex number 2:
Enter real part of complex number: 2
Enter imaginary part of complex number: 3
Complex number 2 is: 2 + 3i
Addition of two complex numbers is: 3.2 + 5.2i
Multiplication of two complex numbers is: -4.2 + 8i

Theory
The code defines a class complex to represent and manipulate complex numbers in C+
+. It includes features like addition, multiplication, and input/output operations
for complex numbers. Complex numbers have two parts: a real part and an imaginary
part, and the class encapsulates these operations using operator overloading.

Definition

𝑎+𝑏𝑖 a+bi, where:


A Complex Number is a number expressed in the form

a: Real part
b: Imaginary part
i: Imaginary unit (

Algorithm
1. Constructor

0+0𝑖.
Default Constructor: Initializes the complex number to

Parameterized Constructor: Accepts real and imaginary parts to initialize the


complex number.
2. Overloaded Operators
Addition (operator+):

Add the real parts and imaginary parts separately.


Multiplication (operator*):

Multiply two complex numbers using the distributive property:


Input (operator>>):

Read the real and imaginary parts from the user.


Output (operator<<):
Advantages:
Encapsulation of complex number operations in a single class.
Operator overloading simplifies usage, making it intuitive to perform arithmetic
operations.
Input and output operators enhance usability.
Disadvantages
Implementation is specific to complex numbers; not reusable for other mathematical
types.
Overloading may add slight overhead and complexity for larger programs.
Limited to two basic operations (addition and multiplication) in the current
implementation.
Applications
Engineering and Physics:
Solve problems involving alternating current (AC) circuits, wave equations, etc.
Mathematics:
Used in solving polynomial equations and transformations.
Graphics and Signal Processing:
Complex numbers are used in transformations and Fourier analysis.
Conclusion
This code provides a simple yet effective implementation of a complex class in C++
using operator overloading. The default and parameterized constructors ensure
flexibility, while overloaded operators simplify arithmetic operations. The program
demonstrates encapsulation, making it a good example of object-oriented
programming.
*/
***********************************************************************************
*******************************************

You might also like