Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab Assignment / C++ main.cpp file //------------------------------ // DO NOT MODIFY NEXT LINE //------------------------------ #include #include catch.hpp using Catch::Matchers::Equals; //------------------------------ // Fix the following class

Lab Assignment / C++
main.cpp file
//------------------------------
// DO NOT MODIFY NEXT LINE
//------------------------------
#include
#include "catch.hpp"
using Catch::Matchers::Equals;
//------------------------------
// Fix the following class
class Complex {
void operator>>(std::string&) const;
void operator
};
//------------------------------
// DO NOT MODIFY TEST CASES
//------------------------------
TEST_CASE( "Assignment" ) {
SECTION( "c1" ) {
Complex c;
REQUIRE( c.re() == 0 );
REQUIRE( c.im() == 0 );
}
SECTION( "c2" ) {
Complex c{1};
REQUIRE( c.re() == 1 );
REQUIRE( c.im() == 0 );
}
SECTION( "c3" ) {
Complex c{1,1};
REQUIRE( c.re() == 1 );
REQUIRE( c.im() == 1 );
}
SECTION( "o1" ) {
Complex c; std::string s{""};
c >> s;
REQUIRE_THAT( s, Equals("0+0i") );
}
SECTION( "o2" ) {
Complex c{1, 2}; std::string s{""};
c >> s;
REQUIRE_THAT( s, Equals("1+2i") );
}
SECTION( "o3" ) {
Complex c{2, -2}; std::string s{""};
c >> s;
REQUIRE_THAT( s, Equals("2-2i") );
}
SECTION( "o4" ) {
Complex c{-2, 2}; std::string s{""};
c >> s;
REQUIRE_THAT( s, Equals("-2+2i") );
}
SECTION( "p1" ) {
Complex c;
c
REQUIRE( c.re() == 1 );
REQUIRE( c.im() == 1 );
}
SECTION( "p2" ) {
Complex c;
c
REQUIRE( c.re() == 1 );
REQUIRE( c.im() == -1 );
}
SECTION( "p3" ) {
Complex c;
c
REQUIRE( c.re() == -1 );
REQUIRE( c.im() == 0 );
}
SECTION( "p4" ) {
Complex c;
c
REQUIRE( c.re() == -1 );
REQUIRE( c.im() == -2 );
}
}
//------------------------------
image text in transcribed
image text in transcribed
Task: Formatting & Parsing The complex number is expressed in the form a +bi, where a and b are real numbers, and i represents the "imaginary unit". Write a class that defines a complex number and use streaming operations for formatting string output and parsing. Details 1. Define a class Complex that will store 2 integer coefficients of the complex number. Add the default constructor that accepts 2 int -typed parameters with default arguments which set these parameters to zero. All data members must be private. 2. Write accessor methods re and in that provide read-only access to the real and imaginary part of the Complex object. 3. Overload the stream extraction operator >> for the Complex class to output the string representation of the complex number, as "i1" where is a real and is an imaginary coefficients of the complex number. See related tests in main.cpp. The operator declaration is provided. Hint: For formatting, use append method and += operator of std::basic_string, or std::ostringstream streaming operators. 4. Overload the stream insertion operator

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Students also viewed these Databases questions

Question

=+ How well do you think you could do your job?

Answered: 1 week ago