Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Safearray problem c++ 4 80
template declaration 35 135
FTP Directory Listing 2 64
.net updatepanel trigger control in accordion pane - Can't find the control 2 140
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
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…

930 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

12 Experts available now in Live!

Get 1:1 Help Now