Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that will include a .h file named complexNumber.h. In this .

ID: 3821280 • Letter: W

Question

Write a C++ program that will include a .h file named complexNumber.h. In this .h file is a class named complexNum. This class has two private variables named real and imaginary both of type float. It has a default constructor that sets both real and imaginary to zero. It has non-default constructor that allows user to initialize real and imaginary to any float number. It has one member function named "setReal" that takes in a float type as an input parameter then set variable real to the input parameter. It has another member function named "getReal" that simply returns a float value that is equal to the variable real. Now extend "getReal" and "setReal" specifications out to write also member functions "setImaginary" and "getImaginary";

Also class complexNum will have a member function named "increment" that has no input parameter. The function returns a complexNum that has real and imaginary data that are equal to the instance's data plus 1. Namely, if instance calling increment is a complex number 5 + 6i, the return value of the function is 6 + 7i. Make sure main tests out the member function "increment". Example:

complexNum a(6, 7);

a=a.increment(); //This will result in real of a = 7 because 6 + 1 = 7 and imaginary of a = 8 because 7 + 1 = 8

Writed the .h file.

Write a complexNumber.cpp file that satisfies the following specifications:

-Has a default constructor that sets both real and imaginary to zero.

-Has a constructor that allows user to initialize real and imaginary to any float number.

-Has member functions "setReal", "getReal", "setImaginary", "getImaginary", and "increment".

Write a main function in a file main.cpp that tests the .h and .cpp files.

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();

complexNum increment();

void display();

};
#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:: increment()
{
  
this->real = this->real +1;
this->imaginary = this->imaginary+1;
return *this;
}


void complexNum :: display()
{
cout<<real<<","<<imaginary;
}

main.cpp

#include <iostream>

using namespace std;
#include "complexNumber.h"

int main()
{
complexNum a(6, 7);
a=a.increment();
a.display();

return 0;
}

Output:

7,8

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote