Question

In: Computer Science

Must be in C++ (beginners coding class) 8. a. Rewrite the definition of the class complexType...

Must be in C++ (beginners coding class)

8.

a. Rewrite the definition of the class complexType so that the arith-metic and relational operators are overloaded as nonmember functions.

b. Write the definitions of the member functions of the class complexType as designed in part a.

c. Write a test program that tests various operations on the class complexType as designed in parts a and b. Format your answer with two decimal places.

(additional info/problem ) does not need to be answered only number 8 needs to be answered

a. Extend the definition of the class complexType so that it performs the subtraction and division operations. Overload the operators subtrac-tion and division for this class as member functions.

If (a, b) and (c, d ) are complex numbers:

(a, b)-(c, d )=(a - c, b - d ).

If (c, d ) is nonzero:

(a, b)/(c, d)=((ac + bd )/(c2+ d2), (-ad + bc)/(c2+ d2)).

b. Write the definitions of the functions to overload the operators - and / asdefined in part a.

c. Write a test program that tests various operations on the classcomplexType. Format your answer with two decimal places.

Solutions

Expert Solution

#include<iostream>
#include<iomanip>
using namespace std;

class complexType{
   private:
       double real,imag;
      
   public:
       complexType(){
           real = 0;
           imag = 0;
       }
       complexType(double r , double i){
           real = r;
           imag = i;
       }
      
       void setReal(double r){
           real = r;
       }
       void setImag(double i){
           imag = i;
       }
       double getReal(){
           return real;
       }
       double getImag(){
           return imag;
       }
      
       complexType& operator+(const complexType& other){
           double r = real + other.real;
           double i = imag + other.imag;
          
           return *(new complexType(r , i));
       }
      
       complexType& operator-(const complexType& other){
           double r = real - other.real;
           double i = imag - other.imag;
          
           return *(new complexType(r , i));
       }
      
       complexType& operator*(const complexType& other){
           double r = real * other.real - imag*other.imag;
           double i = imag*other.real + real*other.imag;
          
           return *(new complexType(r , i));
       }
      
       complexType& operator/(const complexType& other){
           double val = (other.real* other.real + other.imag*other.imag);
           double r = (real * other.real + imag*other.imag)/val;
           double i = (imag*other.real - real*other.imag)/val;
          
           return *(new complexType(r , i));
       }
      
       friend ostream& operator << (ostream& out , const complexType& other){
           out<<setprecision(2)<<fixed;
           out<<"("<<other.real<<", "<<other.imag<<")";
           return out;
       }
      
       friend istream& operator >> (istream& in , complexType& other){
           char ch;
           in>>ch>>other.real>>ch>>other.imag>>ch;
           return in;
       }
};

int main(){
   complexType c1(2,3);
   complexType c2(4,5),c3;

   c3 = c1 + c2;
   cout<<"c1 + c2 = "<<c3<<endl;
  
   c3 = c1 - c2;
   cout<<"c1 - c2 = "<<c3<<endl;
  
   c3 = c1 * c2;
   cout<<"c1 * c2 = "<<c3<<endl;
  
   c3 = c1 / c2;
   cout<<"c1 / c2 = "<<c3<<endl;
  
   return 0;
  
}


Related Solutions

CODE must using C++ language. Write the definition of the class dayTye that implements the day...
CODE must using C++ language. Write the definition of the class dayTye that implements the day of the week in a program. The class dayType should store the day of the week as integer. The program should perform the following operations on an object of type dayType 1. set the day 2. display the day as a string - Sunday, ... Saturday 3. return the day as an integer 4. return the next as an integer 5. return the previous...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data:  Account holder’s name (string)  Account number (int)  Account type (string, check/savings/business)  Balance (double)  Interest rate (double) – store interest rate as a decimal number.  Add appropriate member functions to manipulate an object. Use a static member in the class to automatically assign account numbers. 1.2 Implement...
Note- can you please rewrite the code in C++ Write a class declaration named Circle with...
Note- can you please rewrite the code in C++ Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159 * radius * radius
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight { String stopLight = "red"; String waitLight; String goLight; public void setStopLight(String colour) { stopLight = colour; } public String getGreenLight() { return goLight; } } Question 21 Not yet answered Marked out of 1.00 Flag question Question text D3a - [1 Mark] How many field attributes are there in the TrafficLight class? Answer: Question 22 Not yet answered Marked out of 1.00 Flag...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight { String stopLight = "red"; String waitLight; String goLight; public void setStopLight(String colour) { stopLight = colour; } public String getGreenLight() { return goLight; } } 1 :How many field attributes are there in the TrafficLight class? 2 :Name a field attribute for this class. 3 :What is the name of the method that is an accessor? 4 :What is the name of the...
needs to be done in C++ Q6. Coding Question (you must code this problem and submit...
needs to be done in C++ Q6. Coding Question (you must code this problem and submit via Blackboard): Keep everything as Integers. Row and Column Numbering starts at 1. Equation for each Cell :   Coll = (Row * Col * 10) Using Nested Loops display the following exactly as it is shown below:                                  Columns % Brand         1           2         3          4               A            10         20       30       40          B            20         40        60       80           C            30         60        90    ...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the function does not need to be declared and defined separately • No global variables are allowed • No separate.hor.hpp are allowed • You may not use any of the hash tables, or hashing functions provided by the STL or Boost library to implement your hash table • Appropriate, informative messages must be provided for prompts and outputs You must implement a hash table using...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the function does not need to be declared and defined separately • No global variables are allowed • No separate.hor.hpp are allowed • You may not use any of the hash tables, or hashing functions provided by the STL or Boost library to implement your hash table • Appropriate, informative messages must be provided for prompts and outputs You must implement a hash table using...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the function does not need to be declared and defined separately • No global variables are allowed • No separate.hor.hpp are allowed • You may not use any of the hash tables, or hashing functions provided by the STL or Boost library to implement your hash table • Appropriate, informative messages must be provided for prompts and outputs You must implement a hash table using...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT