[try Beta version]
Not logged in

 
Class Constructor not working

Feb 16, 2013 at 1:04am
Hey there, I have a class called Mixed, which handles Mixed numbers. I've also got overloaded operators to handle arithmetic between mixed numbers, which work fine until the object is added to an Int. From what I've been told, the int should automatically "upconvert" to a Mixed type via the default constructor. I've tried several variations to no avail. Any help please?

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
 
Mixed::Mixed()
{
	integer = 0;
	numerator = 0;
	denominator = 0;
}
/*=========================================================->
The following function: Mixed( int i) 2/15/13

	This is a Mixed constructor, in the event only an integer 
	ia passed in, it sets all other values to their default,
	0,1. 

<-=========================================================*/
Mixed::Mixed(int i)
{
	integer = i;
	numerator = 0;
	denominator = 0;
}
/*=========================================================->
The following function: Mixed(const int i, const int n, const int d) 2/15/13

	This is a Mixed constructor. It set's all values
	passed in to the appropriate variable.

<-=========================================================*/
Mixed::Mixed(const int i, const int n, const int d)
{
	integer = i;
	numerator = n;
	denominator = d;
}
Feb 16, 2013 at 1:15am
Make sure that your operator takes the object by value or by const reference.
Feb 16, 2013 at 1:16am
your constructors look fine.

What error are you getting and what code is causing it?
Feb 16, 2013 at 1:26am
Peter, I just ran it that way and It failed building.

Disch, I'm not receiving an error code, it's just printing out zeros, so now I believe that my once thought good overload functions might be bad.

Ex.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Mixed operator+ (const Mixed &mixed1,  const Mixed &mixed2)
{
	Mixed fraction, a,b;
	a.ToFraction();
	b.ToFraction();

	fraction.numerator = (a.numerator * b.denominator) + (b.numerator * a.denominator);
	fraction.denominator = (a.denominator * b.denominator);
	
	fraction.Simplify();

	return fraction;

}


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
void Mixed::ToFraction()
{
		if(integer<0)
			numerator = (integer*denominator) - numerator;
		else
			numerator = (integer*denominator) + numerator;
		integer = 0;
}
void Mixed::Simplify()
{
	int greatestCommonDenominator = gcd(numerator,denominator);

	if(greatestCommonDenominator==0)
		cout << integer << endl;
	else if((denominator/greatestCommonDenominator != 0)&&(numerator/greatestCommonDenominator != 0))
	{	
		numerator=numerator/greatestCommonDenominator;
		denominator=denominator/greatestCommonDenominator;
		if(integer != 0)
			cout << integer << ' ';
		cout << numerator << "/" << denominator << endl << endl;
	}
	else
		cout << 0;
}
int Mixed::gcd(int a, int b)
{
	if(b == 0)
		return a;
	else
		return gcd(b, a%b);
}
Feb 16, 2013 at 1:28am
Peter, when I bring it back to const, I get this error C2572 'Mixed::Mixed' : redefinition of default parameter
Feb 16, 2013 at 8:31am
I meant the operators, like operator+, but I also thought you had an compiler error.

You operator+ is wrong. It doesn't even use the parameters.
Last edited on Feb 16, 2013 at 8:31am
Topic archived. No new replies allowed.