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...
Note- can you rewrite the code in C++. Circle Class c++ code Write a circle class...
Note- can you rewrite the code in C++. Circle Class c++ code 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 acccssor function...
execute coding examples that demonstrate the 4 scope rules file,function,class,block coding language c++
execute coding examples that demonstrate the 4 scope rules file,function,class,block coding language c++
In-Class coding exercise: (try - catch) (pass / fail) The purpose of this in-class coding exercise...
In-Class coding exercise: (try - catch) (pass / fail) The purpose of this in-class coding exercise is to simulate various exception conditions using the program provided. package com.InClass; /* * This program demonstrates the ability of catching different types of exceptions" * You will have to modify the method() codes to trigger various/different exceptions. * inClass Coding ( you will have to remove (comment out) the previous error condition before moving to trigger the next) * 1) throw a myExcept...
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...
C++ coding functions Implement the following class using linked lists. Creating a simple linked list class...
C++ coding functions Implement the following class using linked lists. Creating a simple linked list class to use is probably your first step.(Please do not use collections like .push() . pop(), etc.) and instead create the implementation A templated stack class that stores type T with the following public functions: - void Push(T t) - adds t to the top of the stack - T Pop() - asserts that the stack is not empty then removes and returns the item...
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    ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT