Solved

What's wrong with my program?????

Posted on 1998-11-29
34
278 Views
Last Modified: 2010-04-01
Can anyone tell me how I can fix the following program... I'm lost in establishing & calling virtual methods in order to make it work!!!  I can't seem to get my 3 derived classes to overwrite the "Shape" (base) class!!!

I have 0 error, 0 warning on compiler MS Visual C++ 5.0 but my #s 2 and 3 in the "Shapes R US" main menu are not working and # 4 does not seem to be taking in the user's input either!!!

Thank you!!!
appreciative    

Scenario
Write a "Shape" class so it will be the base class of 3 derived classes:  Rectangle, Square and Triangle.  The shape should have a length and width.
Each shape should know how to draw itself and, if the object is of the "Shape" class, the message "No Current Shape...Get one they're free!" should be displayed if you try to draw it.  A "Type" method should display the current shape in use and, if the "Type" medhod in the shape class is called the same message as above is displayed.
Data members are not to be pointers and no copy constructor or overloaded assignment operator should be included.
1 menu:  Shapes R Us
2 sub-menus: Select A Shape -- Change Dimensions
The Program should only have one object pointer of type shape(ie, Shape * ptrname) and should pointn to by default to a shape object, until the user specifies otherwide with the menu.  On "Quit", the message "Keep in Shape!" should appear.  Every menu input of "Choice" should be validated for range.

Here is my code up to date:

shape.h
class Shape
{
  public:
    //Constructors
    Shape();//default
    Shape(float aLength,float aWidth);//sets all parameters
                  
    //Destructor
    ~Shape();
            
    //Manipulation
    void Set_Length(float aLength);
    void Set_Width(float aWidth);
                        
    //Accessor Methods
    float Get_Length();
    float Get_Width();

    //Other (display to screen)
    void Display_SubMenu_Select();
    void Display_SubMenu_ChangeDim();
    virtual void Type() {cout<<endl<<"No Current Shape...Get One They're Free!"<<endl;}
    virtual void Draw() {cout<<endl<<"No Current Shape...Get One They're Free!"<<endl;}
            
  protected:
    float length, width;
};

class Rectangle:public Shape
{
  public:
    void Type() {cout<<endl<<"You have selected a RECTANGLE."<<endl;}
    void Draw();
};

class Square:public Shape
{
  public:
    void Type() {cout<<endl<<"You have selected a SQUARE."<<endl;}
    void Draw();
};

class Triangle:public Shape
{
  public:
    void Type() {cout<<endl<<"You have selected a TRIANGLE."<<endl;}
    void Draw();
};


shape.cpp
#include <iostream.h>
#include "shape.h"

Shape::Shape()
{
  length=0;
  width=0;
}

Shape::Shape(float aLength, float aWidth)
{
  length=aLength;
  width=aWidth;
}

Shape::~Shape()
{

}

void Shape::Set_Length(float aLength)
{
  if (aLength>0)
  {
    length=aLength;
  }
}

void Shape::Set_Width(float aWidth)
{
  if (aWidth>0)
  {
    width=aWidth;
  }
}

float Shape::Get_Length()
{
  return length;
}

float Shape::Get_Width()
{
  return width;
}

void Shape::Display_SubMenu_Select()
{
  int Choice;
  Shape *ptrShape;
                  
  do
  {
    cout<<endl<<endl<<"****** SELECT A SHAPE ******";
    cout<<endl<<"You want shapes, we've got shapes!";
    cout<<endl<<endl<<"1) Rectangle";
    cout<<endl<<"2) Square";
    cout<<endl<<"3) Triangle";
    cout<<endl<<"0) Cancel"<<endl;
    cout<<endl<<"Your Choice > ";
    cin>>Choice;

    switch(Choice)
    {
      case 1:
        ptrShape = new Rectangle;
        break;
      case 2:
        ptrShape = new Square;
        break;
      case 3:
        ptrShape = new Triangle;
        break;
      case 0:
        break;
      default:
        ptrShape = new Shape;
        break;
    }
  }while(Choice!=0);
}

void Shape::Display_SubMenu_ChangeDim()
{
  int Choice;
  float length, width;
  Shape *ptrShape = new Shape;
      
  do
  {
    cout<<endl<<endl<<"****** CHANGE DIMENSIONS ******";
    cout<<endl<<"All shapes. All sizes. ALL IN STOCK!!";
    cout<<endl<<endl<<"1) Change Length";
    cout<<endl<<"2) Change Width";
    cout<<endl<<"3) Double Shape Size";
    cout<<endl<<"0) Return to Main Menu"<<endl;
    cout<<endl<<"Your Choice > ";
    cin>>Choice;

    switch(Choice)
    {
      case 1:
        cout<<endl<<"Please enter length: ";
        cin>>length;
        ptrShape -> Set_Length(length);
        break;
      case 2:
        cout<<endl<<"Please enter width: ";
        cin>>width;
        ptrShape -> Set_Width(width);
        break;
      case 3:
        break;
    }
  }while(Choice!=0);
  delete ptrShape;
}

void Rectangle::Draw()
{
  int i,j;
  float shape;
  Shape *ptrShape = new Rectangle;

  for(i=1;i<=length;i++)
  {
    shape=length;
                  
    for(j=1;j<=shape;j++)
    {
      cout<<"*";
    }
    cout<<endl;
  }
  cout<<endl<<endl;
  delete ptrShape;
}

void Square::Draw()
{
  int i,j;
  float shape;
  Shape *ptrShape = new Square;

  for(i=1;i<=length;i++)
  {
    shape=width;
            
    for(j=1;j<=shape;j++)
    {
      cout<<"*";
    }
    cout<<endl;
  }
  cout<<endl<<endl;
  delete ptrShape;
}

void Triangle::Draw()
{
  int i,j;
  float shape;
  Shape *ptrShape = new Triangle;

  for(i=1;i<=length;i++)
  {
    shape=(float)i;
                        
    for(j=1;j<=shape;j++)
    {
      cout<<"*";
    }
    cout<<endl;
  }
  cout<<endl<<endl;
  delete ptrShape;
}

shapemain.cpp
#include <iostream.h>
#include "shape.h"

void main (void)
{
  int Choice;
  Shape *ptrShape = new Shape;
      
  do
  {
    cout<<endl<<endl<<"****** SHAPES R US ******";
    cout<<endl<<"You pick it, we draw it!";
    cout<<endl<<endl<<"1) Select a Shape";
    cout<<endl<<"2) What Shape Do I Have?";
    cout<<endl<<"3) Draw it!";
    cout<<endl<<"4) Change Dimensions";
    cout<<endl<<"0) Quit"<<endl;
    cout<<endl<<"Your Choice > ";
    cin>>Choice;
      
    switch(Choice)
    {
      case 1:
        ptrShape -> Display_SubMenu_Select();
        break;
      case 2:
        ptrShape -> Type();
      case 3:
        ptrShape -> Draw();
        break;
      case 4:
        ptrShape -> Display_SubMenu_ChangeDim();
      case 0:
        cout<<endl<<"Keep in Shape!"<<endl<<endl;
        break;
    }
  }while(Choice!=0);
  delete ptrShape;
}

   
0
Comment
Question by:appreciative
  • 21
  • 10
  • 2
  • +1
34 Comments
 

Author Comment

by:appreciative
ID: 1178849
Edited text of question
0
 
LVL 2

Expert Comment

by:trillo
ID: 1178850
Not very obvious but I think I got it.
First of all I see that you're declaring too much *ptrShape object but you don't use them (in the Draw function, for example)... Even in the Display_SubMenu_Select(), you don't need to declare a shape pointer.... I f you take a look there's no relation between the pointer in the Display_SubMenu_Select() function and the Mainfunction's shape pointer (where's the assignment?)
Do the following:
a) Make the Display_SubMenu_Select() to return an integer value (the users choice)
b) You should use nested "switch" statements in the main function. when the user selects option 1 ("Select a shape"), you fire the Display_SubMenu_Select()  function thet returns the users choice (the desired shape). And IT'S HERE WHERE YOU PLAY WITH THE POINTER. It should look like this (more or less)

// display the menu
    switch(Choice)
        {
          case 1:    //Select a shape
              int SelShape = Display_SubMenu_Select();
              switch(SelShape)
                   {
                     case 1:
                       ptrShape = new Rectangle;
                       break;
                     case 2:
                       ptrShape = new Square;
                       break;
                     case 3:
                       ptrShape = new Triangle;
                       break;
                     case 0:
                       break;
                     default:
                       ptrShape = new Shape;
                       break;
                   }
             case 2....
........}

