patterncppMinor
Basic complex number class
Viewed 0 times
complexnumberclassbasic
Problem
As part of my C++ training, I wanted to create a basic complex number class for basic complex number calculations.
The class should have the following:
I am going to work in a place that uses an underscore at the end of every function parameter, so I am sorry in advance for any discomfort it may cause while reviewing my code.
This is really a part of my baby steps in C++, so any feedback about style, design and coding will be appreciated. Attached are both the header file and the cpp file:
```
// complex.h
// Written by me on Jan 28, 2015
#include
#include
#include "complex.h"
namespace exercises
{
// ---------- global funcs ----------
bool operator==(const Complex a_, const Complex
The class should have the following:
- constructor (non explicit, for implicit conversions)
>operators to stream withcoutandcin
==,!=operators to compare complex numbers
+,+=,-,-=,,=,/,/=operators for simple arithmetics
GetR(),GetI(),SetR(),SetI()functions to access the real and imaginary parts of the number
I am going to work in a place that uses an underscore at the end of every function parameter, so I am sorry in advance for any discomfort it may cause while reviewing my code.
This is really a part of my baby steps in C++, so any feedback about style, design and coding will be appreciated. Attached are both the header file and the cpp file:
// complex.h
// Written by me on Jan 28, 2015
namespace exercises
{
class Complex;
std::ostream& operator>(std::istream&, Complex&); // assumes (a,bi) format
bool operator==(const Complex, const Complex);
bool operator!=(const Complex, const Complex);
Complex operator+(const Complex, const Complex);
Complex operator-(const Complex, const Complex);
Complex operator*(const Complex, const Complex);
Complex operator/(const Complex, const Complex);
class Complex
{
public:
Complex(const double r_=0, const double i_=0);// non explicit on purpse
// using generated ~tor, cctor, c=tor
Complex& operator+=(const Complex);
Complex& operator-=(const Complex);
Complex& operator*=(const Complex);
Complex& operator/=(const Complex);
double GetR()const;
double GetI()const;
void SetR(const double);
void SetI(const double);
private:
double m_r;
double m_i;
};
} // namespace exercises```
// complex.h
// Written by me on Jan 28, 2015
#include
#include
#include "complex.h"
namespace exercises
{
// ---------- global funcs ----------
bool operator==(const Complex a_, const Complex
Solution
-
To avoid bugs like one mentioned by @janos, a common recommendation is to express
-
The same recommendation goes for many other operators, for example
This enforces an important invariant (
-
Speaking of getters and setters, in your case they serve no purpose other than hiding names. The access to private members is still unrestricted.
-
As a mathematician, I would expect
namely
-
To avoid bugs like one mentioned by @janos, a common recommendation is to express
operator!= in terms of operator==:bool operator!=(const Complex a_, const Complex b_) {
return !(a_ == b_);
}-
The same recommendation goes for many other operators, for example
operator+ can and should be expressed in terms of operator+=:Complex operator+(Complex a_, const Complex b_) {
return a_ += b_;
}This enforces an important invariant (
a = b + c must have the same effect as a = b; a += c), and removes the need to call getters and setters.-
Speaking of getters and setters, in your case they serve no purpose other than hiding names. The access to private members is still unrestricted.
-
As a mathematician, I would expect
Complex to have at least more methods,namely
norm and conjugate.-
const qualification of pass-by-value parameters is pointless.Code Snippets
bool operator!=(const Complex a_, const Complex b_) {
return !(a_ == b_);
}Complex operator+(Complex a_, const Complex b_) {
return a_ += b_;
}Context
StackExchange Code Review Q#79192, answer score: 8
Revisions (0)
No revisions yet.