Question

In: Computer Science

Create a class for working with complex numbers. Only 2 private float data members are needed,...

Create a class for working with complex numbers. Only 2 private float data members are needed, the real part of the complex number and the imaginary part of the complex number. The following methods should be in your class:a. A default constructor that uses default arguments in case no initializers are included in the main.b. Add two complex numbers and store the sum.c. Subtract two complex numbers and store the difference.d. Multiply two complex numbers and store the product.e. Print a complex number in the form a + bi or a – bi where a is the real part of the complex number and b is the imaginary part of the complex number. For example, 4 + 5i, 3 + 0i, 0 + 4if. Change a complex number to its cube. Your main should instantiate two complex numbers and call each of the class methods. The two complex numbers should be printed along with the sum, difference, and product of the two complex numbers. Lastly, print the cubes of the two complex numbers. (Please do not overload the operators for this program.)

This is for C++

Solutions

Expert Solution

Try this once:

#include <iostream>
using namespace std;

class Complex
{
private:
float real;
float imag;

public:
Complex(float r = 0, float i = 0):real(r),imag(i){};
void setComplex(void)
{
cout << "Enter the real and imaginary parts : ";
cin >> this->real;
cin >> this->imag;
}
Complex add(const Complex& c)
{

Complex comp;
comp.real = this->real + c.real;
comp.imag = this->imag + c.imag;
return comp;
}
Complex subtract(const Complex& c)
{
Complex comp;
comp.real = this->real - c.real;
comp.imag = this->imag - c.imag;
return comp;
}
Complex multiply(const Complex&c)
{

Complex comp;
comp.real=this->real*c.real;
comp.imag=this->imag*c.imag;
return comp;
}
void square()
{
float temp=this->real;
this->real=this->real*this->real-this->imag*this->imag;
this->imag=2*this->imag*temp;
}

cout << "Setting first complex number " << endl;
a.setComplex();
cout << "Setting second complex number " << endl;
b.setComplex();
/* Adding two complex numbers */
cout << "Addition of a and b : " << endl;
c = a.add(b);
c.printComplex();
/* Subtracting two complex numbers */
cout << "Subtraction of a and b : " << endl;
d = a.subtract(b);
d.printComplex();

/* Multiplication two complex numbers */
cout << "Multiplication of a and b : " << endl;
e = a.multiply(b);
e.printComplex();

/*change the complex number to its square*/
cout << "Changing complex number a to its square: " << endl;
a.square();

a.printComplex();
}


Related Solutions

C++ Create a class for working with fractions. Only 2 private data members are needed: the...
C++ Create a class for working with fractions. Only 2 private data members are needed: the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3/7 will have the two private data member values of 3 and 7. The following methods should be in your class: a. A default constructor that should use default arguments in case no initializers are included in the main. The fraction needs to be stored in reduced...
In the following class public class Single { private float unique; ... } Create constructor, setter...
In the following class public class Single { private float unique; ... } Create constructor, setter and getter methods, and toString method. Write a program that request a time interval in seconds and display it in hours, minutes, second format. NEED THESE ASAP
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
Create a "Date" class that contains: three private data members: month day year (I leave it...
Create a "Date" class that contains: three private data members: month day year (I leave it to you to decide the type) "setters" and "getters" for each of the data (6 functions in total) One advantage of a "setter" is that it can provide error checking. Add assert statements to the setter to enforce reasonable conditions. For example, day might be restricted to between 1 and 31 inclusive. one default constructor (no arguments) one constructor with three arguments: month, day,...
Part 1 Create a class named Room which has two private data members which are doubles...
Part 1 Create a class named Room which has two private data members which are doubles named length and width. The class has five functions: a constructor which sets the length and width, a default constructor which sets the length to 12 and the width to 14, an output function, a function to calculate the area of the room and a function to calculate the parameter. Also include a friend function which adds two objects of the room class. Part...
package dealership; public abstract class Vehicle { private float dealerPrice; private int year; public Vehicle(float d,...
package dealership; public abstract class Vehicle { private float dealerPrice; private int year; public Vehicle(float d, int y) { dealerPrice = d; year = y; } public float getDealerPrice() { return dealerPrice; } public int getYear() { return year; } public abstract float getStickerPrice(); } In the space below write a concrete class Car in the dealership package that is a subclass of Vehicle class given above. You will make two constructors, both of which must call the superclass constructor....
Write a program in which define a templated class mySort with private data members as a...
Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price)...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price) { this.brand = brand; this.price = price; } public String getBrand() { return brand; } public float getPrice() { return price; } } package compstore; public class DeskTopDeals{ // assume proper variables and other methods are here public void dealOfTheDay(Desktop[] items, int numItems){ /**************************** * your code would go here * ****************************/ } } Given the above Desktop class, write code that should go...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price)...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price) { this.contractor = contractor; this.price = price; } public String getContractor() { return contractor; } public float getPrice() { return price; } } package construction; public class ContractorBids{ // assume proper variables and other methods are here public void winningBid(Bid[] bids, int numBids){ /**************************** * your code would go here * ****************************/ } } You are doing renovations on your building, and multiple contractors...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT