C ++ classifier task

As part of my degree in computer software development, one of my labs consists of creating a calculator class template and fraction class.

The problem is the fraction class. Now my task is to overload the plus operator to combine the two fractions.

Fraction.cpp:

#include "Fraction.h" const Fraction Fraction::operator+ (const Fraction &rhs) const { return Fraction(_num * rhs.GetDen() + (rhs.GetNum() * _den), _den * rhs.GetDen()); } 

Fraction.h

 #pragma once class Fraction { public: Fraction(const int &num, const int &den) : _num(num), _den(den) {} ~Fraction(void) {} const int GetNum(void) { return _num; } const int GetDen(void) { return _den; } const Fraction operator+ (const Fraction &rhs) const; private: const int _num, _den; }; 

Visual Studio complains that my fraction accessors cannot "convert this pointer from const to fraction &". I am completely puzzled.

+7
source share
5 answers

You also need to qualify your accessors as const:

 int GetNum(void) const { return _num; } 

BTW, qualifying the return type as const int, does not really make sense, it will still be int. Your compiler should issue a warning.

+12
source

The access methods GetNum and GetDen are not declared as "const", so they cannot be called inside the + operator against const Fractions.

+1
source

The first question is, why do you have a function that primarily returns a "const fraction"? This creates arbitrary restrictions on the way you use your code. Obviously, your Fraction class is already, by and large, implicitly const. No need to do this.

However, your member functions are not constants. That is, they do not tell the compiler that you will not try to change the state inside the class itself. Place the const keyword at the end of the member function declaration for all member functions that do not change state.

+1
source

Your recipients are not constants:

  int GetNum() const { return _num; } int GetDen() const { return _den; } /// ^^^^^^^^ 

You really don't need these getters. Personally, I will throw them away. Note: the class is a friend to itself and thus can refer to members of another instance.

 const Fraction Fraction::operator+ (const Fraction &rhs) const { return Fraction(_num * rhs._den+ (rhs._num * _den), _den * rhs._den); } 

Other notes:

  • Also setting void as a parameter is not a good idea (this is C style).
  • const int doesn't really matter, just makes it return an int.
  • Please do not use the underscore character as the first character. Its legal, but if you are not careful and do not know all the rules regarding underscores, it’s easy to disconnect.
+1
source
 const int GetDen(void) { return _den; } 

should (or maybe)

 int GetDen(void) const { return _den; } 

The first returns a copy of const. Returning by copy means that the copy will be generated and provided to the caller's function as a temporary object, so the const object is a read-only object. Thus, returning a copy makes a read-only copy.

The second makes your function callable even if the const fraction object is an accessible read-only function (you can tell). This is probably what you wanted to write, and if not, then better than without const.

By the way, the void parameter in the parameters is not useful in C ++, you can not print it and just say int GetNum() const{ return _num; } int GetNum() const{ return _num; }

Another detail: names starting with an underscore are reserved by the standard for standard implementations. It is not very dangerous to use, but you never know. If you want to keep them, at least encapsulate all your code in a namespace.

+1
source

All Articles