A template to answer this homework is provided. It contains five files:
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 and b is called the imaginary part. 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) = (a - c) + (b - d)i
(a + bi) x (c + di) = (ac – bd) + (ad + bc)i
(a + bi) / (c + di) = (a + bi) x (((c / (c2 + d2)) + (-d / (c2 + d2)))i)
Notice that I have defined division in term of multiplication. Look over the page for your list of tasks!
I have already given ComplexFrac a default constructor. Define four more constructors:
What should they do? You can hopefully guess, but....
Why do you think I chose not to make a constructor using two ints?
Define a member function isEqual that performs a similar job to the function of the same name belonging to Frac.
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.
You should expect defining multiplication and division to be a bit awkward. In some ways, it would be easier if we had overloaded the +, -, *, / operators, but we're delaying a detailed discussion of such things until PIC 10B.
Your division function can call your multiplication function once you create (((c / (c2 + d2)) + (-d / (c2 + d2)))i)
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.
Make sure that you are const correct.
Make sure to use your main.cpp to test your functions. Wolfram Alpha has a good complex number calculator.
Fracs.hpp
#ifndef Fracs_hpp
#define Fracs_hpp
#include < iostream >
#include < cassert >
#include < algorithm >
class Frac {
public:
Frac(int _num) : num(_num), den(1) { }
Frac(int _num, int _den) : num(_num), den(_den) {
assert(den != 0);
makeDenomPos(); Msimplify();
}
Frac() : num(0), den(1) {
std::cout << "You just called the default constructor " << std::endl;
std::cout << "for Frac. If you think that you didn't, " << std::endl;
std::cout << "ask yourself whether you failed to use an " << std::endl;
std::cout << "initializer list when you should have done." << std::endl;
std::cout << "If you did intend to use the default " << std::endl;
std::cout << "constructor, don't! Remember... I broke it!" << std::endl;
exit(1);
}
void print() const { std::cout << num; if (den != 1) { std::cout << "/" << den; } }
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 < 0) { num *= -1; den *= -1; } }
void makeZeroNice() { if (num == 0) { den = 1; } }
void simplify();
void Msimplify(); // Michael's simplify - uses the Euclidean algorithm
};
#endif /* Fracs_hpp */
ComplexFracs.hpp
#ifndef ComplexFracs_hpp
#define ComplexFracs_hpp
#include "Fracs.hpp"
class ComplexFrac
{
public:
ComplexFrac()
: real(0), imag(0)
{
}
ComplexFrac(const Frac &real)
: real(real), imag(0)
{
}
ComplexFrac(const Frac &real, const Frac &imag)
: real(real), imag(imag)
{
}
ComplexFrac(int n)
{
real = Frac(n, 1);
imag = Frac(0, 1);
}
ComplexFrac(int realN, int realD, int imagN, int imagD)
{
real = Frac(realN, realD);
imag = Frac(imagN, imagD);
}
bool isEqual(const ComplexFrac &other) const;
ComplexFrac add(const ComplexFrac &other) const;
ComplexFrac subtract(const ComplexFrac &other) const;
ComplexFrac multiply(const ComplexFrac &other) const;
ComplexFrac divide(const ComplexFrac &other) const;
void print() const;
private:
Frac real;
Frac imag;
};
#endif /* ComplexFracs_hpp */