语法错误,标志符“point”,
因为point类在Figure后面,程序先后执行,所以就会不认识point,以后要注意先后顺序。vs代码:
// ConsoleApplication4.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include"iostream"
using namespace std;
class point
{
public:
point(int _x = 0, int _y = 0) :x(_x), y(_y)
{}
int get_x()
{
return x;
}
int get_y()
{
return y;
}
protected:
int x;
int y;
};
class Figure
{
public:
Figure(point _point = (0, 0)) :middle(_point)
{}
virtual void draw() = 0;
virtual void move(point _point) = 0;
virtual void rotate(int _t) = 0;
protected:
point middle;
};
class Circle :public Figure, public point
{
public:
Circle(int _x, int _y, int _r) :point(_x, _y), r(_r)
{}
void move(point _point);
void draw();
void rotate(int _t)
{}
protected:
int r;
};
void Circle::move(point _point)
{
x = _point.get_x();
y = _point.get_y();
}
void Circle::draw()
{
cout << "A circle with center (" << x << ", " << y << ") and radius " << r << '.' << endl;
}
class Square :public point, public Figure
{
public:
Square(int _x, int _y, int _d, int _t) :point(_x, _y), d(_d), t(_t)
{}
void draw();
void move(point _point);
void rotate(int _t);
private:
int d;
int t;
};
void Square::move(point _point)
{
x = _point.get_x();
y = _point.get_y();
}
void Square::draw()
{
cout << "A square with center (" << x << ", " << y << ") side length " << d << ". The angle between one side and the X-axis is " << t << '.' << endl;
}
void Square::rotate(int _t)
{
t = _t;
}
int main()
{
Circle c(1, 2, 3); //构建圆形,参数:圆心、半径
Square s(4, 5, 6, 0); //构建矩形,参数:
Figure *f = &c, &g = s; //基类指针指向子类对象
f->draw();
f->move(point(2, 2)); //将圆移动到(2,2)位置
f->draw(); //画圆形
s.draw(); //画矩形
s.rotate(1); //旋转矩形
s.draw();
return 0;
}