# help with making my classes

Page 1 of 1

## 3 Replies - 1373 Views - Last Post: 12 April 2009 - 11:00 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=98305&amp;s=8ee32de668d1b559452c5663688e508e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 steveno3131

Reputation: 0
• 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.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 Subtract(Fraction) const;
Fraction ();	// default class constructor
Fraction (int numerator, int denominator);	// class constructor
private:
int  numerator;
int  denominator;
};
```

Is This A Good Question/Topic? 0

## Replies To: help with making my classes

### #2 moe_3_moe

Reputation: 1
• 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.

### #3 steveno3131

Reputation: 0
• 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?

### #4 janotte

• code > sword

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

## Re: help with making my classes

Posted 12 April 2009 - 11:00 AM

steveno3131, 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).