Question: Hello, I need help with this assignment. So far, I have the constructor functions and some constant variables, I have attahced my code at the

Hello, I need help with this assignment. So far, I have the constructor functions and some constant variables, I have attahced my code at the bottom of the question. Can anyone please help me in sorting the values and making some constant variables? 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

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

//#include

using namespace std;

//Global constant for PI

const double PI = 3.14159;

class Square {

const double length;

public :

Square(const vector&tokens) : length(tokens.size() > 1 ? atof(tokens[1].c_str()) : 0) {};

Square& operator = (const Square&);

void outputSquareCalculation(ostream&)const;

};

class Rectangle {

const double length;

const double width;

public:

Rectangle(const vector &tokens) : length(tokens.size() > 1 ? atof(tokens[1].c_str()) : 0),

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 &tokens) : length(tokens.size() > 1 ? atof(tokens[1].c_str()) : 0){};

Triangle& operator = (const Triangle&);

void outputTriangleCalculation(ostream&)const;

};

class Circle {

const double radius;

public:

Circle(const vector &tokens) : radius(tokens.size() > 1 ? atof(tokens[1].c_str()) : 0) {};

Circle& operator = (const Circle&);

void outputCircleCalculation(ostream&)const;

};

class Cube {

const double length;

const double height;

public:

Cube(const vector &tokens) : length(tokens.size() > 1 ? atof(tokens[1].c_str()) : 0),

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 &tokens) : length(tokens.size() > 1 ? atof(tokens[1].c_str()) : 0),

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 &tokens) : radius(tokens.size() > 1 ? atof(tokens[1].c_str()) : 0),

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 &tokens) : length(tokens.size() > 1 ? atof(tokens[1].c_str()) : 0),

width(tokens.size() > 1 ? atof(tokens[1].c_str()) : 0),

height(tokens.size() > 2 ? atof(tokens[2].c_str()) : 0) {};

Box(const vector&);

Box& operator = (const Box&);

void outputBoxCalculation(ostream&)const;

};

//Function Prototyes

vector parseString(string);

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 myBag; //Create an empty bag

vector myBagType; //It's companion Array

