In: Computer Science
a)Adoubledata field(private)named realfor real part of a complex number.
b)Adoubledata field(private)named imgfor imaginarypart of a complex number.
c)A no-arg constructor that creates a default complex number with real 0and img 0.
d)Auser-defined constructorthat creates a complex number with given 2 numbers.
e)The accessor and mutator functions for realand img.
f)A constant function named addition(Complex&comp1, Complex&comp2) that returns the sum of two givencomplex numbers.
g)Aconstantfunction named subtraction(Complex&comp1, Complex&comp2) that returns the subtractionof two givencomplex numbers.
h)A constant function named multiplication(Complex&comp1, Complex&comp2) that returns the multiplicationof two givencomplex numbers.
i)Write a test program that creates aComplexobject with no-arg constructor.Then, set -4.2 and 3.1to real and imaginary parts of the complex number, respectively.
j)Create another Complexobject using the user-defined constructor.
k)Testaddition, subtractionand multiplication functions using thesetwoComplex objects.
The code for given problem statement:
#include<iostream>
using namespace std;
class Complex
{
// access: private
private:
double real,img; // real and imaginary part of complex number
// access: public
public:
Complex() // Default constructor
{
// Initializes values to 0
real=0;
img=0;
}
Complex(double r, double i)
{
// Parameterized constructor
// Initializes value at the the time of object creation
real=r;
img=i;
}
double R_accessor() // For accessing real part
{
return real;
}
double I_accessor() // For accessing imaginary part
{
return img;
}
void R_mutator(double r) // For updating value of real part
{
real=r;
}
void I_mutator(double i) // For updating value of imaginary part
{
img=i;
}
Complex addition(Complex &comp1,Complex &comp2) const
{
Complex add; // Object of Complex to store addition
double r=comp1.R_accessor()+comp2.R_accessor(); // Addition of real parts
double i=comp1.I_accessor()+comp2.I_accessor(); // Addition of imaginary parts
add.R_mutator(r);
add.I_mutator(i);
return add;
}
Complex subtraction(Complex &comp1,Complex &comp2) const
{
Complex sub; // Object of Complex to store subtraction
double r=comp1.R_accessor()-comp2.R_accessor(); // Subtraction of real parts
double i=comp1.I_accessor()-comp2.I_accessor(); // Subtraction of imaginary parts
sub.R_mutator(r);
sub.I_mutator(i);
return sub;
}
Complex multiplication(Complex &comp1,Complex &comp2) const
{
Complex mul; // Object of Complex to store multiplication
double r=comp1.R_accessor()*comp2.R_accessor()-comp1.I_accessor()*comp2.I_accessor(); // To calculate real part
double i=comp1.R_accessor()*comp2.I_accessor()+comp2.R_accessor()*comp1.I_accessor(); // To calculate imaginary part
mul.R_mutator(r);
mul.I_mutator(i);
return mul;
}
};
int main()
{
Complex a; // First object of Complex to initialize to 0+0i
// Assigning values to real and imaginary part of 'a'
a.R_mutator(-4.2);
a.I_mutator(3.1);
Complex b(1.2,2.2); // Using user defined parameterized constructor to create and initialize 2nd complex number
cout<<"1st complex number is: ";
cout<<a.R_accessor()<<" + ("<<a.I_accessor()<<"i)"<<"\n";
cout<<"2nd complex number is: ";
cout<<b.R_accessor()<<" + ("<<b.I_accessor()<<"i)"<<"\n";
Complex add=add.addition(a,b); // Adding 2 complex numbers and storing into 'add'
cout<<"Addition of given complex number is: ";
cout<<add.R_accessor()<<" + ("<<add.I_accessor()<<"i)"<<"\n";
Complex sub=sub.subtraction(a,b); // subtracting 2 complex numbers and storing into 'subtract'
cout<<"Subtraction of given complex number is: ";
cout<<sub.R_accessor()<<" + ("<<sub.I_accessor()<<"i)"<<"\n";
Complex mul=mul.multiplication(a,b); // multiplying 2 complex numbers and storing into 'add'
cout<<"Multiplication of given complex number is: ";
cout<<mul.R_accessor()<<" + ("<<mul.I_accessor()<<"i)"<<"\n";
return 0;
}
Screenshot of the code:
Screenshot of
output:
Logic
of the code:
A class is defned to store a complex number.
Comlex number is written in the form a+bi where a is real part and b is imaginary part and i=sqrt(-1)
There are 2 private data members: One for storing real part and other for storing imaginary part.
There are 2 constructors. One is default constructor to initialize both data members to 0. Second is parameterized constructor to initialize data members to given values when creating the object.
There are 2 mutators. 1 for real part and 2nd for imaginary part. Mutators are functions used to update the data members.
There are 2 accessors. 1 for real part and 2nd for imaginary part. Accessors are functions used to access the data members.
There are 3 constant functions.
1) Addition: Adds real parts and imaginary parts indivisually for provided 2 comlex objects
2) Subtraction: Subtracts real parts and imaginary parts indivisually for provided 2 comlex objects
3) Multiplication: Multiplication of 2 complex numbers is done
The example taken to test code is: -4.2+3.1i and 1.2+2.2i
Addition=(-4.2+1.2)+(3.1+2.2)i= -3 +5.3i
Subtraction=(-4.2-1.2)+(3.1-2.2)i= -5.4+0.9i
multiplication=((-4.2*1.2)-(3.1*2.2))+((-4.2*2.2)+(3.1*1.2))i = -11.86-5.52i