Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <string>
- using namespace std;
- class shape
- {
- public:
- shape(int x=0)
- {
- ;
- }
- //function to draw shape
- virtual void draw() = 0;
- //------function to calculate area
- virtual float area() = 0;
- //function to return name of the shape
- virtual string name() = 0;
- void print_area()
- {
- cout << this->area() << endl;
- }
- };
- class rectangle : public shape
- {
- float length, width;
- public:
- //parameterized constructor
- rectangle(float length ,float width): shape(0)
- {
- this->length = length;
- this->width = width;
- }
- //------function to return name of the shape "Rectangle"
- string name() { return "Rectangle"; }
- //draw the rectangle
- void draw()
- {
- for (int i = 0; i<length; i++)
- {
- for (int j = 0; j<width; j++)
- {
- if (i == 0 || i == length - 1 || j == 0 || j == width - 1)
- cout << "*";
- else
- cout << " ";
- }
- cout << endl;
- }
- }
- //calculate area of the rectangle
- float area() { return length*width; }
- };
- class square : public rectangle
- {
- float side;
- public:
- //parameterized constructor
- square(float side):rectangle(side,side)
- {
- this->side=side;
- }
- //------function to return name of the shape "Square"
- string name() { return "Square"; }
- };
- class rhombus :public shape
- {
- float diagonal_1, diagonal_2;
- public:
- //parameterized constructor
- rhombus(float diagonal_1,float diagonal_2):shape(0)
- {
- this->diagonal_1=diagonal_1;
- this->diagonal_2 = diagonal_2;
- }
- string name() { return "Rhombus"; }
- //draw the rhombus
- void draw()
- {
- int i = 0, j = 0,d= diagonal_1-1;
- for (i = -d; i <= d; i++)
- {
- for (j = -d; j <= d; j++)
- {
- if (abs(i) + abs(j) == d)
- cout << "*";
- else
- cout << " ";
- }
- cout << endl;
- }
- }
- //calculate area of the rhombus
- float area(){ return (diagonal_1 * diagonal_2) / 2; }
- };
- class vertical_line :public shape
- {
- int v_length;
- public:
- //parameterized constructor
- vertical_line(int v_length) :shape(0)
- {
- this->v_length= v_length;
- }
- string name() { return "Vertical line"; }
- //draw the vertical line
- void draw()
- {
- for(int i=0;i<v_length;i++)
- cout << "*" << endl;
- }
- //calculate area of the vertical line
- float area() { return 0;}
- };
- class horizontal_line :public shape
- {
- int h_length;
- public:
- //parameterized constructor
- horizontal_line(int h_length) :shape(0)
- {
- this->h_length= h_length;
- }
- string name() { return "horizontal line"; }
- //draw the horizontal line
- void draw()
- {
- for (int i = 0;i<h_length;i++)
- cout << "*";
- cout << endl;
- }
- //calculate area of the horizontal line
- float area() { return 0; }
- };
- void main(void)
- {
- float length, width, side,diagonal_1, diagonal_2;
- int h_length, v_length;
- shape* shapes[30] = { NULL };
- int count = 0;
- while (1)
- {
- cout << "Please choose from the following:" << endl << endl;
- cout << "1. Make a new shape" << endl << "2. List shapes" << endl <<
- "3. Query Shape Info" << endl << "4. Draw a shape" << endl <<
- "5. Exit" << endl << endl;
- int choice;
- cin >> choice;
- cout << endl;
- if (choice == 1)
- {
- while (1)
- {
- //prompt user to choose a shape
- cout << "Please choose from the following: " << endl << endl <<
- "1. Rectangle" << endl << "2. Square"<< endl
- << "3. Rhombus" << endl << "4. Vertical Line" << endl <<
- "5. Horizontal Line" << endl << "6. Back" << endl;
- int ans;
- cin >> ans;
- if (ans == 1)
- {
- while (1)
- {
- //prompt user to enter length and width of the rectangle
- cout << "Please enter length and width of the rectangle:" << endl;
- cin >> length >> width;
- //check sides values
- if (length > 100 || length < 2 || width > 100 || width < 2)
- {
- cout << "Sides must be between 2 and 100\n\n";
- }
- else
- break;
- }
- shapes[count] = new rectangle(length,width);
- count++;
- cout << endl;
- }
- else if (ans == 2)
- {
- while (1)
- {
- //prompt user to enter side of the square
- cout << "Please enter a side of a square: ";
- cin >> side;
- cout << endl;
- //check side value
- if (side < 2 || side>100)
- {
- cout << "side must be between 2 and 100\n\n";
- }
- else
- break;
- }
- shapes[count] = new square(side);
- count++;
- cout << endl;
- cout << endl;
- }
- else if (ans == 3)
- {
- while (1)
- {
- //prompt user to enter diagonals of the rhombus
- cout << "Please enter two diagonls of the rhombus: \n";
- cin >> diagonal_1 >> diagonal_2;
- //check diagonals values
- if (diagonal_1 > 100 || diagonal_1 < 2 || diagonal_2 > 100 || diagonal_2 < 2)
- cout << "Diagonals must be between 2 and 100" << endl;
- else
- break;
- }
- shapes[count] = new rhombus(diagonal_1, diagonal_2 );
- count++;
- cout << endl;
- cout << endl;
- }
- else if (ans == 4)
- {
- while (1)
- {
- //prompt user to enter length of the vertical line
- cout << "Please enter length of the vertical line: \n";
- cin >> v_length;
- //check length value
- if (v_length > 100 || v_length < 1)
- cout << "must be between 1 and 100" << endl;
- else
- break;
- }
- shapes[count] = new vertical_line(v_length);
- cout << endl;
- count++;
- cout << endl;
- cout << endl;
- }
- else if (ans == 5)
- {
- while (1)
- {
- //prompt user to enter length of the horizontal line
- cout << "Please enter length of the horizontal line: \n";
- cin >> h_length;
- //check length value
- if (h_length > 100 || h_length < 1)
- cout << "must be between 1 and 100" << endl;
- else
- break;
- }
- shapes[count] = new horizontal_line(h_length);
- cout << endl;
- count++;
- cout << endl;
- cout << endl;
- }
- else if (ans == 6)
- {
- break;
- }
- else
- {
- cout << endl << "Invalid choice" << endl << endl;
- continue;
- }
- }
- }
- if(choice ==2)
- {
- if (count == 0)
- {
- cout << "Shapes not found" << endl <<
- "Please make new shapes" << endl << endl;
- }
- else
- {
- for (int i = 0;i < count;i++)
- {
- cout << i+1<<". "<<shapes[i]->name() << endl;
- }
- cout << endl;
- }
- }
- if (choice == 3)
- {
- if (count == 0)
- {
- cout << "Shapes not found" << endl <<
- "Please make new shapes" << endl << endl;
- }
- else
- {
- for (int i = 0;i < count;i++)
- {
- cout << shapes[i]->name() << endl <<"Area: ";
- shapes[i]->print_area();
- cout << endl;
- }
- cout << endl;
- }
- }
- if(choice==4)
- {
- if (count == 0)
- {
- cout << "Shapes not found" << endl <<
- "Please make new shapes" << endl << endl;
- }
- else
- {
- for (int i = 0;i < count;i++)
- {
- cout << shapes[i]->name() << endl << endl;
- shapes[i]->draw();
- cout << endl;
- }
- }
- }
- if (choice == 5)
- {
- for (int i = 0;i < 30;i++)
- delete(shapes[i]);
- exit(1);
- }
- }
- }
Add Comment
Please, Sign In to add comment