class/header files

I'm making a class to add, subtract, multiply, and divide fractions. here's the actual .cpp file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include "fraction.h"
using namespace std;

int main()
{
	fraction f1(1,0), f2(2,2);
	fraction fAdd, fSub, fMult, fDivide;

	fAdd = f1 + f2;
	fSub = f1 - f2;
	fMult = f1 * f2;
	fDivide = f1 / f2;

	cout << "f1 : ";
	f1.printString();

	cout << "f2 : ";
	f2.printString();

	cout << "fAdd : ";
	fAdd.printString();

	cout << "fSub : ";
	fSub.printString();

	cout << "fMult : ";
	fMult.printString();

	cout << "fDivide : ";
	fDivide.printString();

	return 0;
}


and here is my class so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef FRACTION_H
#define FRACTION_H

class Fraction
{
	private:
		int numerator;
		int denominator;

	public:
		//Constructors
		Fraction(int n = 1, int d = 1)
		{ numerator = n;
		denominator = d; }
		Fraction(int d)
		{ if (d < 0)
		cout << "Denominator cant be zero."; }
		//Mutator Functions
		//Accessor Functions
};

#endif 


how do would i write an overloaded operator function to add two fractions together, to go in the header? if someone can tell me how to do that, i can figure out the other things. also, does my code look correct so far?
 
const Franction operator+(const Franction& rhs);

actually: Fraction operator + (const Fraction& rhs) const;

Also, f1 in your main is 1/0 which is not a number.
But I would insist on
 
const Franction operator+(const Franction& rhs) const ;

It's needed to avoid b + a = c
You're right.

I thought temporary objects couldn't be l-values but I guess they can!

Good call.
Ok, now here's how the function definition file looks. where do i go from here? (Ex/ adding them together)

1
2
3
4
5
6
7
8
9
#include "Fraction.h"

		Fraction Fraction::operator + (const Fraction& rhs) const;
		{
			Fraction temp;

			temp.numerator = rhs.numerator;
			temp.denominator = rhs.denominator;
		}
Topic archived. No new replies allowed.