I am doing a small assignement that has one class with two private members.....numerator and denominator. The question asks to make three overloaded constructors, some have arguements while one does not example .....fraction()

now the deconstructor is basically has to print out the two private members now this works great but when it prints out the values for the contructors with no aruguements it gives the wierd print out of
numerator: -8388484
denominator: -3883489 and so on. If you make three overloaded constructors, do you make three deconstructors tailored to each of the constructors??????

the constructor that has no arguements does not do anything as per the code below, you just call it up but don't assign anything on it.

Help???????

#include <iostream.h>

class fraction
{
private:
	int numerator;
	int denominator;

public:
	fraction(void) { cout<<"constructor\n";}
	fraction(int x);
	fraction(int x, int y);
	~fraction();
	fraction operator * (fraction);
};

fraction::fraction(int x)
{
	numerator = x;
	denominator = 1;
	cout << "single constructor\n";
}

fraction::fraction(int x, int y)
{
	numerator = x;
	denominator =y;
	cout << "double constructor\n";
}

fraction::~fraction()
{
	cout << "the numerator is " << numerator;
	cout << "\nthe denominator is " << denominator;
	cout << "\ndeconstructor\n";
}

fraction fraction::operator * (fraction a)
{ 
	fraction temp(0,0);
	temp.numerator= numerator * a.numerator;
	temp.denominator = denominator * a.denominator;

	return temp;
}

//Question 2 main
//void main(void)
//{
//	fraction one(3,5), two(5,2), three;
//	three = one * two;
//}

//Question 1 main
void main(void)
{
	fraction one(3,5), two(5,2), three, four(4);
}

I don't know how much you know so apologies if I repeat stuff you already know.

Your constructor that uses no arguments is not assigning anything as the value of numerator and denominator. For that reason, whatever happened to be stored in memory at the physical location of numerator and denominator is not overwritten with anything, and so the variables numerator and denominator attain some arbitrary, unpredictable values. Your deconstructor is printing out these values.

You can only have one destructor. If you want your destructor's behavior to be affected by whichever constructor you use, you should use a third variable that indicates which constructor is used. (It could, for example, contain a number, 1, 2, or 3.) Then base your deconstructor's behavior on that.

However, this is not a good practice; I don't recommend it. (I generally don't recommend ordinary programs having deconstructors that print out information, but maybe I'm a bit too picky.) An uninitialized fraction, when used in a program, would be intended to later have some other fraction copied into it. E.g.

fraction x, y(3, 5);
x = y;

If you're going to be printing out values, you might as well recognize the bleak reality of the situation that your fraction contains some uninitialized values.

Generally speaking, for when you're designing programs in the future, whenever you want something to behave completely differently based on the way it's constructed, you probably really want to break your class into two different classes of objects.

Member Avatar for iamthwee

Perhaps this may shed some light regarding the layout...

Fraction.h

/** A program to demonstrate how to turn your
* code into syntax candy by overloading operators
*
* Realeased into the public domain by iamthwee 2006 (c)
*
*/
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>

using namespace std;

class Fraction
{
public:
Fraction(); //The Constructor
~Fraction();//The Default destructor
//various methods();
void Set(int,int);
int Num();
int Denom();
//overloaded operators
Fraction operator=(Fraction); //Overloaded = operator
Fraction operator+(Fraction); //Overloaded + operator
Fraction operator*(Fraction); //Overloaded * operator
friend ostream &operator<<( ostream &output,  Fraction &f );
// To cout a fraction
private:
int numerator;
int denominator;
};
/** Default constructor,initialises the
* two variables
*/
Fraction::Fraction()
{
numerator = 0;
denominator = 0;
}
/** Fraction destructor
*/
Fraction::~Fraction()
{
//
};
/** Overloaded operators=
*/
Fraction Fraction::operator=(Fraction f) {
numerator = f.Num();
denominator = f.Denom();
return *this;
}
/** Overloaded operators+
*/
Fraction Fraction::operator+(Fraction f) {
Fraction temp;
int temp_numerator,temp_denominator;
temp_numerator= numerator * f.Denom() + f.Num()* denominator;
temp_denominator = denominator * f.Denom();
temp.Set(temp_numerator,temp_denominator);

return temp;
}
/** Overloaded operators*
*/
Fraction Fraction::operator*(Fraction f) {
Fraction temp;
int temp_numerator,temp_denominator;
temp_numerator= numerator * f.Num();
temp_denominator = denominator * f.Denom();
temp.Set(temp_numerator,temp_denominator);

return temp;
}
/** Overloaded operators<<
*/
ostream &operator<<( ostream &output, Fraction &f )
{
return output <<  f.numerator << " / " << f.denominator;
}
/** Setter method
*/
void Fraction::Set(int n, int d)
{
numerator = n;
denominator = d;
}
int Fraction::Num()
{
return numerator;
}
int Fraction::Denom()
{
return denominator;
}
#endif

main.cpp

#include <iostream>
#include "Fraction.h"
int main()
{
Fraction a,b,c;
a.Set(1,8);
b.Set(2,3);
c = a * b;
std::cout<<c;
std::cin.get();
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.