Quick question regarding the Fraction class created in the header file. Someone in my group used this in the header and I was wondering if there was an alternative to coding it than what they used. Something simpler for me to understand. It seems they just took a shortcut.

public:
    Fraction(int _num = 0, int _den = 1, char _sign = 'p') : numerator(_num), denominator(_den), sign(_sign) {}

Recommended Answers

All 2 Replies

Another way to write this would be:

class Fraction
{
	int numerator;
	int denominator;
	char sign;
	public:
	Fraction(int _num = 0, int _den = 1, char _sign = 'p');
};

Fraction::Fraction(int _num, int _den, char _sign)
{
	numerator = _num;
	denominator = _den;
	sign = _sign;
}
Fraction(int _num = 0, int _den = 1, char _sign = 'p');

Having the '=' operator in the first declaration of the function makes it so if you do not give that parameter a value it will use the value stated as the default.

Ah ha! Thanks so much for the clarification.

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.