?
Solved

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

Posted on 2004-07-31
3
Medium Priority
?
223 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
1 Comment
 
LVL 6

Accepted Solution

by:
Svetlin_Panayotov earned 2000 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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

807 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