Question
I need a simple solution to the following problem, which although it has been asked and answered previously, the solutions either do not work correctly
I need a simple solution to the following problem, which although it has been asked and answered previously, the solutions either do not work correctly or are way too verbose for the level of c++ which I am currently at. This should be completed at a level where someone was just introduced to the concepts of strings and classes. The class interface should be separated from its implementation and accessed by main. I've been stuck on this for 7 days and still don't understand this problem, even after reading my textbook and watching numerous videos. Any help is appreciated, thanks:
Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form:
realPart + imaginaryPart * i
where i is
-1
Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when its declared. The constructor should contain default values of (1,1) i.e. 1 for the real part and 1 for the imaginary part. Provide public member functions that perform the following tasks:
(a) add Adds two Complex numbers: The real parts are added together and the imaginary parts are added together. (b) subtract Subtracts two Complex numbers. The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand. (c) toString Returns a string representation of a Complex number in the form (a,b), where a is the real part and b is the imaginary part.
Write a main program to test your class. Your program should prompt the user to enter two Complex numbers. If the user hits enter without entering anything, then the default value of (1,1) used by the default constructor should be used. It should then print out the sum and difference of the two Complex numbers entered. The following is a sample run:
Enter a Complex number with the real and imaginary parts separated by a comma: 4,5 Enter a Complex number with the real and imaginary parts separated by a comma: 3,2
The sum of the two complex numbers is (7,7) and the difference is (1,3).
Another sample run would be:
Enter a complex number with the real and imaginary parts separated by a comma: Enter a complex number with the real and imaginary parts separated by a comma: 3,2
The sum of the two Complex numbers is (4,3) and the difference is (-2,-1).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started