3 Replies - 1395 Views - Last Post: 12 April 2009 - 11:00 AM Rate Topic: -----

#1 steveno3131   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 19-March 09

help with making my classes

Posted 09 April 2009 - 01:56 PM

#include <iostream>
#include "fraction.h"
using namespace std;

int main()
{
    fraction f1(9,8);
    fraction f2(2,3);
    fraction result;

    cout << "The result starts off at ";
    result.print();
    cout << endl;

    cout << "The product of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.MultipliedBy(f2);
    result.print();
    cout << endl;

    cout << "The quotient of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.DividedBy(f2);
    result.print();
    cout << endl;

    cout << "The sum of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.AddedTo(f2);
    result.print();
    cout << endl;

    cout << "The difference of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.Subtract(f2);
    result.print();
    cout << endl;

    if (f1.isEqualTo(f2)){
        cout << "The two fractions are equal." << endl;
    } else {
        cout << "The two fractions are not equal." << endl;
    }
    
    const fraction f3(12, 8);
    const fraction f4(202, 303);
    result = f3.MultipliedBy(f4);
    cout << "The product of ";
    f3.print();
    cout << " and ";
    f4.print();
    cout << " is ";
    result.print();
    cout << endl;

    system ("PAUSE");//exclude statement if not using Dev-C++
    return 0;
}


Write a fraction class whose objects will represent fractions. You should provide the following member functions:

1.

Two constructors, a default constructor which assigns the value 0 to the fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the fraction, and the second parameter will represent the initial denominator of the fraction.
2.

Arithmetic operations that add, subtract, multiply, and divide fractions. These should be implemented as value returning functions that return a fraction object. They should be named AddedTo, Subtract, MultipliedBy, and DividedBy.
3.

A boolean operation named isEqualTo that compares two fraction objects for equality.
4.

An output operation named print that displays the value of a fraction object on the screen in the form numerator/denominator.


This is the client program and instructions that I have been given. Here is what I got so far.
// Fraction class specification file

class Fraction
{
public:
    void print();
    Fraction  MultipliedBy(Fraction) const;
    Fraction  DividedBy(Fraction) const;
    Fraction  AddedTo(Fraction) const;
    Fraction Subtract(Fraction) const;
    Fraction ();	// default class constructor
    Fraction (int numerator, int denominator);	// class constructor
private:
    int  numerator;
    int  denominator;
};


*** MOD EDIT: Added code tags. Please :code: ***

Is This A Good Question/Topic? 0
  • +

Replies To: help with making my classes

#2 moe_3_moe   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 12-March 09

Re: help with making my classes

Posted 09 April 2009 - 03:45 PM

"Two constructors, a default constructor which assigns the value 0 to the fraction"...i don't see that your constructor assigns zero to the fraction...usually ti follows the declaration of the class.[quote]Two constructors, a default constructor which assigns the value 0 to the fraction.
Was This Post Helpful? 0
  • +
  • -

#3 steveno3131   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 19-March 09

Re: help with making my classes

Posted 12 April 2009 - 10:53 AM

[quote name='moe_3_moe' date='9 Apr, 2009 - 02:45 PM' post='600262']
"Two constructors, a default constructor which assigns the value 0 to the fraction"...i don't see that your constructor assigns zero to the fraction...usually ti follows the declaration of the class.

Quote

Two constructors, a default constructor which assigns the value 0 to the fraction.


So would it just need Fraction=0 and would it matter where it is placed?
Was This Post Helpful? 0
  • +
  • -

#4 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: help with making my classes

Posted 12 April 2009 - 11:00 AM

View Poststeveno3131, on 12 Apr, 2009 - 09:53 AM, said:

would it matter where it is placed?


Yes it is vital that it is one of two places.

Either write the default constructor in the class implementation file or it can be written immediately after the Fraction() in the header file (to make it an inline declaration).

Here's some reading to help
http://www.glenmccl.com/bett_007.htm

You may need to review your textbook or class notes too.

This is basic stuff that has definitely been covered before you were set this assignment.

This post has been edited by janotte: 12 April 2009 - 11:02 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1