I need the user to input two bool values in main(), which then will be inputed as arguments in a class function found in a separate file. However, I also would like the user to invoke the default constructor in the class if no bool values are inputed. How can I do that?
-
3Have you got any code to show? Where are you stuck?sehe– sehe2012-07-08 23:01:35 +00:00Commented Jul 8, 2012 at 23:01
-
2What progress have you made with this so far?reuben– reuben2012-07-08 23:01:45 +00:00Commented Jul 8, 2012 at 23:01
-
1Homework? It's Ok if it is, but we should know.DavidO– DavidO2012-07-08 23:02:53 +00:00Commented Jul 8, 2012 at 23:02
-
Set appropriate default values on the variables that will be the paramters for the constructor. In case the user does not input parameters, forward those values to the constructor,MFH– MFH2012-07-08 23:03:03 +00:00Commented Jul 8, 2012 at 23:03
-
Have user enter bool1 and bool2 in main(). Call on a class function which takes the two values as arguments. If no input, set bool1 and bool2 to some default value. EDIT: not homework. I'm just curious.Nico Bellic– Nico Bellic2012-07-08 23:05:31 +00:00Commented Jul 8, 2012 at 23:05
3 Answers
Considering the following situation:
struct Test {
bool val1, val2;
Test(bool val1, bool val2) : val1(val1), val2(val2) { }
};
The cleanest solution would look somewhat like this:
int main() {
bool val1 = true, val2 = true;//init with defaults
if(userWantsToChangeDefaults) std::cin >> val1 >> val2;//obviously this is more or less pseudocode
Test test(val1, val2);
}
Comments
Implement a constructor that takes default arguments. For example
TestClass(bool bVal1 = false, bool bVal2 = false);
where you change as required to implement your desired default behaviour, Then if you create an instances like this
TestClass test_instance; // constructor uses default args
TestClass test_instance2(true,false); // constructor uses specified args
then the constructor will treat the arguments as the default values set in the constructor declaration (if none are provided).
Alternatively have 2 constructors
TestClass();
TestClass(bool bVal1, bool bVal2);
and let the user of the class use whichever is appropriate.
3 Comments
TestClass test_instance();, lest you declare a function.Just to substantiate my hint 'use a parser', here is a very very simple (read: lacking all input validation) approach, based on Boost Spirit.
You could write this with plain IO Streams, and you'd get basically what @MFH posted. Depending on the context, the simpler approach might be more adequate.
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>
struct MyClass
{
bool _a, _b;
MyClass(bool a, bool b) :_a(a), _b(b) { }
MyClass(){}
};
BOOST_FUSION_ADAPT_STRUCT(MyClass,(bool,_a)(bool,_b));
namespace qi = boost::spirit::qi;
int main()
{
std::cin.unsetf(std::ios::skipws);
boost::spirit::istream_iterator f(std::cin), l;
MyClass data;
using namespace qi;
auto optbool = bool_ | attr(false);
if (phrase_parse(f,l,
(optbool >> optbool),
qi::blank, data))
{
std::cout << "data._a: " << std::boolalpha << data._a << "\n"
<< "data._b: " << std::boolalpha << data._b << "\n";
} else
std::cerr << "Illegal input\n";
}