--> Frank Masingill wrote to Cliff Rhodes <--
FM>class Calc
FM>{
FM> private:
FM> int Num1, Num2; //private data members
FM> public:
FM> Calc(int, int); //constructor function
FM> Calc(float,float);
FM> int Add(void); //member function to compute sum
FM> int Subtract(void); //member function to compute
FM> int difference Multiply(void); //member function to
FM> compute product float Divide(void); //member function
FM>to compute quotient };
FM> Can you imagine (grin) the courage required to show
FM>one's ignorance all over the place?
Don't worry about making mistakes. As an old friend used to say "Show
me someone who hasn't made any mistakes and I'll show you someone who
hasn't made anything." :-)
FM> Anyhow, the above "works" even if it has technical
FM>flaws but you will notice that I was endeavoring, without
FM>success, to "fix" the problem that the DIVISION process
FM>needs floats rather than ints if THAT answer is to be
FM>accurate. I, apparently, didn't gain anything by
FM>separating the Calc variable into four different variables.
That is always the problem you'll have dealing with integer types.
Integer division simply truncates. There are two possible solutions,
you can give both the quotient and remainder as ints, which is
typical; or, you can report a float result as you have done. I'll redo
your class a bit to do this:
class Calc {
private:
int Num1, Num2; //private data members
public:
Calc(int, int); //constructor function
int Add(void); //return sum of Nums 1 and 2
int Subtract(void); //return difference of Nums 1 and 2
int Multiply(void); //return product of Nums 1 and 2
int Divide(int &r); //return quotient of Nums 1 and 2
float Divide(void); //return quotient as floating point
//Notice we can overload Divide() since
// the int version has an argument. This
// is a very handy feature of C++!
};
Calc::Calc(int i, int j) { Num1 = i, Num2 = j; }
int Calc::Divide(int &r)
{
// Returns the quotient of Num1 and Num2 as an int. The int
// remainder is returned through the reference r.
if(Num2 != 0) // Don't want to divide by 0!
{
r = Num1 % Num2; // Calculate the remainder using modulo op
return Num1 / Num2; // Return the integer quotient
}
// You would actually add other code here to signal a divide by
// zero error, but to keep it simple...
r = 0; // Didn't do the division if here
return 0; // Return 0 quotient if divide by zero
}
float Calc::Divide(void)
{
// Overloaded version of Divide() that returns a float value for
// the quotient of the two numbers
if(Num2 != 0)
return static_cast(Num1) / Num2;
// Notice the use of the C++ cast operator. Since we
// want a float quotient, Num1 and Num2 must be converted
// to floats BEFORE the division. A cast forces this.
// You could also use the old C cast--(float) Num1--but
// that is not recommended for C++.
// Again there should be error notification code here, but...
return 0.0; // If divide by 0, just return 0.
}
// Use these just like you had them...
int Calc::Add() { return Num1 + Num2; }
int Calc::Subtract() { return Num1 - Num2; }
int Calc::Multiply() { return Num1 * Num2; }
#include
int main(void)
{
int i = 156, j = 23; // Numbers to use
Calc Answer(i, j); // Just use 1 instance for all tests!
// Display the two numbers used...
cout << "\nNum1: " << i << " Num2: " << j;
// Do the addition, subtraction and multiply...
cout << "\nSum: " << Answer.Add();
cout << "\nDifference: " << Answer.Subtract();
cout << "\nProduct: " << Answer.Multiply();
// Do the division two ways...
int remainder; // Need this to pass as reference
cout << "\nInt Quotient: " << Answer.Divide(remainder);
cout << " Int Remainder: " << remainder;
cout << "\nFloat Quotient: " << Answer.Divide() << endl;
return 0;
}
Frank, there ought to be a few useful ideas in this code for you. I've
used a reference, I've overloaded a function, and I've used a C++
cast.
Cliff Rhodes
cliff.rhodes@juge.com
X CMPQwk 1.42 1692 X"Nature, to be commanded, must be obeyed." - Franc
--- Maximus/2 3.01
---------------
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)
|