On the other hand, it's not very elegant to implement  the Display_SubMenu_Select() and Display_SubMenu_ChangeDim() functions as members of the shape class, because they don't have nothing to do with the private or public members of the class.... I mean, it's not wrong but it's a little confusing.

0
 

Author Comment

by:appreciative
ID: 1178851
Hi trillo,

Thank you very much for your help, I'm trying to do what you said but I get the following errors:

Compiling...
shapemain.cpp
C:\Windows\Desktop\cplus\Shapes\shapemain.cpp(24) : error C2065: 'Display_SubMenu_Select' : undeclared identifier
C:\Windows\Desktop\cplus\Shapes\shapemain.cpp(44) : error C2360: initialization of 'SelShape' is skipped by 'case' label

I'll transfer my submenus in my main...
0
 

Author Comment

by:appreciative
ID: 1178852
trillo,
I managed to correct the errors, but when I run the program, select a shape and return to main menu to choose 2) What shape do I have, it returns "No current shape...get one they're free!"... would you know why this message is still coming up instead of "You have chosen a rectangle" or whatever the user selects???...
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178853
>>  when I run the program, select a shape and return to main menu
>> to choose 2) What shape do I have, it returns "No current shape...
>> get one they're free!"
This is because when the user selects a shape in main() using Display_SubMenu_Select() the shape pointer in main() ("ptrShape") never gets altered.  The Display_Submenu_Select() function should return a pointer to an object of the shape the user selected.  Then main should use this object as the "current object" until the user chooses another shape.   Note, when the user chooses another shape, the previous current shape should be deleted.  Like


oid main (void)
   {
     int Choice;
     Shape *ptrShape = new Shape;   // Set to NULL????

     do
     {
    //// removed for space savings
       switch(Choice)
       {
         case 1:
          delete ptrShape;
          ptrShape = Display_Submenu_Select();
           break;

0
 

Author Comment

by:appreciative
ID: 1178854
Hi nietod,

I inserted the "delete ptrShape" but now when I select 1) Select a Shape, my submenu appears, then I select 1) Rectangle, OK, but when I select 0) Cancel -- to return to main in order to select 2) What Shape do I have -- it gives me "you have performed an illegal operation, this program will shut down...."
 
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178855
It sounds like you missed the most important part.  You need to change ptrShape to point to a new shape.  The menu function should create a new shape (which it does) and should return a pointer to that shape.  then main() stores this pointer in ptrShape.  Thus ptrShape is the pointer to the "current" shape selected by the user.  When they select a new shape, the previous "current shape" is deleted and a new shape is chosen by the user and allocated and then ptrShape points to this new shape.
0
 

Author Comment

by:appreciative
ID: 1178856
I've printed, read and reread and reread your comments Nietod, but I can't seem to know how to program what you are saying ....
Am I close here or am I going nowhere ???  

void main (void)
{
    int Choice, SelShape;
    Shape *ptrShape = new Shape;
      
    do
    {
        cout<<endl<<endl<<"****** SHAPES R US ******";
        cout<<endl<<"You pick it, we draw it!";
        cout<<endl<<endl<<"1) Select a Shape";
        cout<<endl<<"2) What Shape Do I Have?";
        cout<<endl<<"3) Draw it!";
        cout<<endl<<"4) Change Dimensions";
        cout<<endl<<"0) Quit"<<endl;
        cout<<endl<<"Your Choice > ";
        cin>>Choice;
      
        switch(Choice)
        {
            case 1:
                delete ptrShape;
                SelShape=Display_SubMenu_Select();
                        
                switch(SelShape)
                {
                    case 1:
                        delete ptrShape;
                        Shape * ptrShape = new Rectangle;
                        break;
                    case 2:
                        delete ptrShape;
                        ptrShape = new Square;
                        break;
                    case 3:
                        delete ptrShape;
                        ptrShape = new Triangle;
                        break;
                    case 0:
                        cout<<endl<<"Returning to main menu."<<endl;
                        break;
                    default:
                        delete ptrShape;
                        ptrShape = new Shape;
                        break;
      }
      delete ptrShape;
      
              case 2:
          ptrShape -> Type();
      case 3:
                  ptrShape -> Draw();
                  break;
              case 4:
                  Display_SubMenu_ChangeDim();
              case 0:
                  cout<<endl<<"Keep in Shape!"<<endl<<endl;
                  break;
        }
    }while(Choice!=0);
    delete ptrShape;
}


