Hey guys! I'm on a new topic and I'm finding it to be a bit frustrating. And basically the way I am learning classes and ADT's is by playing with them. So, I started and I think I understand the basic concept.
I am going to post the code below that I am playing with. The part I am looking at now is how to write the Multiply function. I think once I get the concept of how to manipulate things in the implementation file, I'll be okay. The Client Code was given and is not supposed to be changed. The specification and implementation files are the ones I'm tinkering with. I was given a sort of blueprint of what to do. I've already written my default constructors but now I'm stuck at how to manipulate things. If anyone can "break it down":$ for me, I'd really appreciate it.
Specification file:
// Header file fraction.h declares class Fraction.
class Fraction
// Class Fraction represents the numerator and
// denominator of a fraction.
{
public:
// Constructors
Fraction();
// Post: Numerator and denominator have been set to zero
Fraction(int initNumerator, int initDenominator);
// Post: Numerator has been set to initNumerator;
// denominator has been set to initDenominator.
Fraction Add(Fraction frac1) const;
// Post: self + frac1 is returned.
Fraction Subtract(Fraction frac1) const;
// Post: self - frac1 is returned.
Fraction Multiply(Fraction frac1) const;
// Post: self * frac1 is returned.
Fraction Divided(Fraction frac1) const;
// Post: self / frac1 is returned.
int print() const;
// Post: print Numerator and Denominator of frac1.
private:
int numerator;
int denominator;
};
Implementation file:
//*******************************************************
// Implementation file fraction.cpp implements the member
// functions of class Fraction.
#include "fraction.h"
#include<iostream>
using namespace std;
Fraction::Fraction()
{
// FILL IN Code for default constructor.
numerator=0;
denominator=1;
}
Fraction::Fraction(int initNumerator, int initDenominator)
{
// FILL IN Code for default constructor.
numerator=initNumerator;
denominator=initDenominator;
}
Fraction Fraction::Add(Fraction frac1) const
// Pre: frac1 and self have been initialized.
// Post: frac1 + self is returned in reduced form.
{
// FILL IN Code.
}
Fraction Fraction::Subtract(Fraction frac1) const
// Pre: frac1 and self have been initialized.
// Post: self - frac1 is returned in reduced form.are
{
// FILL IN Code.
}
[B]Fraction Fraction::Multiply(Fraction frac1) const
// Pre: frac1 and self have been initialized.
// Post: self - frac1 is returned in reduced form.are
{
// FILL IN Code.
} [/B]
Fraction Fraction::Divided(Fraction frac1) const
// Pre: frac1 and self have been initialized.
// Post: self - frac1 is returned in reduced form.are
{
// FILL IN Code.
}
int Fraction::print() const
{
// FILL IN Code.
cout<<numerator<<"/"<<denominator;
}
How in the world am I supposed to "get" the first fraction to multiply it with the second one???
Client Code:
#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.Multiply(f2);
result.print();
cout << endl;
cout << "The quotient of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.Divided(f2);
result.print();
cout << endl;
cout << "The sum of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.Add(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;
const Fraction f3(12, 8);
const Fraction f4(202, 303);
result = f3.Multiply(f4);
cout << "The product of ";
f3.print();
cout << " and ";
f4.print();
cout << " is ";
result.print();
cout << endl;
system("pause");
}
Any pointers or tips? I know I am missing something in my understanding of classes...