Define a class for complex numbers, name it myComplex. In addition to the member
ID: 3874316 • Letter: D
Question
Define a class for complex numbers, name it myComplex. In addition to the member functions that sets and gets the member attributes, define following member functions:
1. Constructors
a. A constructor uses default parameters.
b. Copy constructor
2. Mutators: setReal(), setImaginary()
3. Assessors: getReal(), getImaginary()
4. Overload output operators (<<) for myComplex class.
5. Overload input operator (>>) for myComplex class.
6. Overload assignment operator (=) for myComplex class
Write a driver program using myComplex class. Every member functions and operators should be called or activated to verify that they are working properly.
The objective of this lab is to define a class with constructors and overload input/output operators.
§ Define and implement myComplex class
§ Define and implement constructors of mComplex class
§ Overload operators: >>, <<, =
This is the input/output screen using the given driver program:
--- Result of using constructors--
c1 = 0 + 0i c2 = 3 - 2i c3 = 3 - 2i
---using input operator-------
Enter a complex number: 3 -2 c1 = 3 - 2i
---using assignment operator----
c1 = 3 - 2i c2 - 2i
press any key to continue....
To design a complex class, we will list the information (i.e. data) of a complex number and actions that a complex number can do.
A complex number:
A complex number has the form of a + bi, where a and b are real numbers and i is the square root of – 1. We refer to a as the real part and b as the imaginary part of the complex number. For example: 2.5 + 3 i is a complex number. 2.5 is the real part and 3 is the imaginary part of 2.5 + 3 i.
Member attributes:
The class should have two member attributes to represent the real and imaginary parts of a complex number.
Input operator (>>) is defined as a friend function of class complex. By doing so, the input operator can modify private member attributes. The function prototype is:
friend std::istream& operator >> (std::istream &ins, myComplex &c);
The output operator (<<) is defined as a non-member function of class complex. The function prototype takes the similar form as that of the input operator.
Have a header file, implementation file, and a driver program.
Explanation / Answer
complexNumber.h
#ifndef COMPLEX_H
#define COMPLEX_H
class complexNum
{
private:
float real;
float imaginary;
public:
complexNum();
complexNum(float real,float imaginary);
void setReal(float real);
void setImaginary(float imaginary);
float getReal();
float getImaginary();
// Copy constructor
complexNum(const complexNum &);
friend std::ostream& operator<<( std::ostream &output, const complexNum &C );
friend std::istream& operator>>( std::istream &input, complexNum &C );
complexNum operator =(complexNum const &obj);
};
#endif
complexNumber.cpp
#include <iostream>
#include "complexNumber.h"
using namespace std;
complexNum :: complexNum()
{
real = 0;
imaginary = 0;
}
complexNum :: complexNum(float real,float imaginary)
{
this->real = real;
this->imaginary = imaginary;
}
void complexNum :: setReal(float real)
{
this->real = real;
}
void complexNum :: setImaginary(float imaginary)
{
this->imaginary = imaginary;
}
float complexNum :: getReal()
{
return real;
}
float complexNum :: getImaginary()
{
return imaginary;
}
complexNum complexNum::operator =(complexNum const &obj)
{
complexNum result;
result.real = obj.real;
result.imaginary = obj.imaginary;
return result;
}
// Copy constructor
complexNum :: complexNum(const complexNum &c2) {real = c2.real; imaginary = c2.imaginary; }
std::ostream& operator<<( std::ostream &output, const complexNum &C ) {
output << C.real << " + " << C.imaginary<<"i ";
return output;
}
std::istream& operator>>( std::istream &input, complexNum &C ) {
input >> C.real >> C.imaginary;
return input;
}
main.cpp
#include <iostream>
using namespace std;
#include "complexNumber.h"
int main()
{
complexNum a(6, 7);
complexNum b(a);
complexNum c = a;
cout<<"a "<<a;
cout<<" b "<<b;
cout<<" c "<<c;
return 0;
}
Output:
a 6 + 7i
b 6 + 7i
c 6 + 7i
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.