Question: In this modification to Shapes v . 3 , apply the principle of least privilege and rewrite all structs into classes. 1 . Constructor Functions

In this modification to Shapes v.3, apply the principle of least privilege and rewrite all structs into classes.
1. Constructor Functions
Complete and build on the exercises from the chapter in which you already wrote a constructor for Squares. Using this as a model. Write constructors for all object types. You may write constructor inline or not, per your choice.
2. Constant Data Members
Make all data members constant and private.
That means your constructor will need an initializer list. But remember that values for dimensions involve if-logic. If there's no token, set the dimension to zero. You cannot write if-else logic inside initializer list parentheses. But you can use the "conditional" ternary operator. (Don't use const_cast for this, even though it would work -- we'll use that for the "assignment operator" only.)
Copies of the shape objects will not be made anywhere in this version of the program. But it's good programming practice to write the assignment operator any time you have constant data members, so do that. Be sure to devise some test code to make sure (1) your assignment operator gets called and (2) that it works. You do not have to include your test code in the CPP submitted for this assignment -- your professor has his own tests!
Sample Square class definition:
//structs & member functions
class Square
{
const double side;
public:
Square(const vector & tokens): side(tokens.size()>1? atof(tokens[1].c_str()) : 0){}
Square& operator=(const Square&); // for swap function but not be used for this assignment. You may signore it
void output(ostream&) const;
};
3. Constant Local Variables
Programmers don't always bother to put const in front of local variables that really should be constants (because once assigned, they never change), but let's apply the principle of least privilege "to the max" in this assignment -- make anything that can be a constant as a constant.
Pointers should be constant read-only. If you use reinterpret_cast , be sure to cast to a read-only pointer data type. If you deallocate using a pointer, that pointer should still be constant read-only -- even though it destroys the object (the ultimate mutation!). That's because "read-only" pertains to the data member values only. For example,
const Square* const pSquare = reinterpret_cast(myBag[i]);
This will not be the same for every student, because it depends on how the coding in the output functions and int main is written. And yes, this applies to the vector's pointers and any pointers written in the parsing and other loops!
4. Group "Like Objects"
After the parsing loop ends and the input file gets closed, and before the output loops, write a nested-for-loop sorting code block to group like objects so that all squares will be output together, all circles together, etc. The order of the object type does not matter -- that's up to you. What matters is that all objects of the same type are output together.
5. Final Checklist
All data members should be private and constant.
All constructors should have exactly one parameter.
All atof calculations should be in the constructors, not in main.
All parameters that are shape objects should be constant references.
All parameters that are doubles or ints should be constants.
The bag should contain read-only pointers.
Any pointers in the 4 main program loops should be constant read-only pointers.
You cannot reinterpret a read-only pointer as a mutating pointer -- you must retain read-only-ness.
Any reference "variables" in the 4 main program loops should be read-only.
Any local variables in any functions that can be constants should be constants.
Include identification code blocks -- comments and cout's.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!