Question: Hi, I need help with my assignment. I need help with doing part 3 Constant variables. The Question is down below. and I have attached
Hi, I need help with my assignment. I need help with doing part 3 Constant variables. The Question is down below. and I have attached my code as well. I need to make the bag constant and the pointers in my 4 loops constant. Any help will be highly appreciated!
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:
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 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.
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 (PLEASE HELP me with this one if possible)
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.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
My code:
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//Global constant for PI
const double PI = 3.14159;
class Square {
const double length;
public :
Square(const vector
Square& operator = (const Square&);
void outputSquareCalculation(ostream&)const;
};
class Rectangle {
const double length;
const double width;
public:
Rectangle(const vector
width(tokens.size() > 2 ? atof(tokens[2].c_str()) : 0) {};
Rectangle& operator = (const Rectangle&);
void outputRectangleCalculation(ostream&)const;
};
class Triangle {
const double length;
public:
Triangle(const vector
Triangle& operator = (const Triangle&);
void outputTriangleCalculation(ostream&)const;
};
class Circle {
const double radius;
public:
Circle(const vector
Circle& operator = (const Circle&);
void outputCircleCalculation(ostream&)const;
};
class Cube {
const double length;
const double height;
public:
Cube(const vector
height (tokens.size() > 2 ? atof(tokens[2].c_str()) : 0) {};
Cube& operator = (const Cube&);
void outputCubeCalculation(ostream&)const;
};
class Prism {
const double length;
const double height;
public:
Prism(const vector
height(tokens.size() > 2 ? atof(tokens[2].c_str()) : 0) {};
Prism& operator = (const Prism&);
void outputPrismCalculation(ostream&)const;
};
class Cylinder {
const double radius;
const double height;
public:
Cylinder(const vector
height(tokens.size() > 2 ? atof(tokens[2].c_str()) : 0) {};
Cylinder& operator = (const Cylinder&);
void outputCylinderCalculation(ostream&)const;
};
class Box {
const double length;
const double width;
const double height;
public:
Box(const vector
width(tokens.size() > 1 ? atof(tokens[1].c_str()) : 0),
height(tokens.size() > 2 ? atof(tokens[2].c_str()) : 0) {};
Box& operator = (const Box&);
void outputBoxCalculation(ostream&)const;
};
//Function Prototyes
vector
ostream& roundingTwo(ostream&);
ostream& roundingOff(ostream&);
int main()
{
//Declare Varaibles
ifstream fin;
ofstream fout;
string line;
//Open the input file
fin.open("Shapes.input.txt");
fout.open("Shapes.output.txt");
vector
vector
while (!fin.eof()) {
// Ignore the spaces
getline(fin, line);
vector
if (tokens.size() == 0)
continue;
if (tokens.at(0) == "SQUARE") {
const Square* s = new Square(tokens);
myBag.push_back(s);
// const_cast
myBagType.push_back(1);
}
else if (tokens.at(0) == "RECTANGLE") {
Rectangle* r = new Rectangle(tokens);
myBag.push_back(r);
myBagType.push_back(2);
}
else if (tokens.at(0) == "TRIANGLE") {
Triangle* t = new Triangle(tokens);
myBag.push_back(t);
myBagType.push_back(3);
}
else if (tokens.at(0) == "CUBE") {
tokens.resize(4, "0");
Cube* c = new Cube(tokens);
myBag.push_back(c);
myBagType.push_back(4);
}
else if (tokens.at(0) == "CIRCLE") {
tokens.resize(3, "0");
Circle* e = new Circle(tokens);
myBag.push_back(e);
myBagType.push_back(5);
}
else if (tokens.at(0) == "CYLINDER") {
Cylinder* y = new Cylinder(tokens);
myBag.push_back(y);
myBagType.push_back(6);
}
else if (tokens.at(0) == "BOX") {
Box* b = new Box(tokens);
myBag.push_back(b);
myBagType.push_back(7);
}
else if (tokens.at(0) == "PRISM") {
tokens.resize(4, "0");
Prism* pr = new Prism(tokens);
myBag.push_back(pr);
myBagType.push_back(8);
}
else if (tokens.at(0) == "EOF") {
continue;
}
else {
cout
}
for (int i = 0; i
{
for (int j = i + 1; j
{
if (myBagType[j]
{
swap(myBagType[i], myBagType[j]);
swap(myBag[i], myBag[j]);
}
}
}
}
fin.close();
for (unsigned int i = 0; i
{
if (myBagType[i] == 1) {
Square* pSquare = reinterpret_cast
Square& rSquare = *pSquare;
pSquare->outputSquareCalculation(cout);
}
else if (myBagType[i] == 2) {
Rectangle* pRectangle = reinterpret_cast
Rectangle& rRectangle = *pRectangle;
pRectangle->outputRectangleCalculation(cout);
}
else if (myBagType[i] == 3) {
Triangle* pTriangle = reinterpret_cast
Triangle& rTriangle = *pTriangle;
pTriangle->outputTriangleCalculation(cout);
}
else if (myBagType[i] == 4) {
Cube* pCube = reinterpret_cast
Cube& rCube = *pCube;
pCube->outputCubeCalculation(cout);
}
else if (myBagType[i] == 5) {
Circle* pCircle = reinterpret_cast
Circle& rCircle = *pCircle;
pCircle->outputCircleCalculation(cout);
}
else if (myBagType[i] == 6) {
Cylinder* pCylinder = reinterpret_cast
Cylinder& rCylinder = *pCylinder;
pCylinder->outputCylinderCalculation(cout);
}
else if (myBagType[i] == 7) {
Box* pBox = reinterpret_cast
Box& rBox = *pBox;
pBox->outputBoxCalculation(cout);
}
else if (myBagType[i] == 8) {
Prism* pPrism = reinterpret_cast
Prism& rPrism = *pPrism;
pPrism->outputPrismCalculation(cout);
}
}
for (unsigned int i = 0; i
{
if (myBagType[i] == 1) {
Square* pSquare = reinterpret_cast
Square& rSquare = *pSquare;
pSquare->outputSquareCalculation(fout);
}
else if (myBagType[i] == 2) {
Rectangle* pRectangle = reinterpret_cast
Rectangle& rRectangle = *pRectangle;
pRectangle->outputRectangleCalculation(fout);
}
else if (myBagType[i] == 3) {
Triangle* pTriangle = reinterpret_cast
Triangle& rTriangle = *pTriangle;
pTriangle->outputTriangleCalculation(fout);
}
else if (myBagType[i] == 4) {
Cube* pCube = reinterpret_cast
Cube& rCube = *pCube;
pCube->outputCubeCalculation(fout);
}
else if (myBagType[i] == 5) {
Circle* pCircle = reinterpret_cast
Circle& rCircle = *pCircle;
pCircle->outputCircleCalculation(fout);
}
else if (myBagType[i] == 6) {
Cylinder* pCylinder = reinterpret_cast
Cylinder& rCylinder = *pCylinder;
pCylinder->outputCylinderCalculation(fout);
}
else if (myBagType[i] == 7) {
Box* pBox = reinterpret_cast
Box& rBox = *pBox;
pBox->outputBoxCalculation(fout);
}
else if (myBagType[i] == 8) {
Prism* pPrism = reinterpret_cast
Prism& rPrism = *pPrism;
pPrism->outputPrismCalculation(fout);
}
}
fout.close();
for (unsigned int i = 0; i
{
if (myBagType[i] == 'S') {
Square* s = reinterpret_cast
delete s; // deallocate the Movie object at memory location myBag[i]
}
else if (myBagType[i] == 'R') {
Rectangle* r = reinterpret_cast
delete r;
}
else if (myBagType[i] == 'T') {
Triangle* t = reinterpret_cast
delete t;
}
else if (myBagType[i] == 'C') {
Cube* c = reinterpret_cast
delete c;
}
else if (myBagType[i] == 'I') {
Circle* e = reinterpret_cast
delete e;
}
else if (myBagType[i] == 'Y') {
Cylinder* y = reinterpret_cast
delete y;
}
else if (myBagType[i] == 'B') {
Box* b = reinterpret_cast
delete b;
}
else if (myBagType[i] == 'P') {
Prism* p = reinterpret_cast
delete p;
}
}
system("PAUSE");
return 0;
}
vector
{
stringstream s(str);
istream_iterator
return vector
}
ostream& roundingTwo(ostream& out)
{
out.setf(ios::fixed);
out.precision(2); // 2 decimal digits
return out;
}
ostream& roundingOff(ostream& out)
{
out.unsetf(ios::fixed);
out.precision(6); // the C++ default
return out;
}
void Square::outputSquareCalculation(ostream& out) const {
double area, length, perimeter;
//Calculations
area = this->length * this->length;
perimeter = 4.0 * this->length;
length = this->length;
out length
roundingTwo
}
void Rectangle::outputRectangleCalculation(ostream& out) const {
double area, lenght, width, perimeter;
//Calculations
area = this->length * this->width;
perimeter = (2.0 * this->length) + (2.0 * this->width);
out
this->length width
roundingTwo
}
void Triangle::outputTriangleCalculation(ostream& out) const {
double area, length, perimeter;
//Calculations
area = (1.732 * this->length *this->length) / 4.0;
perimeter = 3.0 * this->length;
out length
roundingTwo
}
void Cube::outputCubeCalculation(ostream& out) const {
double area, length, height, perimeter, volume;
//Calculation
area = 6 * this->length *this->length;
volume = this->length * this->length * this->length;
perimeter = (2.0 * this->length) + (2.0 * this->height);
out length
roundingTwo
}
void Circle::outputCircleCalculation(ostream& out) const {
double area, radius, perimeter;
//Calculations
area = PI * this->radius * this->radius;
perimeter = 2 * PI * this->radius;
out radius
roundingTwo
}
void Cylinder::outputCylinderCalculation(ostream& out) const {
double area, radius, height, volume;
//Calculations
area = 2.0 * PI * this->radius * (this->radius + this->height);
volume = PI * this->radius * this->radius * this->height;
out radius height
roundingTwo
}
void Box::outputBoxCalculation(ostream& out) const {
double area, lenght, width, height, volume;
//Calculations
area = 2.0 * (this->length * this->width + this->length *this->height + this->width * this->height);
volume = this->length * this->width * this->height;
out length width
height
}
void Prism::outputPrismCalculation(ostream& out) const {
double area, lenght, height, volume;
//Calculations
area = 3.0 * (this->length * this->height) + (1.732 * this->length * this->length) / 2.0;
volume = (1.732 * this->length * this->length *this->height) / 4.0;
out length height
roundingTwo
}
Square& Square::operator = (const Square& copyThis)
{
Square& host = *this;
if (this!= ©This)
{
const_cast
}
return host;
}
Rectangle& Rectangle::operator = (const Rectangle& copyThis)
{
Rectangle& host = *this;
if (this != ©This)
{
const_cast
const_cast
}
return host;
}
Triangle& Triangle::operator = (const Triangle& copyThis)
{
Triangle& host = *this;
if (this != ©This)
{
const_cast
}
return host;
}
Circle& Circle::operator = (const Circle& copyThis)
{
Circle& host = *this;
if (this != ©This)
{
const_cast
}
return host;
}
Cube& Cube::operator = (const Cube& copyThis)
{
Cube& host = *this;
if (this != ©This)
{
const_cast
}
return host;
}
Prism& Prism::operator = (const Prism& copyThis)
{
Prism& host = *this;
if (this != ©This)
{
const_cast
const_cast
}
return host;
}
Cylinder& Cylinder::operator = (const Cylinder& copyThis)
{
Cylinder& host = *this;
if (this != ©This)
{
const_cast
const_cast
}
return host;
}
Box& Box::operator = (const Box& copyThis)
{
Box& host = *this;
if (this != ©This)
{
const_cast
const_cast
const_cast
}
return host;
}
//SAMPLE OUTOUT

class Square const double side; public Square (const vector
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
