Solved

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

Posted on 2004-07-31
3
209 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
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now