How do I fix the output of my fraction class's overloaded + operator?

Jake TheSnake Roberts

I have a question about my fractionType class. I am coding it for an assignment and the assignment requires I overload the basic arithmetic operators (+, - , /, *) the logical operators (==, <, >, =<, =>) and the system operators (<<, >>) So far, the I have system operators working, as well as the multiplication and division operators. My problem came up when I tested the + and - operators. My input is hard coded as 2/4 and 1/8, but my output is 3/8. This is not right.

My code from fractionType.cpp

#include <iostream>
#include "fractionType.h"

fractionType::fractionType(){
}
fractionType::fractionType(int a, int b) {
  numerator = a;
  denominator = b;
}
fractionType fractionType::operator+(fractionType fraction) {
  fractionType anotherFraction;
  if (denominator = fraction.denominator) {

      anotherFraction.numerator = numerator + fraction.numerator;

      anotherFraction.denominator = denominator;
  }
if (denominator != fraction.denominator) {
    anotherFraction.numerator = ((numerator) * (fraction.denominator)) +         ((fraction.numerator) * (denominator));

    anotherFraction.denominator = denominator * fraction.denominator;
}
return anotherFraction;
}
fractionType fractionType::operator-(fractionType fraction) {

fractionType anotherFraction;

if (denominator = fraction.denominator) {

    anotherFraction.numerator = numerator - fraction.numerator;

    anotherFraction.denominator = denominator;
}
else {
    anotherFraction.numerator = (numerator * fraction.denominator) - (fraction.numerator * fraction.denominator);

    anotherFraction.denominator = denominator * fraction.denominator;
}

return anotherFraction;


}

My code from fractionType.h

#include <iostream>

class fractionType {
public: 
  fractionType(int a, int b);
  fractionType();
  fractionType operator*(fractionType fraction);
  fractionType operator/(fractionType fraction);
  fractionType operator+(fractionType fraction);
  fractionType operator-(fractionType fraction);
  friend std::ostream& operator<<(std::ostream& out, const fractionType &fraction) {
    out << fraction.numerator << '/' << fraction.denominator;
    return out;
}
friend std::istream& operator>> (std::istream& in, fractionType &fraction) {
    char c;
    in >> fraction.numerator >> c >> fraction.denominator;
    return in;
}
private:
int numerator, denominator;
};
#endif FRACTIONTYPE_H

and my source.cpp

#include <iostream>
#include "fractionType.h"

int main() {
  fractionType aFraction(2, 4);
  fractionType bFraction(1, 8);
  fractionType cFraction;
  cFraction = aFraction + bFraction;
  std::cout << cFraction << std::endl;
  system("PAUSE");
}

I cut out my operator* and operator/, because they work properly. I have no clue what I've done wrong, i've gone over the math over and over again before I posted this question. Is it because I didn't use a pointer? a const? Or is it something different? Thanks in advance for the help!

m.s.

This line is wrong:

if (denominator = fraction.denominator) {

It has to be

if (denominator == fraction.denominator) {

If warnings are enabled, clang and gcc tell you that, e.g.:

main.cpp:32:19: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]

  if (denominator = fraction.denominator) {

      ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~

live example

If you correct the above error, your code works:

output

20/32

live example

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How do I receive files via Python's BaseHTTPRequestHandler class?

来自分类Dev

How do I fix Null Pointer Exception?

来自分类Dev

How do I iterate through each line of a command's output in bash?

来自分类Dev

How do I make an output for grep fail?

来自分类Dev

How to stop Marshaller class adding XML tag in my output file

来自分类Dev

How can an operator be overloaded for different RHS types and return values?

来自分类Dev

How do i sort my listview alphabetically?

来自分类Dev

How do I link boost to my program?

来自分类Dev

How do I parse a RestSharp response into a class?

来自分类Dev

How do I redirect the output of a system() function to a variable?

来自分类Dev

If i know how a traced output looks, how can i begin creating my function? (Scheme)

来自分类Dev

Can I create a class [] operator in Typescript

来自分类Dev

How do I get the nth derivative in my scheme program?

来自分类Dev

How do i pass my scope correctly to look for variables

来自分类Dev

How do I use a Codeigniter method within my own function?

来自分类Dev

How do I get my aliases to have correct completion?

来自分类Dev

How do I use my Application Resources from another window?

来自分类Dev

How do I get MVC to find my controller path?

来自分类Dev

How do I choose the URL for my Spring Boot webapp?

来自分类Dev

How do I get my nested if statement to work in jQuery

来自分类Dev

.Net Database how do I properly close my database connections?

来自分类Dev

How do I know if my app has been minimized?

来自分类Dev

How do I get `sbt test` to recognize my custom target?

来自分类Dev

How do I know if my MacBook Pro supports SATA III?

来自分类Dev

How do I transfer my Google Chrome profile to a new PC?

来自分类Dev

How do you I add a module into my PYTHONPATH?

来自分类Dev

Any tips on how to fix my code?

来自分类Dev

How do I use an extension function in another class? C#

来自分类Dev

Error: GooglePlayServicesUtil﹕ Internal error occurred. Please see logs for detailed information. How do I fix this?

Related 相关文章

  1. 1

    How do I receive files via Python's BaseHTTPRequestHandler class?

  2. 2

    How do I fix Null Pointer Exception?

  3. 3

    How do I iterate through each line of a command's output in bash?

  4. 4

    How do I make an output for grep fail?

  5. 5

    How to stop Marshaller class adding XML tag in my output file

  6. 6

    How can an operator be overloaded for different RHS types and return values?

  7. 7

    How do i sort my listview alphabetically?

  8. 8

    How do I link boost to my program?

  9. 9

    How do I parse a RestSharp response into a class?

  10. 10

    How do I redirect the output of a system() function to a variable?

  11. 11

    If i know how a traced output looks, how can i begin creating my function? (Scheme)

  12. 12

    Can I create a class [] operator in Typescript

  13. 13

    How do I get the nth derivative in my scheme program?

  14. 14

    How do i pass my scope correctly to look for variables

  15. 15

    How do I use a Codeigniter method within my own function?

  16. 16

    How do I get my aliases to have correct completion?

  17. 17

    How do I use my Application Resources from another window?

  18. 18

    How do I get MVC to find my controller path?

  19. 19

    How do I choose the URL for my Spring Boot webapp?

  20. 20

    How do I get my nested if statement to work in jQuery

  21. 21

    .Net Database how do I properly close my database connections?

  22. 22

    How do I know if my app has been minimized?

  23. 23

    How do I get `sbt test` to recognize my custom target?

  24. 24

    How do I know if my MacBook Pro supports SATA III?

  25. 25

    How do I transfer my Google Chrome profile to a new PC?

  26. 26

    How do you I add a module into my PYTHONPATH?

  27. 27

    Any tips on how to fix my code?

  28. 28

    How do I use an extension function in another class? C#

  29. 29

    Error: GooglePlayServicesUtil﹕ Internal error occurred. Please see logs for detailed information. How do I fix this?

热门标签

归档