real basic class function help!!

hi, i just start to use a class function in c++ in visual basic and i'm stuck with this code

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
using namespace std;

class fraction {
public:
	void print();
	void set(int, int);
private:
	int multipliedBy(int, int);  // i'm stuck with here program says too much argument not sure what do i need to put in as argument??
	int dividedBy();
	int addedTo();
	int subtract();
	bool isEqualTo();

	void fraction::print()
	{
		cout << first_num << "/" << second_num;
	}
	
	void fraction::set(int first_num, int second_num)
	{
		int dummy;
		cin >> first_num;
		cin >> dummy;
		cin >> second_num;
		cin >> dummy;
	}


};

int main()  // i want to set main as fixed can not be changed
{
	fraction f1;
	fraction f2;
	fraction result;

	f1.set(9, 8);
	f2.set(2, 3);

	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;
	}
}

/*
result should looks like this

The product of 9/8 and 2/3 is 18/24
The quotient of 9/8 and 2/3 is 27/16
The sum of 9/8 and 2/3 is 43/24
The difference of 9/8 and 2/3 is 11/24
The two fractions are not equal.
*/


what do i need to fix?
Last edited on
You really should start naming your member function parameter names in your class definition, this will help document the purpose of the parameter.

In the definition you define the multiply() function using two parameters but when you try to call the function it only has one parameter.

Also since most of your member functions are in the private section of the class they can only be called by the public member functions.

Start by scrapping those cin << ...s, and have a private variable to hold them. Also it's best to initialize with a constructor.

Try reading up on this
http://www.cplusplus.com/doc/tutorial/classes/
// i want to set main as fixed can not be changed

What does that mean?

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
55
56
57
58
#include <iostream>
using namespace std;

class fraction 
{   int numerator;      //  Added member variables
    int denominator;
public:
    fraction ()         //  Added default constructor
    {   numerator = 0;
        denominator = 0;
    }
    fraction (int first, int second)    //  Added value constructor
    {   numerator = first; 
        denominator = second;
    }
	fraction multipliedBy (const fraction & f) // The argument and return type should be a fraction
	{   return fraction (this->numerator * f.numerator, this->denominator * f.denominator);
	}
	fraction dividedBy (const fraction & f);    //  TODO
	fraction addedTo (const fraction & f);      //  TODO
	fraction subtract (const fraction & f);     //  TODO
	bool isEqualTo (const fraction & f);        //  TODO

	friend ostream & operator << (ostream & os, const fraction & frac)    //  Change print to overload the >> operator
	{   os << frac.numerator << "/" << frac.denominator;
	    return os;
	}
};

int main()  // i want to set main as fixed can not be changed
{   fraction f1 (9,8);
	fraction f2 (2,3);
	fraction result;

	cout << "The product of " << f1 << " and " << f2 << " is: "; 
	result = f1.multipliedBy(f2);
	cout << result << endl;
	
	cout << "The quotient of " << f1 << " and " << f2 << " is: ";
	result = f1.dividedBy(f2);
	cout << result << endl; 
	
	cout << "The sum of " << f1 << " and " << f2 << " is: ";
	result = f1.addedTo(f2);
	cout << result << endl;
	
	cout << "The difference of " << f1 << " and " << f2 << " is: ";
	result = f1.subtract(f2);
	cout << result << endl;

	if (f1.isEqualTo(f2)) 
	{   cout << "The two fractions are equal." << endl;
	}
	else 
	{   cout << "The two fractions are not equal." << endl;
	}
	system ("pause");
}



Topic archived. No new replies allowed.