Hi everyone,

I am doing a Windows BGI graphics program with C++ that randomly draws different types of shapes on the screen and calculates the area of each shape and displays the area inside the shape and then calculates and displays the total area of all the shapes at the end of the program.

I am trying to declare new objects of each type of shape. For example, I'm trying to create a pointer *r to a rectangle but it is saying that 'r' is undeclared for Rectangle and I can't figure out why.

Here is the code:

Code:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include "C:\Dev-C++\Graphics\winbgim.h"

using namespace std;

// Draw Shape class

class DrawShape
{
private:
   int colour;
   static int count;
public:
   static int getCount() { return count; }
   int getColour()       { return colour; }
   DrawShape() {};
   DrawShape(int c) // All Shapes have a colour
   {
      colour = c;
      count++;      // Increment the count
   }
   ~DrawShape()     // Decrement count
   {
      count--;
   }

   // All shapes require a Label.
   // best to implement the label method in the base class.
   // Note the call to Area.  This calls the Area function in the 
   // actual shape object that has been instantiated.

   void label (int x, int y)
   {
      char s[20];
      sprintf (s, "%d", (int)Area());
      setcolor(WHITE);
      setbkcolor(BLACK);
      settextjustify(CENTER_TEXT, CENTER_TEXT);
      outtextxy(x, y, s);
   }
 
   // virtual functions - MUST be implemented in derived classes
   virtual double Area() = 0; 
   virtual void   Draw() = 0;
};

class Circle:DrawShape {
   private:
      int xpos, ypos, radius;
   public:
      Circle(int);

      virtual double Area() {
         return 3.14*radius*radius;
      }

      virtual void Draw() {

      }
};

Circle::Circle(int rad) {
   radius = rad;
}

class Ellipse:DrawShape {
   private:
      int xpos, ypos, length, width;
   public:
      Ellipse(int, int, int, int);

      virtual double Area() {
         return 3.14*length*width;
      }

      virtual void Draw() {

      }
};

Ellipse::Ellipse(int x, int y, int len, int wid) {
   length = len;
   width = wid;
   xpos = x;
   ypos = y;
}

class Rectangle:DrawShape {
   private:
      int xpos, ypos, length, width;
   public:
      Rectangle(int, int, int, int);

      virtual double Area() {
         return length*width;
      }

      virtual void Draw() {
         rectangle(length, width, xpos, ypos);
      }
};

Rectangle::Rectangle(int x, int y, int len, int wid) {
   length = len;
   width = wid;
   xpos = x;
   ypos = y;
}

class Square:DrawShape {
   private:
      int xpos, ypos, length;
   public:
      Square(int);

      virtual double Area() {
         return length*length;
      }

      virtual void Draw() {

      }
};

Square::Square(int len) {
   length = len;
}

int DrawShape::count = 0;

int main() {
   initwindow(1024, 768);
   settextstyle(GOTHIC_FONT, HORIZ_DIR, 2);
   setcolor(WHITE);
   outtextxy(0, 0, "CO856 Lab 4B: [S]quare [C]ircle [R]ectangle [E]llipse [T]otal Area");
   srand(time(NULL));
   DrawShape *shape[100];
   char choice;
   int RandXPos, RandYPos, RandRadius, RandLength, RandWidth;
   int NumShapes = 0;
   double totarea;
   getch(choice);
   while (choice != 'q') {
      RandXPos = rand() % 1000;
      RandYPos = rand() % 700;
      RandLength = rand() % 400;
      RandWidth = rand() % 400;
      RandRadius = rand() % 500;
      if (choice == 'r' || choice == 'R') {
         Rectangle *r = new Rectangle(RandXPos, RandYPos, RandLength, RandWidth);
         shape[NumShapes] = r;
         NumShapes++;
      }
   }
   return 0;
}
I am using virtual functions because of the fact that there are different types of shapes (Circle, Rectangle, Square and Ellipse up to a maximum of 100 shapes total) that use the same two functions Draw() and Area(). It is saying 'r' is not declared for the pointer to type Rectangle and I don't understand why it's giving me that compile error.

If anyone has any help or suggestions, it would be greatly appreciated. Thanks.