Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-07-31
3
Medium Priority
?
221 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 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

609 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