-
Classes need help
I have a project I'm working on for my C++ class. It's just a simple project to show that I know how to use OO programming.
Here's my problem...
The project requires 2 constructors one that take a double argument to initialize "dRadius" and the other dose not take any arguments, it just sets "dRadius" to 0. The project also requires another function that sets "dRadius". There is no user interaction for this project. So why have a constructor and a function that do the same thing? Is it good programming to initialize a class member 2 time in a row?
-
Its conceivable that you might want to modify this variable later, and hence the other function. The constructors guarantee that it does get initialized, and allows you to do so with a specified value. The only initializing is done in the constructors. After that, it is just modifying it. Also, it allows you to construct the object and set it at the required value, instead of setting it to some default value, and then setting it to something else (which can be costly if your data member is a large, user-defined tyep... though for a primitive type such as an int, it really doesn't matter).
In short, it is perfectly acceptable programming practice.
-
Thanks for the reply that is what I wanted to know.