int Display_SubMenu_Select()
{
    int Choice;
    Shape *ptrShape = new Shape;
                        
    do
    {
        cout<<endl<<endl<<"****** SELECT A SHAPE ******";
        cout<<endl<<"You want shapes, we've got shapes!";
        cout<<endl<<endl<<"1) Rectangle";
        cout<<endl<<"2) Square";
        cout<<endl<<"3) Triangle";
        cout<<endl<<"0) Cancel"<<endl;
        cout<<endl<<"Your Choice > ";
        cin>>Choice;

    }while(Choice!=0);
    return Choice = ptrShape;
}

I have the following error on "return Choice = ptrShape;"

shapemain.cpp(87) : error C2440: '=' : cannot convert from 'class Shape *' to 'int'
                                                       This conversion requires a reinterpret_cast, a                                                                 C-style cast or function-style cast

thanks

0
 
LVL 22

Expert Comment

by:nietod
ID: 1178857
You're closer in a sense.  But things are a little confussed.

Originally (the version that appears in the original question), your menu procedure created a new shape of the type selected by the user.  that is, if they selected a triangle it did "new triangle" to create a new triangle.  But that new shape was then lost.  It never returned it to the caller (or anywhere else).  In addition to being a memory leak (because the object was "new"ed and never "delete"d) this is point less because the object was never used.  My suggestion waht to make the menu function return the pointer to the object that it created.  Thus, if the user selects a Square, it creates a square ("new square") and returns a pointer to it.  Then the calling code ("main()")  can store this pointer. In essence it tracks this shape and allows the user to use (work with) this shape (at least until another shape is selected by the user).

Thus to program it that way, the menu function should return a pointer to a shape ("shape *"), not an integer ("int").  

However, you made it return an integer (the user's choice.)  That can be used in a slightly different aproach.  In that case the function returns the user's chouce (integer vallue of menu option user chose.).  Then the calling code ("main()") would interpret the integer value using a switch statement and create a new object according to the user's choice.

Your code is sort of a mix of these two approaches.  You need to choose one or the other.  Either make the menu function return the shape pointer ("ptrShape") and just have main store the pointer in its ptrShape  OR  allow the menu fiunction to return an integer as currently done and have main() interpret this using a switch statement..
0
 

Author Comment

by:appreciative
ID: 1178858
Would adding the following to my "shape class" work if I return to my original code, I don't think so!!!! ???

int Shape::Get_Shape()
{
      return ptrShape;
}

because I don't see how I can return a (shape *) .... I'm so confused....
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178859
You are confussed, but not too bad.  

option 1: make the menu function return a pointer like

Shape *Display_SubMenu_Select()
   {
     int Choice;
     Shape *ptrShape;

     do
     {
       cout<<endl<<endl<<"****** SELECT A SHAPE ******";
       cout<<endl<<"You want shapes, we've got shapes!";
       cout<<endl<<endl<<"1) Rectangle";
       cout<<endl<<"2) Square";
       cout<<endl<<"3) Triangle";
       cout<<endl<<"0) Cancel"<<endl;
       cout<<endl<<"Your Choice > ";
       cin>>Choice;

       switch(Choice)
       {
         case 1:
           ptrShape = new Rectangle;
           break;
         case 2:
           ptrShape = new Square;
           break;
         case 3:
           ptrShape = new Triangle;
           break;
         case 0:
           break;
         default:
           ptrShape = new Shape;
           break;
       }
     }while(Choice!=0);

    return prtShape;
   }

note that I made this funcion global, rather than a member of the shape class.  That would not have to be done, but there is no reason to have be part of the shape class, and it is much more convenient to use if it is not

Then to use the function you would have


void main (void)
   {
     int Choice;
     Shape *ptrShape = new Shape;

     do
     {
       cout<<endl<<endl<<"****** SHAPES R US ******";
       cout<<endl<<"You pick it, we draw it!";
       cout<<endl<<endl<<"1) Select a Shape";
       cout<<endl<<"2) What Shape Do I Have?";
       cout<<endl<<"3) Draw it!";
       cout<<endl<<"4) Change Dimensions";
       cout<<endl<<"0) Quit"<<endl;
       cout<<endl<<"Your Choice > ";
       cin>>Choice;

       switch(Choice)
       {
         case 1:
           delete ptrShape;  // Delete the shape we were using.
           ptrShape = Display_SubMenu_Select(); // Get a new shape.
           break;
         case 2:
              ptrShape -> Type();
         case 3:
           ptrShape -> Draw();
           break;
         case 4:
           ptrShape -> Display_SubMenu_ChangeDim();
         case 0:
           cout<<endl<<"Keep in Shape!"<<endl<<endl;
           break;
       }
     }while(Choice!=0);
     delete ptrShape;
   }

0
 
LVL 2

Expert Comment

by:trillo
ID: 1178860
No, it won't work... it's alittle bit confused.
1) In your orignial code the class "shape" doesn't have a pointer to a shape object, so you can't return ptrShape.
2) In this case you should declare a pointer, but it's not necesary because it would be a pointer to the object itself, and it would be redi¡undant with the "this" pointer.
3) And even in that case, the declaration should return a pointer, not an integer.
      *Shape Shape::getShape()
