I'm trying to teach myself C++, and I'm going through a basic exercise about constructors. I have one program that is behaving unexpectedly:
Fraction.h:
#include <iostream>
#ifndef FRACTION_H
#define FRACTION_H
using namespace std;
class Fraction
{
private:
int num;
int denom;
static int gcd(int a, int b);
void reduce();
public:
Fraction(int n=0, int d=1);
Fraction(Fraction& f);
~Fraction();
Fraction& operator=(const Fraction& f);
friend Fraction operator+(const Fraction& f1, const Fraction& f2);
friend ostream& operator<<(ostream& out, const Fraction& f);
};
#endif // FRACTION_H
Fraction.cpp (some implementations omitted):
#include "../include/Fraction.h"
#include <cassert>
#include <iostream>
using namespace std;
int Fraction::gcd(int a, int b) {
// implementation omitted
}
void Fraction::reduce() {
// implementation omitted
// this just reduces fractions to lowest terms, like 3/6 to 1/2
}
Fraction::Fraction(int n, int d) {
cout << "new fraction, n=" << n << ", d=" << d << endl;
assert(d != 0);
if (d < 0) {
num = -n;
denom = -d;
} else {
num = n;
denom = d;
}
reduce();
}
Fraction::Fraction(Fraction& f) {
cout << "copy fraction " << f << " at " << &f << endl;
num = f.num;
denom = f.denom;
}
Fraction::~Fraction() {
}
Fraction& Fraction::operator=(const Fraction& f) {
cout << "assign fraction to " << f << " at " << &f << endl;
if (this == &f)
return *this;
num = f.num;
denom = f.denom;
return *this;
}
Fraction operator+(const Fraction& f1, const Fraction& f2) {
cout << "adding " << f1 << " and " << f2 << endl;
return Fraction(f1.num * f2.denom + f2.num * f1.denom,
f1.denom * f2.denom);
}
ostream& operator<<(ostream& out, const Fraction& f) {
out << f.num << "/" << f.denom;
return out;
}
main.cpp:
#include "include/Fraction.h"
#include <iostream>
using namespace std;
int main()
{
Fraction f1(1, 3);
Fraction f2(1, 2);
cout << f1 << endl;
cout << f2 << endl;
cout << (f1 + f2) << endl;
return 0;
}
When I run this, the first two print statements output 1/3
and 1/2
as expected, but the third one prints 0/1
instead of 5/6
. From the debugging statements I have, I create 5/6 through the Fraction(int, int)
constructor, but for some reason it then gets called with 0/1. When I remove the copy constructor, the code prints out 5/6
. What's going on here, and how can I fix it without removing the copy constructor?
The signature for your copy constructor should be
Fraction(const Fraction&);
not
Fraction(Fraction&);
When you do return Fraction(...);
, the compiler has to call Fraction(const Fraction&)
because the fraction being returned is a temporary, but since you don't define it, your compiler makes something weird happen. Your compiler is behaving weirdly and allowing you to use the default constructor somehow, when it should kick out an error. Compiling your code as-is on gcc doesn't work, you'll have to make the modification I mentioned and that should fix it.
Also, the fact that your compiler isn't using RVO on that function hints that you are using a very old and/or sucky compiler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With