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: public DrawShape {
private:
int xpos, ypos, radius;
public:
Circle(int, int, int);
virtual double Area() {
return 3.14*radius*radius;
}
virtual void Draw() {
circle(xpos, ypos, radius);
}
};
Circle::Circle(int x, int y, int rad) {
xpos = x;
ypos = y;
radius = rad;
}
class Ell: public DrawShape {
private:
int xpos, ypos, length, width;
public:
Ell(int, int, int, int);
virtual double Area() {
return 3.14*length*width;
}
virtual void Draw() {
ellipse(xpos, ypos, 0, 0, xpos+length, ypos+width);
}
};
Ell::Ell(int x, int y, int len, int wid) {
length = len;
width = wid;
xpos = x;
ypos = y;
}
class Rect: public DrawShape {
private:
int xpos, ypos, length, width;
public:
Rect(int, int, int, int);
virtual double Area() {
return length*width;
}
virtual void Draw() {
rectangle(xpos,ypos,xpos+length,ypos+width);
}
};
Rect::Rect(int x, int y, int len, int wid) {
length = len;
width = wid;
xpos = x;
ypos = y;
}
class Square: public DrawShape {
private:
int xpos, ypos, length;
public:
Square(int, int, int);
virtual double Area() {
return length*length;
}
virtual void Draw() {
rectangle(xpos, ypos, xpos+length, ypos+length);
}
};
Square::Square(int x, int y, int len) {
xpos = x;
ypos = y;
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;
char a[20];
int RandXPos, RandYPos, RandRadius, RandLength, RandWidth;
int NumShapes = 0;
double totarea;
while (choice != 'q' && NumShapes < 100) {
choice = getch();
if (choice != 'q') {
RandXPos = rand() %400+100;
RandYPos = rand() %400+100;
RandLength = rand() %100+100;
RandWidth = rand() %100+100;
RandRadius = rand() %50+50;
if (choice == 'r' || choice == 'R') {
Rect *r = new Rect(RandXPos, RandYPos, RandLength, RandWidth);
shape[NumShapes] = r;
shape[NumShapes]->Draw();
shape[NumShapes]->label(RandXPos+(RandLength/2), RandYPos+(RandWidth/2));
totarea += shape[NumShapes]->Area();
NumShapes++;
}
else if (choice == 'c' || choice == 'C') {
Circle *c = new Circle(RandXPos, RandYPos, RandRadius);
shape[NumShapes] = c;
shape[NumShapes]->Draw();
shape[NumShapes]->label(RandXPos+(RandRadius/2), RandYPos+(RandRadius/2));
totarea += shape[NumShapes]->Area();
NumShapes++;
}
else if (choice == 's' || choice == 'S') {
Square *s = new Square(RandXPos, RandYPos, RandLength);
shape[NumShapes] = s;
shape[NumShapes]->Draw();
shape[NumShapes]->label(RandXPos+(RandLength/2), RandYPos+(RandLength/2));
totarea += shape[NumShapes]->Area();
NumShapes++;
}
else if (choice == 'e' || choice == 'E') {
Ell *e = new Ell(RandXPos, RandYPos, RandLength, RandWidth);
shape[NumShapes] = e;
shape[NumShapes]->Draw();
shape[NumShapes]->label(RandXPos+(RandLength/2), RandYPos+(RandWidth/2));
totarea += shape[NumShapes]->Area();
NumShapes++;
}
else if (choice == 't' || choice == 'T') {
setactivepage(1);
setvisualpage(1);
sprintf(a, "%d", totarea);
setcolor(WHITE);
outtextxy(150, 20, "Total area is: ");
outtextxy(180, 60, a);
getch();
setvisualpage(0);
setactivepage(0);
}
}
}
return 0;
}
I am looking to chain the derived classes to the base class so that every shape (derived class) can get at and utilize the colour variable from the base class so that the colour can be randomized for every shape selected by the user. I know there is way to do this but I don't know what the syntax is to do it.