4) As nietod said, it would be better to place the menu as a global function instead of a class member.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1178861
Before we go on to option 2, lets improve on this.  First of all, once the user picks a shape, there is no reason to keep them in the shape menu.  so lets make it return them to the main menu whenever they pcik a valid shape.  Second, lets make it so that when they cancel, they continue to use the shape they were using before they chose to go to the select a shape menu.


// Returns -> shape chosen or returns NULL if no shape was chosen.
Shape *Display_SubMenu_Select()
{
     int Choice;

      while (true)
      {
            cout<<endl<<endl<<"****** SELECT A SHAPE ******";
            cout<<endl<<"You want shapes, we've got shapes!";
            cout<<endl<<endl<<"1) Rectangle";
            cout<<endl<<"2) Square";
            cout<<endl<<"3) Triangle";
            cout<<endl<<"0) Cancel"<<endl;
            cout<<endl<<"Your Choice > ";
            cin>>Choice;

            switch(Choice)
            {
              case 0: return NULL;  // Indicate no shape was selected.
              case 1: return new Rectangle;
              case 2: return new Square;
              case 3: return new Triangle;
            }
       }
 }

to use this you would have




void main (void)
 {
      int Choice;
      Shape *ptrShape = new Shape;

       do
       {
            cout<<endl<<endl<<"****** SHAPES R US ******";
            cout<<endl<<"You pick it, we draw it!";
            cout<<endl<<endl<<"1) Select a Shape";
            cout<<endl<<"2) What Shape Do I Have?";
            cout<<endl<<"3) Draw it!";
            cout<<endl<<"4) Change Dimensions";
            cout<<endl<<"0) Quit"<<endl;
            cout<<endl<<"Your Choice > ";
            cin>>Choice;

            switch(Choice)
            {
              case 1:
              {
                 Shape *NewShape = Display_SubMenu_Select(); // Get a new shape.
                 if (NewShape) // If user chose a new shape, then
                 {
                    delete ptrShape;  // Delete the shape we were using.
                    ptrShape = NewShape;
                }
                break;
               }
              case 2:
                   ptrShape -> Type();
              case 3:
                ptrShape -> Draw();
                break;
              case 4:
                ptrShape -> Display_SubMenu_ChangeDim();
              case 0:
                cout<<endl<<"Keep in Shape!"<<endl<<endl;
                break;
            }
          }while(Choice!=0);
          delete ptrShape;
        }

Does that seem better?   does it make sense?
0
 

Author Comment

by:appreciative
ID: 1178862
nietod, I just sent you an e-mail... in hotmail...
0
 

Author Comment

by:appreciative
ID: 1178863
I applied option 1 and I didn't get a change to see that you had posted option 2....explained in my e-mail....
0
 

Author Comment

by:appreciative
ID: 1178864
that should be "chance" not change...
0
 

Author Comment

by:appreciative
ID: 1178865
yes, your second option makes sense, I've been sending the users back to the main menu by choosing 0) cancel after having chosen a shape...but now, if 0 returns NULL I don't see how it will affect the "while(Choice!=0)" in the main function...

