1.
There is a bit of confusion in the constructor and lines 60, 61.
Does this make more sense?
1 2 3 4
|
ComplexNumber(float c, float r) : complexPart(c) , realPart(r)
{
cout << "Inside the 2-argument constructor" << endl;
}
|
1 2 3 4 5 6 7 8
|
ComplexNumber* cStatic = &ComplexNumber(15.0, 10.0); // swapped these two lines to match output
ComplexNumber* cDynamic = new ComplexNumber(55.0, 77.0); // different numbers here not reversed
cout << "Printing out statically allocated object" << endl;
cStatic->print();
cout << "Printing out dynamically allocated object" << endl;
cDynamic->print();
|
Not that it makes any difference to the compiler, just for humans :+)
Some of the member functions should be marked
const
Consider using
std::unique_ptr
instead of
new
, relieve yourself of memory management , destructors etc. It works in the presence of exceptions.
You might find this video interesting:
https://www.youtube.com/watch?v=PNRju6_yn3o
Edit:
The compiler I am using is free. |
Most compilers have plenty of options for warnings, turn on as many of them as is reasonable, they are your friend. For example with g++:
g++ -std=c++17 -Wall -Wextra -pedantic-errors *.cpp -o ExecutableName
Despite all that, these ones are handy too:
http://www.cplusplus.com/forum/general/183731/#msg899203
Not sure what the story is for VS on windows, I have heard that /w4 produces too many warnings, including many from system header files. Maybe you can find the equivalent of those mentioned above, in VS if you use it?