Question:
Why do I need constructors in c++?
?
2014-09-06 21:11:24 UTC
I'm asked to write a class that take is in the day as the first 3 letters, and outputs it as the actual day. So the user puts in mon, it outputs Monday, fairly simple.

My plan is to have 3 private members of type char, a member function of type void that takes no arguments and asks the user to input the 3 letters. A void function that uses a switch or if statement to output the actual day.

I'm also asked to use constructors. WHAT FOR?! to my understanding a constructor is a some kind of functions that takes in arguments from the main function (not necessarily) and assigns them ti the private members (in the definition). In this example, the user of the program, not the programmer, is supposed to input the letters. Note that I'm also required to use the input function.

So why would I declare a constructor, invoke it with some random values, invoke the input function that will change the member variable.

Is there something I'm missing?

Thanks!
Three answers:
SailorDumb
2014-09-06 21:16:41 UTC
for this particular program, you dont need a constructor. Your teacher probably wants you to use it to get the practice in. Later on (or now?) you'll probably eventually use these constructors in different header files. You should probably understand how to use them now, rather than figure it out later while learning something else. But in terms of this specific program, you don't NEED it. In fact, for this specific program, you could probably write the whole thing in the main function with a bunch of if statements, but then that would be too easy. if mon, output monday. if tue, output tuesday.
?
2014-09-07 17:56:41 UTC
In the example you gave, it would be overkill to use constructors. Maybe a example could help you understand how to use them.



Lets say you have a class that will compute the distance between two locations,



One constructor could take in 2 Strings, address1 and address 2. You then convert them to geo points.



Now maybe you would like to pass one string and one set of geo points? fine another constructor.



Now maybe you have two sets of geo points, for another constructor.



So you class, now can be constructed with at least 3 different ways. internally the code would work the same.



So having different constructors allows uses of your class a little bit flexibility. There are some classes out there that can be constructed with 10 or more different constructors.



Now you can make the argument, that just leave your class with one constructor, and you can and you might not lose anything. Just not as flexible.



Thanks
some guy
2014-09-07 08:16:40 UTC
You can have overloads of constructors. For example one without arguments and the other with arguments. Having constructors is a conventional way of programming so there is nothing bad about having one even if you don't need it.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...