it validates that they have in fact chosen a new shape and I do have to validate every "choice" input so I guess I'll have to change my code...

Thanks nietod, I was forgetting the validation... from what I can see, you seem to be condensing the code (getting directly to the point) which seems to be more efficient, but I probably would not have understood it as well if I had not been through option 1...!!!

I'm going for option 2...  
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:appreciative
ID: 1178866
with option 2, I get:

Compiling...
shapemain.cpp
C:\Windows\Desktop\cplus\Shapes\shapemain.cpp(36) : error C2360: initialization of 'newShape' is skipped by 'case' label

for my cases 2,3,4,0 in the main function.

In the help:

The following is an example of this error:

void func( void )
{
   int x;
   switch ( x )
   {
   case 0 :
      int i = 1;       // error, skipped by case 1  
      { int j = 1; }   // OK, initialized in enclosing block
   case 1 :
      int k = 1;       // OK, initialization not skipped
   }
}

but I don't see the difference between "int i=1;" which is an error and "int k=1;" which is not an error... I guess I must be tired...!!!!
0
 

Author Comment

by:appreciative
ID: 1178867
>error C2360: initialization of 'newShape' is
      skipped by 'case' label
I fixed this, it works perfectly....Thank you very much...
the program now returns the user to the main menu when he selects a shape...

GREAT!!!  
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1178868
I think that there are some design problems.
Your Display_SubMenu_Select() should not be kept as a member function of the class shape. Same applies to Display_SubMenu_ChangeDim(); . Keep them as seperate stand-alone funtions and pass a pointer to the shape obeject as a parameter. Depending on your choice, make the pointer point to the appropriate type of object. The rest of the problem is simple. Just make a call to draw or type with this pointer.
Hope this helps. In case you have any problems, please let me know.
Thanks
pagladasu
       
0
 

Author Comment

by:appreciative
ID: 1178869
Hi pagladasu,

Thanks for your input... I did get my two menus out of the "shape class", that was one of the first things I did.  Yes I'll have to try and use the pointer to get it to draw and  change dimensions, I just hope I use it correctly....

Once again Thank You!!!
appreciative
0
 

Author Comment

by:appreciative
ID: 1178870
in my shape.cpp I have:

void Rectangle::Draw()
{
    int i,j;
    float shape;
    Shape *ptrShape = new Rectangle;

    for(i=1;i<=length;i++)
    {
        shape=length;
                  
        for(j=1;j<=shape;j++)
        {
            cout<<"*";
        }
        cout<<endl;
    }
    cout<<endl<<endl;
    delete ptrShape;
}

void Square::Draw()
{
    int i,j;
    float shape;
    Shape *ptrShape = new Square;

    for(i=1;i<=length;i++)
    {
        shape=width;
            
        for(j=1;j<=shape;j++)
        {
            cout<<"*";
        }
        cout<<endl;
    }
    cout<<endl<<endl;
    delete ptrShape;
}

void Triangle::Draw()
{
    int i,j;
    float shape;
    Shape *ptrShape = new Triangle;

    for(i=1;i<=length;i++)
    {
        shape=(float)i;
                        
        for(j=1;j<=shape;j++)
        {
            cout<<"*";
        }
        cout<<endl;
    }
    cout<<endl<<endl;
    delete ptrShape;
}

and in my shapemain.cpp, I call the draw this way:

ptrShape -> draw();

There's something missing here, by calling draw it doesn't know what to draw (a rect, sqr or tri.) !!!  How can I tell it to draw the "type" the user has chosen???  tried quite a few things but without success...
Thanks
appreciative
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178871
>>There's something missing here, by calling draw it doesn't know
>> what to draw (a rect, sqr or tri.)
Draw is a virtual function.  That means that given a pointer (or reference) to an object in the shape hierarchy, the correct draw will be called.  ie.  in

Shape *ptrShape;

   *    *   *

  ptrShape->Draw();

One of 4 draw functions (shape's, rectangles, triangles, square's)  will be called depending on what ptrShape actually points to (it points to "at least" a shape, but might actually point to something derived from shape, like a rectangle).  That is what is virtual about a virtual function.  The fact that the  "version" to be called is determined at run-time based on the object that it is called with.  

In your drawing procedures, get rid of the shapeptr you declare.  You don''t need it and don't even use it.
0
 

