can someone's the assignment and what i have thusfar...I'm confused with adding the addtl constructor.

//Create a class called Fraction with the following member variables and methods:
//"	Variables
//1.	numerator and denominator (Integer)
//	Methods
//1.	Accessor methods for member variables (could be inline functions)
//2.	Mutator methods for member variables 
//"	should not allow denominator to be set to 0
//3.	A method named reduce that will reduce fraction to lowest terms
//"	ex. 9/15 = 3/5
//4.	A method named print that will print the fraction in the form: numerator / denominator Ex. 3/5
//5.	A default constructor that initializes numerator to 0 and denominator to 1
//6.	Another constructor that takes 2 arguments (to set numerator and denominator) Hint: use mutator methods
#include <iostream>
using namespace std,

class Fraction
		int numerator;
		int denominator;
		fraction() // default constructor
		numerator = 0;
		denominator =1;
		int GetNumerator() { return numerator; }
		int GetDenominator() { return denominator; }
		double GetFraction() { return static_cast<double>(numerator) / denominator; }
Fraction cDefault; // calls Fraction() constructor
	std::cout << cDefault.GetNumerator() << "/" << cDefault.GetDenominator() << std::endl;

In your code, you have an attempt at a default constructor:

fraction() // default constructor
 numerator = 0;
 denominator =1;

Unfortunately, because your capitalization isn't correct, your compiler won't recognize it as your default constructor. Remember, C++ is case-sensitive. This means that "fraction" is not the same name as "Fraction".

The default constructor takes zero (0) arguments. Just like any other function, it is possible to override the constructor method by creating a different version that has different arguments/parameters. You will have to define an overloaded constructor that takes two (2) appropriately-typed arguments.


class Sample {
   int Sample_PvtInt;
   Sample() {                //default constructor
     Sample_PvtInt = 0;

   Sample (int incomming) {  //overridden constructor
     Sample_PvtInt = incomming;
};  //end Sample class

Notice the second constructor. You need to add something similar to your Fraction class.

This article has been dead for over six months. Start a new discussion instead.