while (!fin.eof()) {

// Ignore the spaces

getline(fin, line);

int shape;

vector tokens = parseString(line);

if (tokens.size() == 0)

continue;

if (tokens.at(0) == "SQUARE") {

shape = 1;

Square* s = new Square(tokens);

myBag.push_back(s);

myBagType.push_back('S');

}

else if (tokens.at(0) == "RECTANGLE") {

Rectangle* r = new Rectangle(tokens);

shape = 2;

myBag.push_back(r);

myBagType.push_back('R');

}

else if (tokens.at(0) == "TRIANGLE") {

Triangle* t = new Triangle(tokens);

shape = 3;

myBag.push_back(t);

myBagType.push_back('T');

}

else if (tokens.at(0) == "CUBE") {

tokens.resize(4, "0");

Cube* c = new Cube(tokens);

shape = 4;

myBag.push_back(c);

myBagType.push_back('C');

}

else if (tokens.at(0) == "CIRCLE") {

tokens.resize(3, "0");

Circle* e = new Circle(tokens);

shape = 5;

myBag.push_back(e);

myBagType.push_back('E');

}

else if (tokens.at(0) == "CYLINDER") {

Cylinder* y = new Cylinder(tokens);

shape = 6;

myBag.push_back(y);

myBagType.push_back('Y');

}

else if (tokens.at(0) == "BOX") {

Box* b = new Box(tokens);

shape = 7;

myBag.push_back(b);

myBagType.push_back('B');

}

else if (tokens.at(0) == "PRISM") {

tokens.resize(4, "0");

shape = 8;

Prism* pr = new Prism(tokens);

myBag.push_back(pr);

myBagType.push_back('P');

}

else if (tokens.at(0) == "EOF") {

shape = 9;

continue;

}

else {

cout

}

for (int i = 0; i

{

for (int j = i + 1; j

if (tokens[j].name

swap(tokens[i], tokens[j]);

}

}

fin.close();

for (unsigned int i = 0; i

{

if (myBagType[i] == 'S') {

Square* pSquare = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

Square& rSquare = *pSquare;

//const_cast &>(myBag).push_back(s);

pSquare->outputSquareCalculation(cout);

}

else if (myBagType[i] == 'R') {

Rectangle* pRectangle = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

Rectangle& rRectangle = *pRectangle;

pRectangle->outputRectangleCalculation(cout);

}

else if (myBagType[i] == 'T') {

Triangle* pTriangle = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

Triangle& rTriangle = *pTriangle;

pTriangle->outputTriangleCalculation(cout);

}

else if (myBagType[i] == 'C') {

Cube* pCube = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

Cube& rCube = *pCube;

pCube->outputCubeCalculation(cout);

}

else if (myBagType[i] == 'E') {

Circle* pCircle = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

Circle& rCircle = *pCircle;

pCircle->outputCircleCalculation(cout);

}

else if (myBagType[i] == 'Y') {

Cylinder* pCylinder = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

Cylinder& rCylinder = *pCylinder;

pCylinder->outputCylinderCalculation(cout);

}

else if (myBagType[i] == 'B') {

Box* pBox = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

Box& rBox = *pBox;

pBox->outputBoxCalculation(cout);

}

else if (myBagType[i] == 'P') {

Prism* pPrism = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

Prism& rPrism = *pPrism;

pPrism->outputPrismCalculation(cout);

}

}

for (unsigned int i = 0; i

{

if (myBagType[i] == 'S') {

Square* pSquare = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

Square& rSquare = *pSquare;

pSquare->outputSquareCalculation(fout);

}

else if (myBagType[i] == 'R') {

Rectangle* pRectangle = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

Rectangle& rRectangle = *pRectangle;

pRectangle->outputRectangleCalculation(fout);

}

else if (myBagType[i] == 'T') {

Triangle* pTriangle = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

Triangle& rTriangle = *pTriangle;

pTriangle->outputTriangleCalculation(fout);

}

else if (myBagType[i] == 'C') {

Cube* pCube = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

Cube& rCube = *pCube;

pCube->outputCubeCalculation(fout);

}

else if (myBagType[i] == 'E') {

Circle* pCircle = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

Circle& rCircle = *pCircle;

pCircle->outputCircleCalculation(fout);

}

else if (myBagType[i] == 'Y') {

Cylinder* pCylinder = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

Cylinder& rCylinder = *pCylinder;

pCylinder->outputCylinderCalculation(fout);

}

else if (myBagType[i] == 'B') {

Box* pBox = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

Box& rBox = *pBox;

pBox->outputBoxCalculation(fout);

}

else if (myBagType[i] == 'P') {

Prism* pPrism = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

Prism& rPrism = *pPrism;

pPrism->outputPrismCalculation(fout);

}

}

fout.close();

for (unsigned int i = 0; i

{

if (myBagType[i] == 'S') {

Square* s = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

delete s; // deallocate the Movie object at memory location myBag[i]

}

else if (myBagType[i] == 'R') {

Rectangle* r = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

delete r;

}

else if (myBagType[i] == 'T') {

Triangle* t = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

delete t;

}

else if (myBagType[i] == 'C') {

Cube* c = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

delete c;

}

else if (myBagType[i] == 'I') {

Circle* e = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

delete e;

}

else if (myBagType[i] == 'Y') {

Cylinder* y = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

delete y;

}

else if (myBagType[i] == 'B') {

Box* b = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

delete b;

}

else if (myBagType[i] == 'P') {

Prism* p = reinterpret_cast(myBag[i]); // restore its "Movie-ness"

delete p;

}

}

system("PAUSE");

return 0;

}

vector parseString(string str)

{

stringstream s(str);

istream_iterator begin(s), end;

return vector(begin, end);

}

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!= &copyThis)

{

const_cast(host.length) = copyThis.length;

}

return host;

}

Rectangle& Rectangle::operator = (const Rectangle& copyThis)

{

Rectangle& host = *this;

if (this != &copyThis)

{

const_cast(host.length) = copyThis.length;

const_cast(host.width) = copyThis.width;

}

return host;

}

Triangle& Triangle::operator = (const Triangle& copyThis)

{

Triangle& host = *this;

if (this != &copyThis)

{

const_cast(host.length) = copyThis.length;

}

return host;

}

Circle& Circle::operator = (const Circle& copyThis)

{

Circle& host = *this;

if (this != &copyThis)

{

const_cast(host.radius) = copyThis.radius;

}

return host;

}

Cube& Cube::operator = (const Cube& copyThis)

{

Cube& host = *this;

if (this != &copyThis)

{

const_cast(host.length) = copyThis.length;

}

return host;

}

Prism& Prism::operator = (const Prism& copyThis)

{

Prism& host = *this;

if (this != &copyThis)

{

const_cast(host.length) = copyThis.length;

const_cast(host.height) = copyThis.height;

}

return host;

}

Cylinder& Cylinder::operator = (const Cylinder& copyThis)

{

Cylinder& host = *this;

if (this != &copyThis)

{

const_cast(host.radius) = copyThis.radius;

const_cast(host.height) = copyThis.height;

}

return host;

}

Box& Box::operator = (const Box& copyThis)

{

Box& host = *this;

if (this != &copyThis)

{

const_cast(host.length) = copyThis.length;

const_cast(host.width) = copyThis.width;

const_cast(host.height) = copyThis.height;

}

return host;

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The input file is something like this:

SQUARE 14 SQUARE RECTANGLE 14.5 4.65 CIRCLE 14.5 BOX x 2 9 CUBE 13 BOX 1 2 3 CYLINDER 2.3 4 56 CANDY SPHERE 2.4 CYLINDER 1.23 CYLINDER 50 1.23 TRIANGLE 1.2 3.2 PRISM 2.2 5 EOF

----------------------------------------------------------------------

The sample output file is this image down below:

Hello, I need help with this assignment. So far, I have the

class Square const double side; public Square (const vector&); Square& operator-(const Square&) void output (ostream&) const

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!