Good Day, I will be starting a course in C++ soon and i was just looking over the internet for some examples and practise questions. I found this question very interested as these topics are on the course outline. Is it possible for you to assisst me in doing this program so i can use it as examples in practising other programs?

Question

Create a class Rational Number (fractions) with the following capabilities:
a. It should accept 2 private data members: denominator and numerator.
b. Mutators and Accessors.
c. Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions that are not in reduced form and avoids negative denominators.
d. Overload the addition, subtraction, multiplication and division operators for this class to work with two objects.
e. Overload the relational and equality operators for this class.

I only know how to create the class. This might look like a bit crappy as i am relatively new to this. I have finish my course in C and will start C++ in soon and just want some help so i can be aware when i start my course

#include <iostream.h>
#include <stdlib.h>

class fraction

private:
{
      int den, num;
      void checkdata();
}

public:
{
      fraction ();
}

You got the curly brackets a bit wrong:

#include <iostream> //omit the .h for the standard C++ libraries.
//#include <stdlib.h> //stdlib.h is a C library that is not really used in C++.

class fraction 
{ //start the { just after the class name
private:

      int den, num;  //these are called "data members"
      void checkdata(); //this is called a "member function" or "method"

public:

      fraction (); //this is the "constructor"
}; //end it after all the members have been declared.

Keep going, and you can post your progress for further comments and guidance. Just posting the code that solves the problem you posted wouldn't be very helpful to you, believe me, it will be better for you if you do most of the work.

I understand, i will read up some more and post back the results later. thanks alot for your help

This is what i have done from reading other references, im not sure if the operator overloading section is correct and i didnt find how to overload the relational and equality operator.

#include <iostream.h>
#include <stdlib.h>

class fraction
{
private:

      int den, num;
      void checkdata();


public:

      fraction();
      void print();
      int operator+(int);
      int operator-(int);
      int operator/(int);
      int operator*(int);
      
};

void fraction:: checkdata()
{
if (den=0 && den<0)

}

int fraction::operator+(fraction a, fraction b)
{
    int den = a;
    int num = b;
  return a+b;
}

int fraction::operator-(fraction c, fraction d)
{
    
}

int fraction::operator/(fraction e, fraction f)
{
    
}

int fraction::operator*(fraction g, fraction h)
{
    
}



fraction::fraction()
{
   checkdata();
}

void fraction::print()
{
     cout << num << "/" << den << endl;
     
}


main()
{
      fraction one;
      fraction two;
      int obj;
      
      obj=one+two;
      obj=one-two;
      obj=one*two;
      obj=one/two;
      
 
      print();     
system('pause');
}

It's a good start, let me give some comments:

#include <iostream.h>
#include <stdlib.h>

//if you want to use cout and endl later, you should add this line:
using namespace std;

class fraction
{
private:

      int den, num;
      bool checkdata();


public:

      fraction(); //a default constructor is not very useful in this case.
      fraction(int aNum,int aDen); //this can be used to initialize den and num.
      void print();
      //the operators need to operate on objects of class "fraction", as follows:
      fraction operator+(fraction); //actual should be "const fraction&", but for now, this is OK
      fraction operator-(fraction);
      fraction operator/(fraction);
      fraction operator*(fraction);
      
};

/*void fraction:: checkdata()
{
if (den=0 && den<0) //here (den=0) will assign the value 0 to den, it should be den==0 to test if den is equal to 0.

}*/ //this function has no effect that way
//this is more useful:
bool fraction::checkdata() //you can return a bool that signifies whether the test passed or not.
{
  return den != 0; //test that denominator is not zero.
}

fraction fraction::operator+(fraction a)
{
  //fill this yourself
  return fraction; //just return 0 for now.
}

fraction fraction::operator-(fraction a)
{
  //fill this yourself
  return fraction; //just return 0 for now.
}

fraction fraction::operator/(fraction a)
{
  //fill this yourself
  return fraction; //just return 0 for now.
}

fraction fraction::operator*(fraction a)
{
  //this is the simpler one, so I will show how to implement it:
  int newDen = den * a.den;
  int newNum = num * a.num; //just "num" takes the value of num stored in this object and a.num takes the value of num stored in object "a".
  return fraction(newDen,newNom); //see constructor below.
}

fraction::fraction()
{
   //checkdata(); //this is a useless constructor, you need to give initial values:
  den = 1;
  num = 0; //initialize to zero with a proper denominator.
}

//this is a more useful constructor:
fraction::fraction(int aNum,int aDen)
{
  den = aDen;
  num = aNum;
};

void fraction::print()
{
     cout << num << "/" << den << endl;
     
}


main()
{
      fraction one(1,2); //initialize to 1/2
      fraction two(1,6); //initialize to 1/6
      fraction obj; //the results of the operations are fractions too.
      
      obj=one+two; obj.print(); //just print the results.
      obj=one-two; obj.print();
      obj=one*two; obj.print();
      obj=one/two; obj.print();
      
 
      //print();//this print function is part of fraction, it is not global, so it needs to be called on an object (like the above obj.print() examples).     
system('pause');
}

Now, this should compile and it will output "0/1" "0/1" "1/12" and "0/1". Now you can try and fill in the remaining operators and test them out.

For the relational operators, they are essentially the same but with "bool" result types.

Little disclaimer: I didn't fully improve your implementation up to good standards because that would be meaningless for now (this is just in case someone else complains that I didn't point out a bunch of other "mistakes" that are more relevant at a more advanced state of learning C++).

Edited 6 Years Ago by mike_2000_17: n/a

Thanks alot this really helped alot, i am now able to reason out the simple operator overloading. I will go back to my reference to read up on:
1. Mutators and accessors
2. the relational and equality overloading
3. and how to reduce or simplify the fraction using the constructor

when i am finish reading i will implement my knowledge into what i have already and post it. Thanks again

This article has been dead for over six months. Start a new discussion instead.