I have a project where pretty much all of the variables that I will be working with in spread though my base class and two inherited classes will be prompted for. Up until now, I just passed in the information through parameter constructors in the classes themselves from my application file like:

cargoShip myCargoShip1 ("USS Enterprise", "1985", 230456);

So my question is what is the best way to write this? Would I include all of the prompts and input validation in the implementation file & classes themselves or is it better to put the prompts in my application file? If so, is there a particular way I would pass the local variable in my application file back to my class implementation file?

Thanks.

Recommended Answers

All 2 Replies

>So my question is what is the best way to write this?
Without more details, I'll offer the recommendation that your class shouldn't prompt for input because that hurts its flexibility. Your initial design is better, where the calling code provides the constructor arguments. If you need to add prompts, do so there rather than within your classes.

If for some reason you need to centralize the code to prompt for and create objects and bind it to the class hierarchy, a variation of the factory method pattern would be acceptable.

I could just prompt for overtime, payperhour, ect in my application code and then just pass those variables into the functions in my inherited classes as parameters, right?

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.