- Objective
- Create a class named Complex to represent complex numbers. Use the main() function and create at least three complex number objects and test all member functions of the Complex class.
- How to use classes.
- Create a program named LastnameFirstname19.cpp, that does the following:
- Note: There is no user input in this assignment.
- Copy and paste the Complex class definition from the previous assignment to this one.
- If you did not complete the previous assignment, you will need to create the class definition.
- Make any corrections to your Complex class based on the feedback provided from Assignment 18.
- Add the following member functions to the Complex class definition:
- Additional constructor with 2 parameters
- Signature: Complex(double realParam, double imaginaryParam)
- Allows a Complex object to be created with the specified real and imaginary parts.
- copy constructor
- Signature: Complex(const Complex ©Complex)
- Copies the data members from copyComplex to this object.
- destructor
- Signature: ~Complex()
- Output a message in the format: Destructor for: (a + bi) followed by a newline.
- If the imaginary part is negative (less than 0), print in the format Destructor for: (a - bi) followed by a newline.
- For example, if real is 6 and imaginary is -3: Destructor for: (6 - 3i)
- See the example program destructor.cpp
- add member function
- Signature: Complex add(const Complex &addComplex) const
- Returns a new Complex object that is this object added by the specified object.
- Formula: (a + bi) + (c + di) = (a + c) + (b + d)i
- New Real: (a + c)
- New Imaginary: (b + d)
- subtract member function
- Signature: Complex subtract(const Complex &subtractComplex) const
- Returns a new Complex object that is this object subtracted by the specified object.
- Formula: (a + bi) - (c + di) = (a - c) + (b - d)i
- New Real: (a - c)
- New Imaginary: (b - d)
- multiply member function
- Signature: Complex multiply(const Complex &multiplyComplex) const
- Returns a new Complex object that is this object multiplied by the specified object.
- Formula: (a + bi) * (c + di) = (a*c - b*d) + (b*c + a*d)i
- New Real: (a*c - b*d)
- New Imaginary: (b*c + a*d)
- divide member function
- Signature: Complex divide(const Complex ÷Complex) const
- Returns a new Complex object that is this object divided by the specified object.
- Formula: (a + bi) / (c + di) = ((a*c + b*d)/(c*c + d*d)) + ((b*c - a*d)/(c*c + d*d))i
- New Real: ((a*c + b*d) / (c*c + d*d))
- New Imaginary: ((b*c - a*d) / (c*c + d*d))
- Be sure to have a program description at the top and in-line comments.
- Be clear with your comments and output to the user, so I can understand what the program is doing.
- Additional constructor with 2 parameters
Complex Class that should be adjusted
class Complex
{
private:
double real;
double imaginary;
public:
//structure of non parameterized constructor
Complex()
{
real = imaginary = 0.0;
}
void set(double r, double i)
{
real = r; //r initialized during object creation
imaginary = i; //i initialized during object creation
}
void print() const
{
//statement that adds real part of complex and adds imaginary parts of complex numbers.
if(imaginary < 0)
if(imaginary == -1)
cout << "(" << real << " - i" << ")" << endl;
else
cout << "("<< real << " - " << imaginary << "i" << ")" << endl;
else
if(imaginary == 1)
cout << "("<< real << " + i" << ")"<< endl;
else
cout << "("<< real << " + " << imaginary << "i" << ")" <<endl;
}
//return real
double getReal()
{
return real;
}
//return imaginary
double getImaginary()
{
return imaginary;
}
};
// main class
int main()
{
//printing of first complex statement
cout<<"Test the constructor"<<endl;
Complex c1;
cout<<"Complex number complex1 is: ";
c1.print();
//printing of second complex statement
cout<<"\nTest the one set() function."<<endl;
Complex c2;
c2.set(3.3, -4.4);
cout<<"Complex number complex2 is: ";
c2.print();
//printing of third complex statement
cout<<"\nTest the two get() function."<<endl;
Complex c3;
c3.set(5.5, 6.6);
cout<<"Complex number complex3's real part is: " << c3.getReal() << endl;
cout<<"Complex number complex3's imaginary part is: " << c3.getImaginary() << endl;
return 0;
}
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 2 images
- 1- Design a class named Rectangle to represent a Rectangle. The class contains: • three variables integer data field named height, integer data filed named width and integer data filed named length. The default values are 1 for height, length, and width are 1.0 for each of them respectively. • A no-arg constructor that creates a default Rectangle. • A constructor that creates a Rectangle with the specified height, length and width. • A method named getVolume() that returns the volume of this Rectangle. (Volume = height * width * length) %3! 2- Write a test program called Test Rectangle that: • Creates two Rectangle objects: one object with height 11, length 5, and width 7, the other object with height 10, length 2 and width 6 • Display the volume of each Rectangle object.arrow_forwardWrite a graphical application that contains a class named RV whose objects are the recreational vehicle designed and digitized as described in Knowledge Exercises 20 and 21 (you can design your version as well. Simple car shape is will be appreciated. The class’s private data members should be the vehicle’s body color and (x, y) location. a) Give the UML diagram for the class. It should include a three-parameter constructor, a toString method, a method to input the values of all of an object’s data members, and a show method to draw the RV at its current (x, y) location. b) Progressively implement and test the RV class by adding a method and verifying it before adding the next method. A good order to add the methods to the class is the three-parameter constructor, followed by the toString method, the show method, and finally the inputmethod. The client code should create an RV object using the three-parameter constructor to test all of the methods as they are progressively added to the…arrow_forwardclass Student: def _init_(self, firstname, lastname, idnum): self.first = firstname self.last = lastname self.id = idnum self.courses = [] def add_course(self, course): ''' don't allow overloads or signing up for the same course twice if course not in self.courses and len(self.courses) = 4 What are all of the attributes of this class? last overload idnum add_course self first course firstname courses id clear_courses lastnamearrow_forward
- Double Bubble For this exercise you need to create a Bubble class and construct two instances of the Bubble object. You will then take the two Bubble objects and combine them to create a new, larger combined Bubble object. This will be done using functions that take in these Bubble objects as parameters. The Bubble class contains one data member, radius_, and the corresponding accessor and mutator methods for radius_, GetRadius and SetRadius. Create a member function called CalculateVolume that computes for the volume of a bubble (sphere). Use the value 3.1415 for PI. Your main function has some skeleton code that asks the user for the radius of two bubbles. You will use this to create the two Bubble objects. You will create a CombineBubbles function that receives two references (two Bubble objects) and returns a Bubble object. Combining bubbles simply means creating a new Bubble object whose radius is the sum of the two Bubble objects' radii. Take note that the CombineBubbles function…arrow_forwardPart A Create a class named Apartment that holds an apartment number as aptNumber, number of bedrooms as bedrooms, number of baths as baths, and rent amount as rent. Create a default constructor that accepts no arguments and an overloaded constructor that accepts values for each data field. Also create a get method for each field. Part B Write an application called TestApartments that creates at least five Apartment objects. Then prompt a user to enter a minimum number of bedrooms required, a minimum number of baths required, and a maximum rent the user is willing to pay. Display data for all the Apartment objects that meet the user’s criteria or an appropriate message if no such apartments are available. An example of the program is shown below: Enter minimum number of bedrooms needed >> 2 Enter minimum number of bathrooms needed >> 1.5 Enter maximum rent willing to pay >> 1200 Apartments meeting criteria of at least 2 bedrooms, at least 1.5 baths, and no more than…arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY