敲多态时发现的问题

语法错误,标志符“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;

}                     

2.figure和point,figure中函数能调用point构造函数,所以point和figure是组合关系。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值