Author Comment

by:appreciative
ID: 1178872
Thanks nietod, I'll have to sort this out... I don't quite get it, it gives me a "you have performed an illegal operation"....again...!!!  But I have to run, can't be late for class...
Thanks again!!!
appreciative
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178873
>> "you have performed an illegal operation"....again.
That comes from dereferencing (using with * or ->) a NULL pointer.  (ussually).
0
 

Author Comment

by:appreciative
ID: 1178874
>> "you have performed an illegal operation"....again.

Thanks nietod... got rid of the above, but I'm trying to change my code to make it draw, I don't get any errors, but it just won't draw!!!!  even when I input a length and width in my "Change Dimensions" menu, it's not taking the inputs....????
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178875
I think I need to see the source again.  Its undercone to many changes.  I suspect that the nature of the problem is that you have multiple shape objects running around  You really should have just one, the one that main stores.  But I suspect you are creating others when you try to input dimensions or draw and thus these shapes have no relationship with the one in main()
0
 

Author Comment

by:appreciative
ID: 1178876
here is my code nietod, I bet that, by now, you wish I never found this site....
thanks

shape.h
class Shape
{
    public:
        //Constructors
        Shape();//default
        Shape(float aLength,float aWidth);//sets all parameters
                  
        //Destructor
        ~Shape();
            
        //Manipulation
        void Set_Length(float aLength);
        void Set_Width(float aWidth);
                        
        //Accessor Methods
        float Get_Length();
        float Get_Width();

        //Other (display to screen)
        virtual void Type() {cout<<endl<<"No Current Shape...Get One They're Free!"<<endl;}
       virtual void Draw() {cout<<endl<<"No Current Shape...Get One They're Free!"<<endl;}
            
    protected:
        float length, width;
};

class Rectangle:public Shape
{
    public:
        void Type() {cout<<endl<<"You have selected a RECTANGLE."<<endl;}
        void Draw();
};

class Square:public Shape
{
    public:
        void Type() {cout<<endl<<"You have selected a SQUARE."<<endl;}
        void Draw();
};

class Triangle:public Shape
{
    public:
        void Type() {cout<<endl<<"You have selected a TRIANGLE."<<endl;}
        void Draw();
};

shape.cpp
#include <iostream.h>
#include "shape.h"

Shape::Shape()
{
    length=0;
    width=0;
}

Shape::Shape(float aLength, float aWidth)
{
    length=aLength;
    width=aWidth;
}

Shape::~Shape()
{

}

void Shape::Set_Length(float aLength)
{
    if (aLength>0)
    {
        length=aLength;
    }
}

void Shape::Set_Width(float aWidth)
{
    if (aWidth>0)
    {
        width=aWidth;
    }
}

float Shape::Get_Length()
{
    return length;
}

float Shape::Get_Width()
{
    return width;
}

void Rectangle::Draw()
{
    int i,j,shape;

    for(i=1;i<=length;i++)
    {
        shape=width;
                  
        for(j=1;j<=shape;j++)
        {
            cout<<"*";
        }
        cout<<endl;
    }
    cout<<endl<<endl;
}

void Square::Draw()
{
    int i,j,shape;

    for(i=1;i<=length;i++)
    {
        shape=length;
            
        for(j=1;j<=shape;j++)
        {
            cout<<"*";
        }
        cout<<endl;
    }
    cout<<endl<<endl;
}

void Triangle::Draw()
{
    int i,j,shape;
 
    for(i=1;i<=length;i++)
    {
        shape=i;
                        
        for(j=1;j<=shape;j++)
        {
            cout<<"*";
        }
        cout<<endl;
    }
    cout<<endl<<endl;
}

shapemain.cpp
#include <iostream.h>
#include "shape.h"

Shape *Display_SubMenu_Select();
void Display_SubMenu_ChangeDim();

