Question: ComplexFracs.cpp included in the template: #include ComplexFracs.hpp void ComplexFrac::print() const { if (!real.isZero()) { real.print(); if (imag.isPositive()) { std::cout } if (!imag.isZero()) { imag.print(); std::cout


ComplexFracs.cpp included in the template:
#include "ComplexFracs.hpp"
void ComplexFrac::print() const {
if (!real.isZero()) {
real.print();
if (imag.isPositive()) { std::cout
}
if (!imag.isZero()) { imag.print(); std::cout
if ( real.isZero() && imag.isZero() ) { std::cout
}
ComplexFracs.hpp included in the template:
#ifndef ComplexFracs_hpp
#define ComplexFracs_hpp
#include "Fracs.hpp"
class ComplexFrac {
public:
ComplexFrac() : real(0), imag(0) {}
void print() const;
private:
Frac real;
Frac imag;
};
#endif /* ComplexFracs_hpp */
Fracs.cpp already included in the template:
#include "Fracs.hpp"
Frac Frac::add(const Frac& other) const {
return Frac(num * other.den + other.num * den, den * other.den);
}
Frac Frac::subtract(const Frac& other) const {
return Frac(num * other.den - other.num * den, den * other.den);
}
Frac Frac::multiply(const Frac& other) const {
return Frac(num * other.num, den * other.den);
}
Frac Frac::divide(const Frac& other) const {
return Frac(num * other.den, den * other.num);
}
void Frac::simplify() {
makeZeroNice();
for (int d = std::min(abs(num),den); d > 1; --d) {
if ((num % d == 0) && (den % d == 0)) {
num /= d;
den /= d;
break;
}
}
}
void Frac::Esimplify() {
int a = abs(num);
int b = den;
while (b != 0) {
int r = a % b;
a = b;
b = r;
}
num /= a;
den /= a;
}
Fracs.hpp already included in the template:
#ifndef Fracs_hpp
#define Fracs_hpp
#include
#include
#include
class Frac {
public:
Frac(int _num) : num(_num), den( 1) { }
Frac(int _num, int _den) : num(_num), den(_den) { assert(den != 0);
makeDenomPos(); Esimplify(); }
Frac() : num( 0), den( 1) {
std::cout
std::cout
std::cout
std::cout
std::cout
std::cout
exit(1);
}
void print() const { std::cout if(den != 1) { std::cout
Frac add (const Frac& other) const;
Frac subtract(const Frac& other) const;
Frac multiply(const Frac& other) const;
Frac divide (const Frac& other) const;
// The following functions make use of the fact that
// all of our fractions have positive denominators
// and that they are simplified as much as possible.
bool isEqual (const Frac& other) const { return (num == other.num) && (den == other.den); }
bool isZero() const { return num == 0; }
bool isPositive() const { return num > 0; }
private:
int num;
int den;
void makeDenomPos() { if (den
void makeZeroNice() { if (num == 0) { den = 1; } }
void simplify();
void Esimplify(); // simplify that uses the Euclidean algorithm
};
#endif /* Fracs_hpp */
- Fracs.hpp and Fracs.cpp contain the Frac class which I will define in lecture. The class interface is in the .hpp file. Any leftover member function definitions appear in the .cpp file. YOU SHOULD NOT CHANGE THESE TWO FILES. You may notice that I have destroyed the default constructor. I have done this on purpose. YOU ARE NOT ALLOWED TO FIX THIS. So do not ever call the default constructor for Frac explicitly or implicitly (by not using an initializer list in ComplexFrac). - main.cpp contains some demonstration code. You can edit this file as much as you like, and you should edit this file in order to test the functionality of the new class that you create. - ComplexFracs.hpp and ComplexFracs.cpp are the files which you will turn in. Currently there is a declaration of a class called ComplexFrac in the .hpp file. It has two private member variables: real and imag. It has one public constructor with no parameters which creates an instance of ComplexFrac with real ==0 and imag==0. It has one public member function called print. The definition of print is given in the .cpp file. You should read and understand what it is doing. The goal of this question is to add more functionality to the class ComplexFrac so that it can perform complex number arithmetic (amongst other things). A complex number is a number of the form a+bi where a and b are real numbers. a is called the real part, b is called the imaginary part, and i=1. In order to use the Frac class from lectures, we will take a and b to be fractions. All you need to know about complex numbers to answer this question is that a+bi=c+di exactly when a=c and b=d, and,,+, are defined as follows: (a+bi)+(c+di)=(a+c)+(b+d)i(a+bi)(c+di)=(ac)+(bd)i(a+bi)(c+di)=(acbd)+(ad+bc)i(a+bi)(c+di)=(a+bi)(c2+d2c+c2+d2di) Notice that I have defined division in term of multiplication (this is on purpose - see task 3). Look over the page for your list of tasks! 1. Constructors. I have already given ComplexFrac a default constructor. Define four more constructors: (a) The first should have 1 parameter of type Frac. (b) The second should have 2 parameters of type Frac. (c) The third should have 1 parameter of type int. (d) The fourth should have 4 parameters of type int. What should they do? (a) The first is for constructing a complex number with imaginary part equal to 0 . The first is for constructing a complex number with imaginary part equal to 0 . (b) The second does the "obvious" thing. (c) The third takes an integer n and constructs the complex number 1n+10i. Just like how when I wrote Frac, one constructor used n to construct the fraction 1n. It should help to recall the different constructors that Frac has. (d) The fourth takes ints re num,reden,imnum,imden tp make redenrenum+imdenimnumi. It should help to recall the different constructors that Frac has. Why do you think I chose not to make a constructor using two ints? 2. Comparison. Define a member function isEqual that performs a similar job to the function of the same name belonging to Frac. 3. Arithmetic. Declare member functions add, subtract, multiply, and divide in the .hpp file. If the parameters and/or return type are unclear, look back to what I did with Frac. Define these member functions in the .cpp file. Your division function can call your multiplication function once you create create c2+d2c+c2+d2di 4. Public versus private. The member functions you defined in 1-3 should all be public. If you need to use auxilary functions, they should be private. THERE SHOULD BE NO PUBLIC MEMBER VARIABLES. THERE SHOULD BE NO PUBLIC GETTERS OR SETTERS. 5. Const correctness. Make sure that you are const correct. 6. Testing. Make sure to use your main.cpp to test your functions. Wolfram Alpha has a good complex number calculator
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
