Here is what I'm working off of:
"Write a program that overloads a getnumber function. If you pass it a bool argument, that version of the function returns zero or one depending on whether the argument is false or true. If you pass it an int, that version of the program returns the value multiplied by itself." Teach Yourself C++ 6th Edition Page 67 Practice #2
Here is my code for the entire program:
#include <iostream>
short inches;
bool yn;
inline bool getNumber(bool);
inline int getNumber(int);
int main()
{
std::cout << "Chapter 3";
std::cout << "\nExtra Practice 2";
std::cout << "\n\nAre you looking for the measurement of the area in a square? (1=Yes 0=No) ";
std::cin >> yn;
if (getNumber(yn) == 1)
{
std::cout << "\nWhat is the measurement of one side of the square in inches? ";
std::cin >> inches;
std::cout << "\nThe area of your square is " << getNumber(inches) << " units.";
}
else
{
std::cout << "\nSorry this program is only for calculating the area of a square.";
}
}
inline bool getNumber(bool b)
{
return (b ? 1 : 0);
}
inline int getNumber(int i)
{
return i * i;
}
Is there a different way that would work better to do the overloaded functions?
Second question:
"Write a function that accepts an int with a default function value of -1. If you call the function with no arguments, it displays a message to that effect. If you pass an argument of other then -1, the function displays the argument value." Teach Yourself C++ 6th Edition Page 67 Practice #3
The program written:
#include <iostream>
int anInt;
bool question;
void myFunc(int i = -1);
int main()
{
std::cout << "Chapter 3";
std::cout << "\nExtra Practice 3";
std::cout << "\n\nDo you wish to enter a number? (1=Yes, 0=No) ";
std::cin >> question;
if (question)
{
std::cout << "\n\nPlease enter a number: ";
std::cin >> anInt;
myFunc(anInt);
}
else
{
myFunc();
}
}
void myFunc(int i)
{
if (i == -1)
std::cout << "\nERROR...Nothing new entered...";
else
std::cout << "\nGood job, you got it right! Here's your number: " << i;
}
[\source]
If the user enters -1 it does what the question asks it to do. Is there a way to check to see if NOTHING was passed to the myFunc() in general? In this way getting rid of the possibility that the user will enter -1?
Thank you all in advance for your advice and help.
[Edited by - zychrias on August 20, 2004 2:33:13 AM]