Снова вернемся к задаче определения фигур. В C++ принято описывать интерфейсную часть классов в заголовочных файлах. Мы можем написать:
struct point {
int x; int y;
};
class Shape { public:
Shape(); void setCenter(Point p}; virtual void draw() = 0; Point center() const;
private
Point theCenter;
};
class Circle : public Shape { public:
Circle(); void setRadius(int r); virtual void draw(); int radius() const;
private:
int theRadius;
};
class Rectangle : public Shape { public:
Rectangle(); void setHeight(int h); void setWidth(int w); virtual void draw(); int height() conat; int width() const;
private:
int theHeigh; int theWidth;
};
class SolidRectangle : public Rectangle { public:
virtual void draw();
};
Определение C++ не предполагает наличия библиотеки классов. Для наших целей мы предположим наличие программного интерфейса Х Windows и глобальных объектов Display, window, GraphicsContext (требуемых xlib). Теперь можно завершить разработку, написав в отдельном файле реализацию методов, перечисленных выше:
Shape::Shape() {
theCenter.x = 0; theCenter.y = 0;
};
void Shape::getCenter(Point p) {
theCenter = p;
};
Point Shape::center() const {
return theCenter;
};
Circle::Circle() : theRadius(0) {};
void Circle: :setRadius( int r) {
theRadius = r;
};
void Circle::draw() {
int x = (center().x - theRadius); int y = (center().y - theRadius); XDrawArc(Display, Window, GraphicsContext, x, y, (theRadius ∙ 2), (theRadius * 2), 0, (360 ∙ 64));
};
int Circle::radius() const {
return theRadius;
};
Rectangle::Rectangle() : theHeight(0), theWidth(0) {};
void Rectangle::setHeight( int h) {
theHeight = h;
};
void Rectangle::setWidth( int w) {
theWidth = w;
};
void Rectangle::draw() {
int x = (center().x - (theWidth / 2)); int y = (center().y - (theHeight / 2)); XDrawRectangle(Display, Window, GraphicsContext, x, y, thewidth, theHeight);
};
int Rectangle::height() const {
return theHeight;
};
int Rectangle::width() const {
return thewidth;
};
void SolidRectangle::draw() {
Rectangle::draw(); int x - (center().x - (width() / 2)); int y - (center().y - (height() / 2)); gc oldGraphicsContext = GraphicsContext; XSetForeground(Display, GraphicsContext, Gray); XDrawFilled(Display, Window, GraphicsContext, x, y, width(), height()); GraphicsContext = OldGraphicsContext;
};
Ссылки
Основной ссылкой по C++ является "Annotated C++ Reference Manual" Эллис и Страуструпа [15]. Кроме того, Страуструп [16] предложил углубленный анализ языка и его использования в контексте объектно-ориентированного проектирования.
А.5. Common Lisp Object System (CLOS)
Происхождение
Существуют буквально десятки диалектов языка Lisp, включая MacLisp, Standard Lisp, SpiceLisp, S-1 Lisp, ZetaLisp, Nil, InterLisp и Scheme. В начале 80-х годов под воздействием идей объектно-ориентированного программирования возникла серия новых диалектов Lisp, многие из которых были ориентированы на представление знаний. Успех в стандартизации Common Lisp стимулировал попытки стандартизировать объектно-ориентированные диалекты в 1986 году.