# class Fraction Operator

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-27-2011
jeroconde
class Fraction Operator
Hello, Im new at this, and I would like everyone outthere that could help me in my programmin problem, please. I will really appreciated.
here is my program.

Code:

``` #include <iostream> using namespace std;         class fraction   {       public:                                       fraction ();                  //default class constructor               fraction (int num2, int den2); // class constructor                   void set (int num2, int den2);                     fraction MultipliedBy (fraction const) ;               fraction DividedBy (fraction const) ;                   fraction AddedTo (fraction const) ;           fraction Subtract (fraction const) ;           bool isEqualTo (fraction const) ;           bool isGreaterThan (fraction const);               void print() ;                       private:                                           int num;                 int den;   };         fraction::fraction ()   {           num=0;             den=1;   }             fraction::fraction (int num2, int den2)   {       num=num2;               den=den2;   }             void fraction::set (int num2, int den2)   {           num=num2;         den=den2;   }                   fraction fraction ::MultipliedBy (fraction temp)       {               fraction result;                 result.num=this->num*temp.num;             result.den=this->den*temp.den;                 return result;   }                     fraction fraction:: DividedBy (fraction temp)       {                 fraction result;             result.num=this->num*temp.den;             result.den=this->den*temp.num;                 return result;   }                   fraction fraction ::AddedTo (fraction temp)       {         fraction result;           result.den=this->den*temp.den;         result.num=(this->num*temp.den)+(this->den*temp.num);         return result;   }                   fraction fraction ::Subtract (fraction temp)         {               fraction result;                 result.den=this->den*temp.den;               result.num=(this->num*temp.den)-(this->den*temp.num);                 return result;   }                     bool fraction::isEqualTo (fraction temp)       {                 if((this->num==temp.num)&&(this->den==temp.den))               return true;           else               return false;   }         void fraction::print()     {         cout << "(" << this->num << "," << this -> den << ")";                   } // Client code. This code was given by the Professor.   int main() {     fraction f1(9,8); //calling a parameterized class constructor     fraction f2(2,3); //calling a parameterized class constructor     fraction result;  //calling a default class constructor     fraction f3; //calling a default class constructor     cout << "The result starts off at ";     result.print(); //calling an observer function     cout << endl;     cout << "The product of ";     f1.print();     cout << " and ";     f2.print();     cout << " is ";     result = f1.MultipliedBy(f2); //a class binary operation - function     result.print();     cout << endl;         f3 = result; //assignment         if (f2.isGreaterThan(f3)){ //a class relational expression - boolean operation/function         f2.print();         cout <<" is greater than ";         f3.print();         cout<<endl;     } else {         f2.print();         cout <<" is less than ";         f3.print();         cout<<endl;     }     cout << "The sum of ";     f1.print();     cout << " and ";     f2.print();     cout << " is ";     result = f1.AddedTo(f2); //a class binary operation - function     result.print();     cout << endl;     cout << "The difference of ";     f1.print();     cout << " and ";     f2.print();     cout << " is ";     result = f1.Subtract(f2); //a class binary operation - function     result.print();     cout << endl;     if (f1.isEqualTo(f2)){ //a class relational expression - boolean operation/function         cout << "The two fractions are equal." << endl;     } else {         cout << "The two fractions are not equal." << endl;     }         const fraction f4(12, 8);     const fraction f5(202, 303);     result = f4.DividedBy(f5); //a class binary operation - function     cout << "The quotient of ";     f4.print();     cout << " and ";     f5.print();     cout << " is ";     result.print();     cout << endl;                     system ("PAUSE");               return 0;          //if everything runs fine,it will return 0.          }                     // Fraction class specification file```
and my error messajes are this :

