Solved

Creat a Shapes class program to display different shape of figure  from a derived class.

Posted on 2004-07-31
3
213 Views
Last Modified: 2010-04-24
The program is asking to create a Shapes class program  that dynamically displays either a triangle, rectangle or a circle from a derived class, depending on a user's selection.  the three figures are giving as follows:
rectangle::
cout<<"*******"<<endl;
cout<<"*******"<<endl;
cout<<"*******"<<endl;
cout<<"*******"<<endl;

triangle::
cout<<"*"<<endl;
cout<<"**"<<endl;
cout<<"***"<<endl;
cout<<"****"<<endl;

circle::
                cout<<"     * * *"<<endl;
      cout<<"    * * * *"<<endl;
      cout<<"   * * * * * "<<endl;
      cout<<" * * * * * * *"<<endl;
      cout<<"  * * * * * *"<<endl;
      cout<<"   * * * * *"<<endl;
      cout<<"    * * * *"<<endl;
the problem asks to add a virtual function named drawShape() in the Shapes class, and we have to override the drawShape() function and include the figures above to the derived class, in the program's main() function, instantiate a Shapes class pointer, then create a looping statement that display a menu allowing users to pick the type of shape they want.  when the user picks a shape, instantiate a new object of that shape's class and assign it to the Shapes class pointer.  and we need to use the Shapes class pointer to call the getShapeType() and drawShape() function, and last modify the Shapes class to be an abstract base class.

i'm quiet confusing about where to put those figures above, how to instantiate the Shapes class pointers, and how to use the Shapes class pointers to call the getShapeType() and drawType() function. do we need to have setShapeType() function? and we suppose to have three derived classes and one base class. and do we need to declare any data type for the figures?

thank you very much!
ahdeenhu
0
Comment
Question by:ahdeenhu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Accepted Solution

by:
Svetlin_Panayotov earned 500 total points
ID: 11683642
I've seen something like this in a book - maybe from Herbet Shildt..whatever - first some answers:
You can put those figures in the body of drawShape().And you also don't need
setShapeType() method - in a simple exercise like this one you can use getShapeType() just to identify what shape has been drawn.And about calling drawShape() and setShapeType() - this exercise (I suposse) - is just about that - having pointer to base class and creating with it instances of derived classes.It may look confusing - but it works...and here's some code
(no user input etc.):


#include <iostream.h>
class Shapes
{
public:
      virtual void drawShape() { cout << "";}
      virtual int  getShapeType() { return -1;}
};


class Trianlge : public Shapes
{
public:
      void drawShape()
      {
            cout<<"*"<<endl;
            cout<<"**"<<endl;
            cout<<"***"<<endl;
            cout<<"****"<<endl;
      }

      int getShapeType()
      {
            return 1;
      }
};

class Rectangle : public Shapes
{
      public:
      void drawShape()
      {
            cout<<"*******"<<endl;
            cout<<"*******"<<endl;
            cout<<"*******"<<endl;
            cout<<"*******"<<endl;
      }

      int getShapeType()
      {
            return 2;
      }
};

class Circle : public Shapes
{
      void drawShape()
      {
            cout<<"     * * *"<<endl;
            cout<<"    * * * *"<<endl;
            cout<<"   * * * * * "<<endl;
            cout<<" * * * * * * *"<<endl;
            cout<<"  * * * * * *"<<endl;
            cout<<"   * * * * *"<<endl;
            cout<<"    * * * *"<<endl;
      }

      int getShapeType()
      {
            return 3;
      }
};


int main(void)
{
      Shapes *ourShape;
      ourShape = new Trianlge;
      ourShape->drawShape();
      delete ourShape;
      ourShape = new Circle;
      ourShape->drawShape();
                delete ourShape();
      return 0;
}
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question