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

Language: C++ with comments Class Design Exercise OK, so now I want you to test

ID: 3914665 • Letter: L

Question

Language: C++ with comments

Class Design Exercise OK, so now I want you to test your design chops: Design & declare a class called "Phasor" (or similar name). The class must have the following capabilities 1. Be able to add, subtract, multiply & divide two members of the class 2. Be able to add, subtract, multiply & divide by a scalar quantity (use double); 3. Be able to create an instance of the class from class "complex" that we've discussed in 4. And yes, you must include the OCF and accessor methods in your declaration Your homework should be in the form of an appropriately-named header file. You do not need to implement the methods in a CPP file, but are welcome to do so I deliberately am giving you a fair amount of design freedom here. If you have any "what about..." questions-you're the engineer, so you decide

Explanation / Answer

// class Phasor design with implementation of each required method

class Phasor : public complex // inheriting from complex class so as to create an instance

{

  

private:

int real, imag; // two memebers of this class: real no. & imaginary no.

public:

  

// Constructor

Phasor(int real, int imag)

{

this->real = real;

this->imag = imag;

}

// accessor methods

void setReal(int real)

{

this->real = real;

}

// accessor methods

void setImag(int imag)

{

this->imag = imag;

}

// accessor methods

int getReal()

{

return this->real;

}

// accessor methods

int getImag()

{

return this->imag;

}

// method to add scalar quantity

void add(double num)

{

this->real += num;

}

// method to add two members of a class

void add(Phasor p)

{

this->real += p->real;

this->imag += p->imag;

}

// method to subtract scalar quantity

void subtract(double num)

{

this->real -= num;

}

// method to subtract two members of a class

void subtract(Phasor p)

{

this->real -= p->real;

this->imag -= p->imag;

}

// method to multiply scalar quantity

void multiply(double num)

{

this->real *= num;

}

// method to multiply two members of a class

void multiply(Phasor p)

{

this->real *= p->real;

this->imag *= p->imag;

}

// method to divide scalar quantity

void divide(double num)

{

this->real /= num;

}

// method to divide two members of a class

void divide(Phasor p)

{

this->real /= p->real;

this->imag /= p->imag;

}

// accessor method to print the number

void print() {

cout << endl << "Number: " << this->real << " + " << this->imag + " i";

}

};