In function `int main()':
passing `const fraction' as `this' argument of `fraction fraction::DividedBy(fraction)' discards qualifiers
passing `const fraction' as `this' argument of `void fraction::print()' discards qualifiers
passing `const fraction' as `this' argument of `void fraction::print()' discards qualifiers

Thank you !!
• 07-27-2011
tabstop
If you want to be able to use a member function on a const object, then that function has to have the word "const" in it, after the parentheses but before the squiggly:
Code:

`fraction fraction:: DividedBy (fraction temp) const`
• 07-27-2011
jeroconde
So, do I have to put "const" in every function ? ex:
fraction fraction ::Subtract (fraction temp) const
and one more thing , whats the squiggly? im sorry, I didnt get that. I edited the post and I saw your repply, was it the little face?
Thank you .
• 07-27-2011
tabstop
Squiggly is what starts a function.
• 07-27-2011
jeroconde
I have another quick question, I did what you said,

Code:

```class fraction   {       public:                                       fraction ();                  //default class constructor               fraction (int num2, int den2); // class constructor                   void set (int num2, int den2);           fraction MultipliedBy (fraction) const;               fraction DividedBy (fraction) const;                   fraction AddedTo (fraction) const;           fraction Subtract (fraction) const;           bool isEqualTo (fraction) const;           bool isGreaterThan (fraction) const;               void print() ;                       private:                                           int num;                 int den;   };         fraction::fraction ()   {           num=0;             den=1;   }             fraction::fraction (int num2, int den2)   {       num=num2;               den=den2;   }             void fraction::set (int num2, int den2)   {           num=num2;         den=den2;   }                   fraction fraction ::MultipliedBy (fraction temp) const       {               fraction result;                 result.num=this->num*temp.num;             result.den=this->den*temp.den;                 return result;   }                 fraction fraction:: DividedBy (fraction temp) const       {                 fraction result;             result.num=this->num*temp.den;             result.den=this->den*temp.num;                 return result;   }                   fraction fraction ::AddedTo (fraction temp) const       {         fraction result;           result.den=this->den*temp.den;         result.num=(this->num*temp.den)+(this->den*temp.num);         return result;   }                   fraction fraction ::Subtract (fraction temp) const         {               fraction result;                 result.den=this->den*temp.den;               result.num=(this->num*temp.den)-(this->den*temp.num);                 return result;   }                     bool fraction::isEqualTo (fraction temp) const       {                 if((this->num==temp.num)&&(this->den==temp.den))               return true;           else               return false;   }         void fraction::print()     {         cout << "(" << this->num << "," << this -> den << ")";                   }```
and now I have this error
In function `int main()':
passing `const fraction' as `this' argument of `void fraction::print()' discards qualifiers
passing `const fraction' as `this' argument of `void fraction::print()' discards qualifiers

almost there I think. Thanks!
• 07-27-2011
tabstop
It's the same error, with the same fix.
• 07-27-2011
iMalc
By putting const at the end of the function declaration, you're telling the compiler that the function does not, or should not, modify any of the member variables of the class. Since print doesn't modify any, then it too deserves a const. However, if you had a method such as Invert, which just swapped the numerator and denominator of itself (rather than returning a modified copy) then that would have to be not marked as const.
const doesn't apply to constructors or destructors of course since the item either doesn't exist before or after that call.

The other things it would be good to learn about in relation to that code is passing your parameters by "const-reference" instead of by value, and also using "constructor initialisation lists".
• 07-27-2011
jeroconde
Thanks everyone for your help, I am going to try it more times, and if I can't I will post my errors again.
right now it seems that almost get it I get:

passing `const fraction' as `this' argument of `void fraction::print()' discards qualifiers
ld returned 1 exit status
its weird.
well Thank you!
PS: you guys are cool. :)
• 07-27-2011
tabstop
This is still the same error, and it is still fixed the same way.
• 07-27-2011
jeroconde
Im sorry , I meant to pu this instead of my third line:

[Linker error] undefined reference to `fraction::isGreaterThan(fraction) const'

thanks .
• 07-27-2011
tabstop
You forgot to write the function. Do so.
• 07-28-2011
jeroconde
im sorry but I couldnt figured out.
• 07-28-2011
jeroconde
so heres my code
Code:

``` // This Programs makes some operations, in order similar like a vector notation. // it multiplies what is given // Jeronimo Hernandez . // Professor : Sarkar #include <iostream> using namespace std;         class fraction   {       public:                                       fraction ();                  //default class constructor               fraction (int num2, int den2); // class constructor                   void set (int num2, int den2);           fraction MultipliedBy (fraction) const;               fraction DividedBy (fraction) const;                   fraction AddedTo (fraction) const;           fraction Subtract (fraction) const;           bool isEqualTo (fraction) const;           bool isGreaterThan (fraction) const;               void print() const;                       private:                                           int num;                 int den;   };         fraction::fraction ()   {           num=0;             den=1;   }             fraction::fraction (int num2, int den2)   {       num=num2;               den=den2;   }             void fraction::set (int num2, int den2)   {           num=num2;         den=den2;   }                   fraction fraction ::MultipliedBy (fraction temp) const       {               fraction result;                 result.num=this->num*temp.num;             result.den=this->den*temp.den;                 return result;   }                 fraction fraction:: DividedBy (fraction temp) const       {                 fraction result;             result.num=this->num*temp.den;             result.den=this->den*temp.num;                 return result;   }                   fraction fraction ::AddedTo (fraction temp) const       {         fraction result;           result.den=this->den*temp.den;         result.num=(this->num*temp.den)+(this->den*temp.num);         return result;   }                   fraction fraction::Subtract (fraction temp) const         {               fraction result;                 result.den=this->den*temp.den;               result.num=(this->num*temp.den)-(this->den*temp.num);                 return result;   }                     bool fraction::isEqualTo (fraction temp) const       {                 if((this->num==temp.num)&&(this->den==temp.den))               return true;           else               return false;   }             void print ()     {         cout << "(" << this->num << "," << this->den << ")";                   } // Client code. This code was given by the Professor.   int main() {     fraction f1(9,8); //calling a parameterized class constructor     fraction f2(2,3); //calling a parameterized class constructor     fraction result;  //calling a default class constructor     fraction f3; //calling a default class constructor     cout << "The result starts off at ";     result.print(); //calling an observer function     cout << endl;     cout << "The product of ";     f1.print();     cout << " and ";     f2.print();     cout << " is ";     result = f1.MultipliedBy(f2); //a class binary operation - function     result.print();     cout << endl;         f3 = result; //assignment         if (f2.isGreaterThan(f3)){ //a class relational expression - boolean operation/function         f2.print();         cout <<" is greater than ";         f3.print();         cout<<endl;     } else {         f2.print();         cout <<" is less than ";         f3.print();         cout<<endl;     }     cout << "The sum of ";     f1.print();     cout << " and ";     f2.print();     cout << " is ";     result = f1.AddedTo(f2); //a class binary operation - function     result.print();     cout << endl;     cout << "The difference of ";     f1.print();     cout << " and ";     f2.print();     cout << " is ";     result = f1.Subtract(f2); //a class binary operation - function     result.print();     cout << endl;     if (f1.isEqualTo(f2)){ //a class relational expression - boolean operation/function         cout << "The two fractions are equal." << endl;     } else {         cout << "The two fractions are not equal." << endl;     }         const fraction f4(12, 8);     const fraction f5(202, 303);     result = f4.DividedBy(f5); //a class binary operation - function     cout << "The quotient of ";     f4.print();     cout << " and ";     f5.print();     cout << " is ";     result.print();     cout << endl;                     system ("PAUSE");               return 0;          //if everything runs fine,it will return 0.          }```
// Fraction class specification file

and my errors

In function `void print()':
invalid use of `this' in non-member function
invalid use of `this' in non-member function
• 07-28-2011
SteveHan
you can try to delete 'this' from void print() and then compile it,,
• 07-28-2011
tabstop
It's supposed to be
Code:

`void fraction::print() const`
as mentioned about 235 times above. You're using it as a member function (things like f2.print()), you said it was a member of your class in the function definition, so do it already.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last