Assigement issues.

Write your question here.

This is my first major assigment in OOP in c++.
for my header i have been given the following class definition
1
2
3
4
5
6
7
8
9
10
using namespace std;
class Fraction
{
  public:
	Fraction(void);
	Fraction(int aN, int aD);
    int getNumerator();
    int getDenominator();
    Fraction adder(Fraction aF);
        ~ Fraction(void);

The assigment says that the adder function is supposed to work in a way were the current object is the first operand and the parameter of the function is the second operand. ex. If the object is 1/6 and the parameter is 1/3 then adder is supposed to give 3/6.


My questions are following:

a) Both getNumertor() and getDenominator() are set to return a integer, but i have no idea what adder(Fraction aF) is supposed to return. It says that it is supposed to return an instant of the class Fraction. It can't be a integer even though my product is supposed to produce two, if we were to go by the parameters set in the constructor.

for instance this is the cpp. i have built so far:

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
#include "fraction.h"
#include <string>



Fraction::Fraction()
{
}
Fraction::Fraction(int aN, int aD) {
	numerator = aN;
	denominator= aD;
	
	
}

int Fraction::getNumerator() {
	return numerator;
	
}
int Fraction::getDenominator() {
	return denominator;

}

Fraction Fractiom::adder(Fraction aF) {
 aF = 1/3?      
return ??
	
}


Fraction::~Fraction()
{
}


The issue isnt the arithemetics. I can figur those out. I just dont know what this function can return. I need the most simple example so just about anything compiles, just so i know which size i am dealing with(an iteger, a single string etc.)





Then if we look at the source i have made:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "fraction.h"
using namespace std;



int main() {
	
	Fraction f1(1, 6);
	
	 cout << f1.getNumerator() << "/" << f1.getDenominator() << endl;
	
  

	cin.get();
	return 0;
}


The first example just shows how i use getNumerator and getDenominator to produce a random Fraction.

b) I asume i can write f1.adder(aF) to produce my product when i get the arithemtics in the cpp right, or is that assumption wrong?
Last edited on
closed account (48T7M4Gy)
This might help you with a start on part a) adder returns another Fraction which you can access through the get functions.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>

class Fraction
{
private:
    int numerator;
    int denominator;
public:
    Fraction(int = 0, int = 0);
    ~Fraction(){};
    
    int getNumerator();
    int getDenominator();
    
    Fraction adder(Fraction);
};

Fraction::Fraction(int aN, int aD)
{
    numerator = aN;
    denominator = aD;
}

int Fraction::getNumerator()
{
    return numerator;
}

int Fraction::getDenominator()
{
    return denominator;
}

Fraction Fraction::adder(Fraction aF)
{
    Fraction total;
    total.numerator = numerator * aF.denominator + aF.numerator * denominator;
    total.denominator = denominator * aF.denominator;
    
    return total;
}

int main()
{
    Fraction A(1,3);
    Fraction B(1,6);
    
    Fraction Sum = A.adder(B);
    
    std::cout << Sum.getNumerator() << '\n';
    std::cout << Sum.getDenominator() << '\n';
    
    return 0;
}
Last edited on
thx il check it out :)
Topic archived. No new replies allowed.