Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7757455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:05:58+00:00 2026-06-01T13:05:58+00:00

I’m trying to teach myself C++, and I’m going through a basic exercise about

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:05:59+00:00Added an answer on June 1, 2026 at 1:05 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.