Hi,
I can run the program succefully but can't make it stop at the screen. It only pop out 1 second.
Please help !
#include<iostream.h>
#include<conio.h>
//#include<Shape.h>
//#include<Rectangle.h>
//#include<Circle.h>
//Shape Class (Shape.h)
class Shape {
public:
Shape(int newx, int newy);
int getX();
int getY();
void setX(int newx);
void setY(int newy);
void moveTo(int newx, int newy);
void rMoveTo(int deltax, int deltay);
virtual void draw();
private:
int x;
int y;
};
//Shape Implementation (Shape.cpp)
// constructor
Shape::Shape(int newx, int newy) {
moveTo(newx, newy);
}
// accessors for x & y
int Shape::getX() { return x; }
int Shape::getY() { return y; }
void Shape::setX(int newx) { x = newx; }
void Shape::setY(int newy) { y = newy; }
// move the shape position
void Shape::moveTo(int newx, int newy) {
setX(newx);
setY(newy);
}
void Shape::rMoveTo(int deltax, int deltay) {
moveTo(getX() + deltax, getY() + deltay);
}
// abstract draw method
void Shape::draw() {
}
//Rectangle Class (Rectangle.h)
class Rectangle: public Shape {
public:
Rectangle(int newx, int newy, int newwidth, int newheight);
int getWidth();
int getHeight();
void setWidth(int newwidth);
void setHeight(int newheight);
void draw();
private:
int width;
int height;
};
//Rectangle Implementation (Rectangle.cpp)
// constructor
Rectangle::Rectangle(int newx, int newy, int newwidth, int newheight): Shape(newx, newy) {
setWidth(newwidth);
setHeight(newheight);
}
// accessors for width and height
int Rectangle::getWidth() { return width; }
int Rectangle::getHeight() { return height; }
void Rectangle::setWidth(int newwidth) { width = newwidth; }
void Rectangle::setHeight(int newheight) { height = newheight; }
// draw the rectangle
void Rectangle::draw() {
cout << "Drawing a Rectangle at" << getX() << "," << getY() <<
"), width " << getWidth() << ", height " << getHeight() << endl;
}getch();
//Circle Interface (Circle.h)
class Circle: public Shape {
public:
Circle(int newx, int newy, int newradius);
int getRadius();
void setRadius(int newradius);
void draw();
private:
int radius;
};
//Circle Implementation (Circle.cpp)
// constructor
Circle::Circle(int newx, int newy, int newradius): Shape(newx, newy) {
setRadius(newradius);
}
// accessors for the radius
int Circle::getRadius() { return radius; }
void Circle::setRadius(int newradius) { radius = newradius; }
// draw the circle
void Circle::draw() {
cout << "Drawing a Circle at" << getX() << "," << getY() <<
"), radius " << getRadius() << endl;
}getch();
//Try shapes module (Polymorph.cpp)
int main(void) {
// set up array to the shapes
Shape *scribble[2];
scribble[0] = new Rectangle(10, 20, 5, 6);
scribble[1] = new Circle(15, 25, 10);
// iterate through the array and handle shapes polymorphically
for (int i = 0; i < 2; i++) {
scribble[i]->draw();
scribble[i]->rMoveTo(100, 100);
scribble[i]->draw();
}
// call a rectangle specific function
Rectangle *arec = new Rectangle(0, 0, 15, 15);
arec->setWidth(30);
arec->draw();
}
thanks
Start Free Trial