how to add code in my code.. plz help me #include <iostream> using std::cout; us
ID: 3545521 • Letter: H
Question
how to add code in my code..
plz help me
#include <iostream>
using std::cout; using std::endl; #include <fstream> using std::ifstream; #include <iomanip> using std::setprecision; using std::ios; #include <cstdlib> #include <cstring> #include <cmath> const int MAX_CHARS_PER_LINE = 200; const int MAX_TOKENS_PER_LINE = 4; const char* const DELIMITER = " "; class Square { public: Square(const char* const []); void print() const; private: const double side; }; class Rectangle { public: Rectangle(const char* const []); void print() const; private: const double length; const double width; }; class Circle { public: Circle(const char* const []); void print() const; private: const double radius; }; class Cube { public: Cube(const char* const []); void print() const; private: const double side; }; class Prism { public: Prism(const char* const []); void print() const; private: const double length; const double width; const double height; }; class Cylinder { public: Cylinder(const char* const []); void print() const; private: const double radius; const double height; }; Square::Square(const char* const token[]) : side((token[1] == 0 ? 0.0 : atof(token[1]))){} Rectangle::Rectangle(const char* const token[]) : length((token[1] == 0? 0.0 : atof(token[1]))), width((token[2] == 0? 0.0 : atof(token[2]))){} Circle::Circle(const char* const token[]) : radius((token[1] == 0? 0.0 : atof(token[1]))){} Cube::Cube(const char* const token[]) : side((token[1] == 0? 0.0 : atof(token[1]))){} Prism::Prism(const char* const token[]) : length((token[1] == 0? 0.0 : atof(token[1]))), width((token[2] == 0? 0.0 : atof(token[2]))), height(((token[3] == 0? 0.0 : atof(token[3])))){} Cylinder::Cylinder(const char* const token[]) : radius((token[1] == 0? 0.0 : atof(token[1]))), height((token[2] == 0? 0.0 : atof(token[2]))){} void Square::print() const { double area, perimeter; area=pow(this->side, 2); perimeter=4*this->side; cout.unsetf(ios::fixed|ios::showpoint); cout << setprecision(6); cout<<"SQUARE side="<<this->side; cout.setf(ios::fixed|ios::showpoint); cout<<setprecision(2)<<" area="<<area<<" perimeter="<<perimeter<<endl; } void Rectangle::print() const { double area, perimeter; area=(this->length)*(this->width); perimeter=2*(this->length)+2*(this->width); cout.unsetf(ios::fixed|ios::showpoint); cout << setprecision(6); cout<<"RECTANGLE length="<<(this->length)<<" width="<<(this->width); cout.setf(ios::fixed|ios::showpoint); cout<<setprecision(2)<<" area="<<area<<" perimeter="<<perimeter<<endl; } void Circle::print() const { double area, circumference; area=4*atan(1.0)*pow((this->radius), 2); circumference=2*4*atan(1.0)*(this->radius); cout.unsetf(ios::fixed|ios::showpoint); cout << setprecision(6); cout<<"CIRCLE radius="<<(this->radius); cout.setf(ios::fixed|ios::showpoint); cout<<setprecision(2)<<" area="<<area<<" circumference="<<circumference<<endl; } void Cube::print() const { double surface,volume; volume=pow((this->side), 3); surface=6*pow((this->side), 2); cout.unsetf(ios::fixed|ios::showpoint); cout << setprecision(6); cout<<"CUBE side="<<(this->side); cout.setf(ios::fixed|ios::showpoint); cout<<setprecision(2)<<" surface area="<<surface<<" volume="<<volume<<endl; } void Prism::print() const { double surface,volume; volume=(this->length)*(this->width)*(this->height); surface=2*(this->length)*(this->width)+2*(this->width)*(this->height)+2*(this->height)*(this->length); cout.unsetf(ios::fixed|ios::showpoint); cout << setprecision(6); cout<<"PRISM length="<<(this->length)<<" width="<<(this->width)<<" height="<<(this->height); cout.setf(ios::fixed|ios::showpoint); cout<<setprecision(2)<<" surface area="<<surface<<" volume="<<volume<<endl; } void Cylinder::print() const { double surface,volume; volume=4*atan(1.0)*pow((this->radius), 2)*(this->height); surface=2*4*atan(1.0)*pow((this->radius), 2)+2*4*atan(1.0)*(this->radius)*(this->height); cout.unsetf(ios::fixed|ios::showpoint); cout << setprecision(6); cout<<"CYLINDER radius="<<(this->radius)<<" height="<<(this->height); cout.setf(ios::fixed|ios::showpoint); cout<<setprecision(2)<<" surface area="<<surface<<" volume="<<volume<<endl; } int main() { const void* shapes[100]={}; int shapeId[100]={}; int i; int d=0; ifstream fin; fin.open("geo.txt"); if (!fin.good()) return 1; while (!fin.eof()) { char buf[MAX_CHARS_PER_LINE]; fin.getline(buf, MAX_CHARS_PER_LINE); int n=0; const char* token[MAX_TOKENS_PER_LINE] = {0}; token[0] = strtok(buf, DELIMITER); if (token[0]) { for (n = 1; n < MAX_TOKENS_PER_LINE; n++) { token[n] = strtok(0, DELIMITER); if (!token[n]) break; } } for (i = 0; i < n; i++) { if (!((strcmp(token[0],"SQUARE") == 0) || (strcmp(token[0],"RECTANGLE") == 0) || (strcmp(token[0],"CUBE") == 0) || (strcmp(token[0],"CIRCLE") == 0) || (strcmp(token[0],"PRISM") == 0) || (strcmp(token[0],"CYLINDER") == 0))) { cout << token[0] << " invalid object" << endl; break; } else if(strcmp(token[0], "SQUARE")==0) { const Square* const s = new Square(token); shapes[d] = s; shapeId[d] = 1; d++; break; } else if(strcmp(token[0], "RECTANGLE") == 0) { const Rectangle* const r = new Rectangle(token); shapes[d] = r; shapeId[d] = 2; d++; break; } else if(strcmp(token[0], "CIRCLE") == 0) { const Circle* const c = new Circle(token); shapes[d] = c; shapeId[d] = 3; d++; break; } else if(strcmp(token[0], "CUBE") == 0) { const Cube* const c = new Cube(token); shapes[d] = c; shapeId[d] = 4; d++; break; } else if(strcmp(token[0], "PRISM") == 0) { const Prism* const p = new Prism(token); shapes[d] = p; shapeId[d] = 5; d++; break; } else if(strcmp(token[0], "CYLINDER") == 0) { const Cylinder* const c = new Cylinder(token); shapes[d] = c; shapeId[d] = 6; d++; break; } } } fin.close(); for (int j = 0; j < d; j++) { if (shapeId[j] == 1) { ((Square*)shapes[j])->print(); } else if (shapeId[j] == 2) { ((Rectangle*)shapes[j])->print(); } else if (shapeId[j] == 3) { ((Circle*)shapes[j])->print(); } else if (shapeId[j] == 4) { ((Cube*)shapes[j])->print(); } else if (shapeId[j] == 5) { ((Prism*)shapes[j])->print(); } else if (shapeId[j] == 6) { ((Cylinder*)shapes[j])->print(); } } for (int p = 0; p < d; p++) { if (shapeId[p] == 1) { delete (Square*)shapes[p]; break; } else if (shapeId[p] == 2) { delete (Rectangle*)shapes[p]; break; } else if (shapeId[p] == 3) { delete (Circle*)shapes[p]; break; } else if (shapeId[p] == 4) { delete (Cube*)shapes[p]; break; } else if (shapeId[p] == 5) { delete (Prism*)shapes[p]; break; } else if (shapeId[p] == 6) { delete (Cylinder*)shapes[p]; break; } } }
1. OPTIONAL: Use the variable-sized array vector class to replace the fixed-size void* array.
Use the "linked list" implementation, starting with a zero-length vector, and not the "array"
implementation that starts with a pre-sized vector. You may use eitherconst void*'s
or const Shape*'s as the data type in the vector. For example, vector<const void*> shapes;
or vector<const Shape*> shapes;. Replace the int array with a vector, too!
In your vectors, do not store constants! No "const void* const" or "const Shape* const" or
"const int". Some compiler implementations don't allow assignment of values to constants in vectors
NOTE: This applies to the vector declarations only. You will still need to use constant, read-only
pointers in the loops in main, as in the previous version of GeometryHomework. It's okay to copy constant pointers into a non-constant vector.
If you want to use const Shape*'s, define the base class like this: class Shape{};.
The class needs no members -- it just exists so that we can have a vector of read-only const Shape*'s.
The 2-dimensional shapes (square, rectangle, and circle) would derive from Shape.
If you do not choose to implement the vector option at this time, then you may continue to use
the arrays as in the lab 4 version.
Iterators: With vectors, you can use either operator[] and the size() function,
or you can use "iterators". (Note: do NOT use vector::at(int), because it is not well-supported
among compilers, while it's equivalent, vector::operator[] is.) If you use iterators, declare them
something like this: vector<const Shape*>::iterator it; and vector<int>::iterator it2;.
Hint: Use a traversal loop in the print and deallocation structures that traverses both iterators in tandem.
Remember that it returns pointer to a pointer -- you have to dereference it to get a Shape* like this:*it.
To cast the pointers, you'll have to do something like this: ((const Circle*)(*it)),
which resolves to a const Circle*. To cast the shape IDints,
do something like this: *it2, which resolves to an int.
2. Modify the print functions to include an ostream& object as their first (and only) parameter.
Call the print functions with cout for that parameter. Inside the print functions, do NOT print to cout.
Instead, print to the aliased object name as it appears in the paramter list.
(I suggest using out for that name.) In this way, the print functions will be made generic,
able to print to ANY stream -- console (cout), file (fout), network (nout), or memory (sout).
3. Use stand-alone, programmer-defined output stream manipulators. (See lecture notes.)
4. Do NOT use friend relationships, and do NOT write getters or setters other than those specified.
If you have any remaining stand-alone or member functions to support printing, remove them,
and move their coding into your print functions.
5. Apply inheritance, so that the three 3-dimensional shapes (cube, prism, and cylinder)
derive from their 2-dimensional relatives (square, rectangle, and circle, respectively).
The derived from their 2-dimensional relatives (square, rectangle, and circle, respectively).
The derived classes should use their inherited dimensions, and add a dimension if needed.
Do NOT declare new versions of the 2-D dimension data members in the derived 3-D classes.
Explanation / Answer
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.