// virtshap.cpp // Виртуальные функции и геометрические фигуры #include using namespace std; #include "msoftcon.h" //для графических функций /////////////////////////////////////////////////////////// class shape //базовый класс { protected: int xCo, yCo; //координаты центра color fillcolor; //цвет fstyle fillstyle; //заполнение public: //конструктор без аргументов shape() : xCo(0), yCo(0), fillcolor(cWHITE), fillstyle(SOLID_FILL) { } //конструктор с четырьмя аргументами shape(int x, int y, color fc, fstyle fs) : xCo(x), yCo(y), fillcolor(fc), fillstyle(fs) { } virtual void draw() = 0 //чистая виртуальная функция { set_color(fillcolor); set_fill_style(fillstyle); } }; /////////////////////////////////////////////////////////// class ball : public shape { private: int radius; //центр с координатами(xCo, yCo) public: ball() : shape() //конструктор без аргументов { } //конструктор с пятью аргументами ball(int x, int y, int r, color fc, fstyle fs) : shape(x, y, fc, fs), radius(r) { } void draw() //нарисовать шарик { shape::draw(); draw_circle(xCo, yCo, radius); } }; /////////////////////////////////////////////////////////// class rect : public shape { private: int width, height; //(xCo, yCo) – верхний левый угол public: rect() : shape(), height(0), width(0) //конструктор //без аргументов { } //конструктор с шестью аргументами rect(int x, int y, int h, int w, color fc, fstyle fs) : shape(x, y, fc, fs), height(h), width(w) { } void draw() //нарисовать прямоугольник { shape::draw(); draw_rectangle(xCo, yCo, xCo+width, yCo+height); set_color(cWHITE); //нарисовать диагональ draw_line(xCo, yCo, xCo+width, yCo+height); } }; /////////////////////////////////////////////////////////// class tria : public shape { private: int height; //(xCo, yCo) – вершина пирамиды public: tria() : shape(), height(0) //конструктор без аргументов { } //конструктор с пятью аргументами tria(int x, int y, int h, color fc, fstyle fs) : shape(x, y, fc, fs), height(h) { } void draw() //нарисовать треугольник { shape::draw(); draw_pyramid(xCo, yCo, height); } }; /////////////////////////////////////////////////////////// int main() { int j; init_graphics(); //инициализация графики shape* pShapes[3]; //массив указателей на фигуры //определить три фигуры pShapes[0] = new ball(40, 12, 5, cBLUE, X_FILL); pShapes[1] = new rect(12, 7, 10, 15, cRED, SOLID_FILL); pShapes[2] = new tria(60, 7, 11, cGREEN, MEDIUM_FILL); for(j=0; j<3; j++) //нарисовать все фигуры pShapes[j]->draw(); for(j=0; j<3; j++) //удалить все фигуры delete pShapes[j]; set_cursor_pos(1, 25); return 0; }