void main (void)
{
    int Choice;
    Shape *ptrShape = new Shape;
    Shape *newShape;
      
    do
    {
        cout<<endl<<endl<<"****** SHAPES R US ******";
        cout<<endl<<"You pick it, we draw it!";
        cout<<endl<<endl<<"1) Select a Shape";
        cout<<endl<<"2) What Shape Do I Have?";
        cout<<endl<<"3) Draw it!";
        cout<<endl<<"4) Change Dimensions";
        cout<<endl<<"0) Quit"<<endl;
        cout<<endl<<"Your Choice > ";
        cin>>Choice;
      
        switch(Choice)
        {
            case 1:
                newShape=Display_SubMenu_Select();
                        
                if(newShape)
                {
                    delete ptrShape;
                    ptrShape = newShape;
                }
                break;
            case 2:
                ptrShape -> Type();
            case 3:
                ptrShape -> Get_Length();
                ptrShape -> Get_Width();
                ptrShape -> Draw();
                break;
            case 4:
                Display_SubMenu_ChangeDim();
            case 0:
                cout<<endl<<"Keep in Shape!"<<endl<<endl;
                break;
        }
    }while(Choice!=0);
    delete ptrShape;
}

Shape *Display_SubMenu_Select()
{
    int Choice;
                        
    while(true)
    {
        cout<<endl<<endl<<"****** SELECT A SHAPE ******";
        cout<<endl<<"You want shapes, we've got shapes!";
        cout<<endl<<endl<<"1) Rectangle";
        cout<<endl<<"2) Square";
        cout<<endl<<"3) Triangle";
        cout<<endl<<"0) Cancel"<<endl;
        cout<<endl<<"Your Choice > ";
        cin>>Choice;

        switch(Choice)
        {
            case 1:
                return new Rectangle;
                break;
            case 2:
                return new Square;
                break;
            case 3:
                return new Triangle;
                break;
            case 0:
                cout<<endl<<"Returning to main menu."<<endl;
                return NULL;
                break;
        }
    }
}

void Display_SubMenu_ChangeDim()
{
    int Choice;
    float aLength, aWidth;
      
    do
    {
        cout<<endl<<endl<<"****** CHANGE DIMENSIONS ******";
        cout<<endl<<"All shapes. All sizes. ALL IN STOCK!!";
        cout<<endl<<endl<<"1) Change Length";
        cout<<endl<<"2) Change Width";
        cout<<endl<<"3) Double Shape Size";
        cout<<endl<<"0) Return to Main Menu"<<endl;
        cout<<endl<<"Your Choice > ";
        cin>>Choice;

        switch(Choice)
        {
            case 1:
                cout<<endl<<"Please enter length: ";
                cin>>aLength;
                ptrShape -> Set_Length(aLength);
                break;
            case 2:
                cout<<endl<<"Please enter width: ";
                cin>>aWidth;
                ptrShape -> Set_Width(aWidth);
                break;
            case 3:
                break;
        }
    }while(Choice!=0);
}

0
 

Author Comment

by:appreciative
ID: 1178877
I overheard the teacher say today that we did not need the "Set_Shape(..), Set_Length(..) and Set_Width(..)" nor the "Get_Shape(), Get_Length() and Get_Width()" for this program... is this so ???  Or I might not have heard right....
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178878
Is this compiling?  That doesn't seem possible.  For example, in Display_SubMenu_ChangeDim() it uses "ptrShape" but there is no ptrShape declared.  That probably should be passed in a parameter.

Can you e-mail me the code.  This page is getting too slow to load.  In additon, I would like to test it.  

The way things are currently, you do need the set_ functions.  when this is working better I will suggest a change regarding that.  (making the set options mor intelligint.  For example, It makes no sense to set the width AND height of a square.)

Also when is this due?
0
 

Author Comment

by:appreciative
ID: 1178879
Sorry Trillo, but I needed more in-depth explanations...
0
 

Author Comment

by:appreciative
ID: 1178880
Waiting for answer from Nietod...
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1178881
thanks.

(But if I know so much about turkeys, how come I bought a $3.00/lb hormone-free antibiotic-free  organic free-range turkey that was totally indistinguishable from the $0.70/lb turkey everyone else had?)
0
 

Author Comment

by:appreciative
ID: 1178882
>>(But if I know so much about turkeys, how come I bought a $3.00/lb hormone-free
>>antibiotic-free  organic free-range turkey that was totally indistinguishable from the >>$0.70/lb turkey everyone else had?)

Boy, you wanted the "real" thaaang, nothing but the best for nietod, heh ....  Wasn't it even a tiny bit tastier or juicier... didn't it have that honest to goodness mother earth  yumyum melts-in-the-mouth taste...!!!!

thanks nietod
 
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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

744 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