Question

In: Computer Science

C++ PLEASE This will be a multi-part assignment. Later homework will expand upon what is developed...

C++ PLEASE

This will be a multi-part assignment. Later homework will expand upon what is developed in this assignment.

1. Create a class called MyComplex to represent complex numbers. Each part (Real and Imaginary parts) should be stored as a double. Data members should be private and you need to supply appropaiate accessor and mutator methods.

2. Supply a default constructor (both parts zero), a constructor that allows the real part to be set and a third constructor that allows both parts to be set.

3. Supply an insertion operator that will allow the value of a complex number to be printed. The output should look like: 2+3i, 1-4i, -2+0i, 0+5i for example.

4. Supply a complete program that exercises your class. (be sure you also exercise the accessor and mutator functions). You must supply a listing of your program and sample output.

Solutions

Expert Solution

//Programm is as below-

#include<iostream>

using namespace std;

class MyComplex{
  
   private:

//private data members
       double realPart;
       double imaginaryPart;
      
   public:
//defaukt constructor
       MyComplex(){
           realPart=0;
           imaginaryPart=0;
       }
      

//constructor which is setting only real part and hence imaginary part is zero
       MyComplex(double real){
           this->realPart=real;
           this->imaginaryPart= 0;
       }
      

//constructor which is setting both the real and imaginary part
       MyComplex(double real, double imaginary){
           this->realPart =real;
           this->imaginaryPart = imaginary;
          
       }
//Accesor   

double getReal(){
           return realPart;
       }
      
       double getImaginary(){
           return imaginaryPart;
       }
      

//Mutator
       void setRealPart(double real){
           this->realPart = real;
       }
      
       void setImaginaryPart(double imaginary){
           this->imaginaryPart = imaginary;
       }

//display function which will display the complext number
       void display(){
           if(imaginaryPart >= 0){
               cout << realPart << "+" <<imaginaryPart<< "i"<<"\n";
           }else{
                   cout << realPart <<imaginaryPart<< "i"<<"\n";
           }
          
       }
};

int main(){
//creating object of MyComplex class and displaying
   MyComplex complexNumber(2,3);
   complexNumber.display();
  
   MyComplex complexNumber2(1,-4);
   complexNumber2.display();
  
   MyComplex complexNumber3(-2);
   complexNumber3.display();
  
   MyComplex complexNumber4(0,5);
   complexNumber4.display();
}

// executable screen shot is as belowscreen shot is as below


Related Solutions

Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects •...
Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects • Methods • Arrays of Primitive Values • Arrays of Objects • Recursion • for and if Statements • Insertion Sort 2. Use the following Guidelines • Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). • User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for...
Please answer this multi-part question. Thank you! For this assignment you must write the following function...
Please answer this multi-part question. Thank you! For this assignment you must write the following function in Scheme: 4 Write a recursive function called mergesort that sorts a list by doing the following: (a) Use split to split the list into two roughly equal-sized partitions. (b) Recursively sort both partitions. (c) Use merge to merge the sorted partitions together. Once again you will need two base cases, one for the empty list and the other for a single-element list. >...
Please answer this multi-part question. Thank you! For this assignment you must write the following functions...
Please answer this multi-part question. Thank you! For this assignment you must write the following functions in Scheme: 4 Write a recursive function called mergesort that sorts a list by doing the following: (a) Use split to split the list into two roughly equal-sized partitions. (b) Recursively sort both partitions. (c) Use merge to merge the sorted partitions together. Once again you will need two base cases, one for the empty list and the other for a single-element list. >...
Please expand upon the arguments presented by William R. Jones, in "The Legitimacy and Necessity of...
Please expand upon the arguments presented by William R. Jones, in "The Legitimacy and Necessity of Black Philosophy." In addition, please reference the Interview on William R. Jones by George Yancy as a way to further understand the need for an African American philosophy. Joyce Mitchell Cook was the first African American Women to receive a Ph.D. in Philosophy. What are her views of a BLACK philosophy and do you think her and William R. Jones would have agreed about...
Please use markup language HTML5 please. For this homework assignment, you will create a Web site...
Please use markup language HTML5 please. For this homework assignment, you will create a Web site made up of three different pages and links between those pages Index.htm The Web pages in a site should have a similar look-and-feel. For this site, you should create a simple menu as follows: Create a horizontal line across the very top and bottom of the page. Also on the home (Index) page, create links to the other two pages. The links should appear...
Wondering where to start on this C++ homework assignment involving friend functions and overloading operators implemented...
Wondering where to start on this C++ homework assignment involving friend functions and overloading operators implemented in a Rational class. These are the instructions: Your class will need to store two internal, integer values for each Rational number, the numerator (top) and denominator (bottom) of the fraction. It will have three constructor functions, with zero, one and two arguments, used as follows:     Rational test1, test2(10), test3(1, 2); The declaration for test1 calls the default (no argument) constructor, which should...
PROGAMMING LANGUAGE: C++ Can anyone solve this assignment? Especially the extra credit part? Thanks. Please study...
PROGAMMING LANGUAGE: C++ Can anyone solve this assignment? Especially the extra credit part? Thanks. Please study the code 'Stack as Array example ' posted below. You need to implement a stack template, but this time implement the stack as a single linked list. The test of the class should be done with the same main function, slightly modified to declare a stack object and properly call the class functions. There is a helper file in the module that shows you...
Please answer both of the 2 multi-part questions: 1. 78% of people report that they are...
Please answer both of the 2 multi-part questions: 1. 78% of people report that they are sleepy two hours after they wake up. But you believe that among people who drink coffee, this rate is lower. 1a) Determine the null and alternative hypothesis. 1b) What would it mean here to make a Type I error? 2. Write the hypotheses and the conclusion for this situation: It is published that a certain kind of light bulb has a mean lifetime of...
Multi part question needing assistance please. 1. Assume that the readings at freezing on a batch...
Multi part question needing assistance please. 1. Assume that the readings at freezing on a batch of thermometers are normally distributed with a mean of 0°C and a standard deviation of 1.00°C. A single thermometer is randomly selected and tested. Find the probability of obtaining a reading greater than -1.378°C. P(Z>−1.378) _____________ ******* WHAT DO I DO ABOUT THE 8? I CAN SEE ON THE CHART THE -1.37 BUT DONT KNOW WHAT TO DO CONCERNING THE 8. 2. Assume that...
In C++ Please, using only the libraries given in this homework prompt, Write a program that...
In C++ Please, using only the libraries given in this homework prompt, Write a program that (1) prompts for two integers, (2) prints out their sum, (3) prints out the first divided by the second, and (4) prints out the natural log of the first number raised to the power of the second number. #include <iostream> #include <iomanip> using namespace std; int main() { <your answer goes here> return 0; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT