Hello, I have a problem which goes like this.
I need to be able to design a simple class, which i have done, and it must represent any numeric value, which includes a decimal and negative value. So i need to overload the operators, because the '%' will make an error if introduced a double.
Type in explanations as simple as possible.
Someone help!!

//****************************************************
// This Program Demonstrates a simple class structure
//****************************************************
#include <iostream>
using namespace std;

class game
{
private:
	int number_one;
	int number_two;
public:
	void set_numbers();
	int validation();
	void addition();
	void subtraction();
	void multiplication();
	void division();
	void modulus();
};
void game::set_numbers() // Give Values Prompts For User Input
{
	cout << "Please Enter the 1st number" << endl;
	number_one = validation();
	cout << "Please Enter the 2nd number" << endl;
	number_two = validation();
	cout << "\n";
};
int game::validation() // Validates Input for set_numbers
{
	char input[20];
	int number;
	bool valid = false;
	int period = 0; // Countes Periods
	int negative = 0; // Counts Negative Symbols
	do
	{
		period = 0;
		negative = 0;
		valid = false;
		cin >> input;
		for (int i = 0; i<strlen(input); i++)
		{
			if (!isdigit(input[i]) && (input[i] != '.') && (input[i] != '-'))
			{
				cout << "Error! Please Re-Enter Value That has no letters" << endl;
				valid = true;
				fflush(stdin);
				break;
			}
			if (input [i] == '.')
			{
				period++;
			}
			if (input[i] == '-')
			{
				negative++;
			}
			if (period >= 2 || negative >=2)
			{
				cout << "Do not enter a number with more than one Period (.) or Negative Symbol (-)" << endl;
				valid = true;
				fflush(stdin);
				break;
			}
		}
	} while (valid == true);
	number = atoi(input);
	return number;
};
void game::addition()
{
	set_numbers();
	cout << "Addition:       " << number_one <<  " + " << number_two << " = " << (number_one+number_two) << endl;
};
void game::subtraction()
{
	cout << "Subtraction:    " << number_one <<  " - " << number_two << " = " << (number_one-number_two) << endl;
};
void game::multiplication()
{
	cout << "Multiplication: " << number_one <<  " * " << number_two << " = " << (number_one * number_two) << endl;
};
void game::division()
{
	cout << "Division        " << number_one <<  " / " << number_two << " = " << (number_one / number_two) << endl;
};
void game::modulus()
{

	cout << "Modulus:        " << number_one <<  " % " << number_two << " = " << (number_one % number_two) << endl;
	cin.get();
};
int main() // Main
{
	game something; // Creates Object 'something' of class "game"

	something.addition();			// Access Addition
	something.subtraction();		// Access Subtraction
	something.multiplication();		// Access Multiplication
	something.division();			// Access Division
	something.modulus();			// Access Modulus
	cin.get();
	fflush(stdin);
	return 0;
}

Recommended Answers

All 2 Replies

you could probably handle your modulus operation like this:

#include<cmath>

class Number
{
     public:

     Number(){x = 0.0;}
     Number(double a){x = a;}
     Number operator= (Number rhs);
     Number operator% (Number rhs);

     private:

     double x;
};

friend ostream &operator<<(ostream &stream, Number n){stream << n.x; return stream;}


Number Number::operator=(Number rhs)
{
     x = rhs.x;
     
     return *this;
}

Number Number::operator%(Number rhs)
{     
     return Number(fmod(x, rhs));  

}

Now you should be able to do stuff like this:

int main()
{

     //Using overloaded constructor, overloaded assignment, and default constructor:
     Number num1(235.9), num2 = 6.9, temp;
    
     cout << "My Number: " << num1 << endl;

     temp = num1 % 2.57;

     cout << "Implied cast modulo: " << temp << endl;

     temp = num1 % num2;

     cout << num1 << " % " << num2 << " is " << temp;

     return 0;
}

This is uncompiled and untested, it is for conceptual purposes only; point ye' in the right direction.

Here is a good youtube on operator overloading: http://www.youtube.com/watch?v=hiog9Y-683w

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.