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

please complete the following program in c++: In this homework assignment you will modify the program...
please complete the following program in c++: In this homework assignment you will modify the program you have developed in Homework Assignment 1 by implementing the following: 1- Your program will ask the user to choose to either Compute and Display Surface Areas of Hemispheres and their Average (Choice 1) or to Compute and Display Volumes of Hemispheres and their Average (Choice 2). The program will only perform the chosen operation. (Hint: use a character variable to read the character...
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...
C++ Using your completed LinkedList template class, developed in homework and lab, write a program that...
C++ Using your completed LinkedList template class, developed in homework and lab, write a program that implements and properly tests the following functions (these functions are not member functions of the class, you will write these function in mainTest.cpp ) 1. Function compare that receives two LinkedList objects and compares the data in the nodes of the two lists to check if they are the same. The lists are equal only if they have the same number of nodes and...
4.4 Lab: Arrays of Pointers to Structures (Sort) C++ programming This assignment is based on Homework...
4.4 Lab: Arrays of Pointers to Structures (Sort) C++ programming This assignment is based on Homework 3. The program will create an array of Airport structures. On the first line in the input file airports.txt there is an integer n, representing the number of lines in the input file. On each of the following n lines there is an airport code (a unique identifier) followed by the airport’s number of enplanements (commercial passenger boardings), and city served. You may assume...
Language: C and comment code please PThreads The goal of this homework is to show an...
Language: C and comment code please PThreads The goal of this homework is to show an understanding of pthreads and C. Assignment: Write a C program to use the bitwise operations with each bitwise operation contained within its own thread. Each thread should take 1 parameter and will only modify global values. First, create global integers x and y, then, in the main function you will read in values for x and y from the user and then create the...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT