Question

In: Computer Science

PLEASE WRITE THIS IN C++ Write a ComplexNumber class that has two member variables realNum and...

PLEASE WRITE THIS IN C++

Write a ComplexNumber class that has two member variables realNum and complexNum of type double. Your class shall have following member functions

Complex();

Complex(double, double);

Complex add(Complex num1, Complex num2);

Complex subtract(Complex num1, Complex num2);

string printComplexNumber();

Write a driver program to test your code. Please make sure your code is separated into Complex.h Containing definition of the class Complex.cpp Containing the implementation ComplexTestProgram.cpp Containing main program Use the following directive in class definition to prevent duplicate inclusions . #ifndef COMPLEX_H #define COMPLEX_H ... #endif

Your Output should be like

Enter real and imaginary part of first complex number

5

6

Enter real and imaginary part of second complex number

4

9

Sum of two complex numbers is 9+15i

Enter real and imaginary part of first complex number

-7

6

Enter real and imaginary part of second complex number

9

-3

Sum of two complex numbers is 2+3i

Enter real and imaginary part of first complex number

-6

-6

Enter real and imaginary part of second complex number

-3

-8

Sum of two complex numbers is -9+-14i

Solutions

Expert Solution

PROGRAM:

#include <iostream>
using namespace std;

class ComplexNumber
{
private:
double realNum;
double complexNum;

public:
ComplexNumber();
ComplexNumber(double, double);
~ComplexNumber();
double getRealNum();
double getComplexNum();
void setRealNum(double realNum);

void setComplexNum(double complexNum);

void print(ComplexNumber);
ComplexNumber operator+(ComplexNumber);
ComplexNumber operator-(ComplexNumber);
};
ComplexNumber::ComplexNumber()
{
}
ComplexNumber::ComplexNumber(double realNum, double complexNum)
{
this->realNum = realNum;
this->complexNum = complexNum;
}
ComplexNumber::~ComplexNumber()
{
}
double ComplexNumber::getRealNum()
{
return realNum;
}
double ComplexNumber::getComplexNum()
{
return complexNum;
}
void ComplexNumber::setRealNum(double realNum)
{
this->realNum = realNum;
}
void ComplexNumber::setComplexNum(double complexNum)
{
this->complexNum = complexNum;
}

void ComplexNumber::print(ComplexNumber c)
{
cout << c.getRealNum() << "/" << c.getComplexNum();
}
ComplexNumber ComplexNumber::operator+(ComplexNumber c)
{
ComplexNumber a;
a.realNum = realNum + c.realNum;
a.complexNum = complexNum + c.complexNum;
return a;
}
ComplexNumber ComplexNumber::operator-(ComplexNumber c)
{
ComplexNumber a;
a.realNum = realNum - c.realNum;
a.complexNum = complexNum - c.complexNum;
return a;
}
int main()
{
// Declaring variables
double real, complex;

// Getting the first complex number entered by the user
cout << "\nEnter The First Complex Number:" << endl;
cout << "Enter Real Part:";
cin >> real;
cout << "Enter Complex Part:";
cin >> complex;

// creating an instance of ComplexNumber class
ComplexNumber c1(real, complex);
// Getting the second complex number entered by the user
cout << "\nEnter The Second Complex Number:" << endl;
cout << "Enter Real Part:";
cin >> real;
cout << "Enter Complex Part:";
cin >> complex;

// creating an instance of ComplexNumber class
ComplexNumber c2(real, complex);

// Performing addition
ComplexNumber c3 = c1 + c2;
c1.print(c1);
cout << "+";
c2.print(c2);
cout << "=";
c3.print(c3);


cout << endl;
// Performing subtraction
ComplexNumber c4 = c1 - c2;
c1.print(c1);
cout << "-";
c2.print(c2);
cout << "=";
c4.print(c4);


return 0;
}


Related Solutions

In C++ please Your class could have the following member functions and member variables. However, it's...
In C++ please Your class could have the following member functions and member variables. However, it's up to you to design the class however you like. The following is a suggestion, you can add more member variables and functions or remove any as you like, except shuffle() and printDeck(): (Note: operators must be implemented) bool empty(); //returns true if deck has no cards int cardIndex; //marks the index of the next card in the deck Card deck[52];// this is your...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Car Class Write a class named Car that has the following member variables: • year. An...
Car Class Write a class named Car that has the following member variables: • year. An int that holds the car’s model year. • make. A string object that holds the make of the car. • speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. • Constructor. The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables....
Write a Circle class that has the following member variables: radius as a double PI as...
Write a Circle class that has the following member variables: radius as a double PI as a double initialized with 3.14159 The class should have the following member functions: Default constructor Sets the radius as 0.0 and pi as 3.14159 Constructor Accepts the radius of the circles as an argument setRadius A mutator getRadius An accessor getArea Returns the area of the circle getDiameter Returns the diameter of the circle getCircumference Returns the circumference of the circle Write a program...
Write a Circle class that has the following member variables: • radius: a double • pi:...
Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the circle, which...
# List the two private member variables (including name and functionality) in the node class. #Write...
# List the two private member variables (including name and functionality) in the node class. #Write a general pattern for a loop statement that traverses all the nodes of a linked list
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the...
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the class are: NUM_EMPLOYEES: a constant integer variable to hold the number of employees. employeeId. An array of integers to hold employee identification numbers. hours. An array of integers to hold the number of hours worked by each employee payRate. An array of doubles to hold each employee’s hourly pay rate wages. An array of seven doubles to hold each employee’s gross wages The class...
C++ make a rational class that includes these members for rational -private member variables to hold...
C++ make a rational class that includes these members for rational -private member variables to hold the numerator and denominator values -a default constructor -an overloaded constructor that accepts 2 values for an initial fraction -member fractions add(), sub(), mul(), div(), less(), eq(), and neq() (less should not return true if the object is less than argument) -a member function that accepts an argument of type of ostream that writes the fraction to that open output stream do not let...
write the code in python Design a class named PersonData with the following member variables: lastName...
write the code in python Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData , which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool ....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT