I am trying to write a program that will initialize a new "Square" object with private const data member "side". I have written it properly to do so, but I need to write in a check to make sure "const char* token[1]" is not a NULL pointer, and if it is, to initialize"side" to 0 instead. Here's what I have so far:
The ternary operator (condition ? returniftrue : returniffalse) is shorthand notation for an if-else statement.
But why do you need to pass an array of strings to construct a Square? And why will the side length be stored in the second and not first index?
Random Note:
I am assuming you are programming in C++ since you are using classes, so I suggest you use std::string and std::istringstream instead of working with C-strings manually.
Thanks a bunch! That worked just as I needed it to! The reason its in the second index is because the first index identifies the shape (SQUARE, RECTANGLE, etc) and subsequent tokens are sides, length, radius depending on the shape.
The reason its like that is because the sides and such are parsed from a text file. Also I could use strings, but that's not what the assignment criteria called for.
Well, I would still suggest you leave the converting from C-string to floating point outside of the constructor. It just makes your class more sensible that way. Plus, you can centralize all the conversions somewhere else.