![C-C++-Programming](https://connectkreations.com/wp-content/uploads/2023/02/C-C-Programming.jpg)
![C-C++-Programming](https://connectkreations.com/wp-content/uploads/2023/02/C-C-Programming.jpg)
Problem Statement
Write and execute a C++ program to implement the complex number class.
Algorithm
Here’s an algorithm for a C++ class to implement complex numbers:
- Start by defining the class structure with its data members to store the real and imaginary parts of the complex number.
- Define a constructor to initialize the real and imaginary parts of the complex number. You can use default arguments to initialize the real and imaginary parts to zero, in case the user does not provide any values.
- Overload the ‘+’ and ‘-‘ operators to perform addition and subtraction of two complex numbers.
- Overload the ‘*’ operator to perform multiplication of two complex numbers.
- Overload the ‘/’ operator to perform division of two complex numbers.
- Implement a method to calculate the magnitude of the complex number.
- Implement a method to calculate the phase of the complex number, if necessary.
- Implement a method to display the complex number in the format “a + bi”, where “a” is the real part and “bi” is the imaginary part.
- Test the implementation by creating objects of the class and performing operations on them, such as addition, subtraction, multiplication, and division.
Code Implementation Method-1
#include<bits/stdc++.h>
using namespace std;
class Complex {
public:
int real, imaginary;
Complex(int tempReal = 0, int tempImaginary = 0)
{
real = tempReal;
imaginary = tempImaginary;
}
Complex addComp(Complex C1, Complex C2)
{
Complex temp;
temp.real = C1.real + C2.real;
temp.imaginary = C1.imaginary + C2.imaginary;
return temp;
}
};
// Main Class
int main()
{
Complex C1(3, 2);
cout<<"Complex number 1 : "<< C1.real
<< " + i"<< C1.imaginary<<endl;
Complex C2(9, 5);
cout<<"Complex number 2 : "<< C2.real
<< " + i"<< C2.imaginary<<endl;
Complex C3;
C3 = C3.addComp(C1, C2);
cout<<"Sum of complex number : "
<< C3.real << " + i"
<< C3.imaginary;
}
Code Implementation Method-2
#include <iostream>
#include <cmath>
class Complex {
private:
double real, imag;
public:
Complex(double real = 0, double imag = 0) : real(real), imag(imag) {}
Complex operator+(const Complex &other) const {
return Complex(real + other.real, imag + other.imag);
}
Complex operator-(const Complex &other) const {
return Complex(real - other.real, imag - other.imag);
}
Complex operator*(const Complex &other) const {
return Complex(real * other.real - imag * other.imag, real * other.imag + imag * other.real);
}
Complex operator/(const Complex &other) const {
double denominator = other.real * other.real + other.imag * other.imag;
return Complex((real * other.real + imag * other.imag) / denominator, (imag * other.real - real * other.imag) / denominator);
}
double magnitude() const {
return sqrt(real * real + imag * imag);
}
double phase() const {
return atan2(imag, real);
}
void display() const {
std::cout << real << " + " << imag << "i" << std::endl;
}
};
int main() {
Complex a(1, 2), b(3, 4), c;
c = a + b;
std::cout << "Addition: ";
c.display();
c = a - b;
std::cout << "Subtraction: ";
c.display();
c = a * b;
std::cout << "Multiplication: ";
c.display();
c = a / b;
std::cout << "Division: ";
c.display();
return 0;
}
Explore More Projects on Matlab
- Text Analysis of Wikipedia Page – “Apple” using PythonText Analysis of Wikipedia Page – “Apple” 1. Introduction 1.1 Project Overview This project aims to perform a comprehensive text… Read more: Text Analysis of Wikipedia Page – “Apple” using Python
- Digital Communication Projects using MatlabDigital Communication Projects using Matlab. 1. Multiple Input Multiple Output(MIMO) using Matlab Output 2. Pulse Width Modulation(PWM) working Principal using… Read more: Digital Communication Projects using Matlab
- Write and execute a C++ program to implement the complex number classProblem Statement Write and execute a C++ program to implement the complex number class. Algorithm Here’s an algorithm for a… Read more: Write and execute a C++ program to implement the complex number class
- C++ and Python Program for Staff Management in Organization/CompanyYou need to create a base class called Manager and include all the general data members like name, age, employee… Read more: C++ and Python Program for Staff Management in Organization/Company
- C program that implements a simple banking systemIntroduction This program implements a basic banking system with a balance variable that keeps track of the current balance. The… Read more: C program that implements a simple banking system
Join us for Regular Updates
Telegram | Join Now |
Join Now | |
Join Now | |
Join Now | |
Join Now | |
Join our Telegram | connectkreations |
About Connect Kreations
We the team Connect Kreations have started a Blog page which is eminently beneficial to all the students those who are seeking jobs and are eager to develop themselves in a related area. As the world is quick on uptake, our website also focuses on latest trends in recent technologies and project learning and solutions. We are continuously putting our efforts to provide you with accurate, best quality, and genuine information. Here we also have complete set of details on how to prepare aptitude, interview and more of such placement/ off campus placement preparation.
Connect Kreations is excited to announce the expansion of our services into the realm of content creation! We are now offering a wide range of creative writing services, including poetry, articles, and stories.
Whether you need a heartfelt poem for a special occasion, a thought-provoking article for your blog or website, or an engaging story to captivate your audience, our team of talented writers is here to help. We have a passion for language and a commitment to creating high-quality content that is both original and engaging.
Our services are perfect for individuals, businesses, and organizations looking to add a touch of creativity and personality to their content. We are confident that our unique perspectives and diverse backgrounds will bring a fresh and exciting voice to your project.
Thank you for choosing Connect Kreations for your content creation needs. We look forward to working with you and helping you to bring your vision to life!
The website is open to all and we want all of you to make the best use of this opportunity and get benefit from it..🤓
- Our Websites: Connect Kreations
- Our Websites: StudentsDev Connect Kreations