2

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?

6
  • 3
    Have you got any code to show? Where are you stuck? Commented Jul 8, 2012 at 23:01
  • 2
    What progress have you made with this so far? Commented Jul 8, 2012 at 23:01
  • 1
    Homework? It's Ok if it is, but we should know. Commented 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, Commented 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. Commented Jul 8, 2012 at 23:05

3 Answers 3

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Whilst that works, it's 100% easier and cleaner to use default values in the calling code and supply these to the constructor in case the user doesn't input data...
@MFH You could well be right. The question is not 100% clear. Why not write up your proposed method as an answer so the OP has more options.
You should really lose the brackets on TestClass test_instance();, lest you declare a function.
0

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";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.