1 / 8
文档名称:

2022年东北大学C实验报告.doc

格式:doc   大小:604KB   页数:8页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

2022年东北大学C实验报告.doc

上传人:非学无以广才 2021/12/17 文件大小:604 KB

下载得到文件列表

2022年东北大学C实验报告.doc

相关文档

文档介绍

文档介绍:试验六

(1)定义Point类, 有坐标_x, _y两个组员变量; 对Point类重载“++”(自增)、 “――”(自减)运算符, 实现对坐标值改变。
(2)定义一个车(vehiele)基类, 有Run、 Stop等组员函数, 由此派生出自行车(bicycle)类、 汽车(motorcar)类, 从bicycle和motorcar派生出摩托车(motorcycle)类, 它们都有Run、 Stop等组员函数。观察虚函数作用。
2. 试验内容及试验步骤
(1) 编写程序定义Point类, 在类中定义整型私有组员变量_x_y, 定义组员函数Point& operator++(); Point operator++(int); 以实现对Point类重载“++”(自增)运算符, 定义组员函数Point& operator--(); Point operator--(int); 以实现对Point类重载“--”(自减)运算符, 实现对坐标值改变。程序名: 1ab8_1.cpp。
(2) 编写程序定义一个车(vehicle)基类, 有Run、 Stop等组员函数, 由此派生出自行车(bicycle)类、 汽车(motorcar)类, 从bicycle和motorcar派生出摩托车(motorcycle)类, 它们都有Run、 Stop等组员函数。在main()函数中定义vehicle、 bicycle、 motorcar、 motorcycle对象, 调用其Run()、 Stop()函数, 观察其实施情况。再分别用vehicle类型指针来调用这多个对象组员函数, 看看能否成功; 把Run、 Stop定义为虚函数, 再试试看。程序名: lab8_2.cpp。
3. 源程序
Lab8_1
#include<iostream>
using namespace std;
class Point{
public:
Point(int X,int Y): _x(X),_y(Y){}
Point operator++();
Point operator++(int);
Point operator--();
Point operator--(int);
void Putout() const;
private:
int _x,_y;
};
void Point::Putout() const{
cout<<"("<<_x<<","<<_y<<")"<<endl;
}
Point Point:: operator++(){
_x++;
_y++;
return *this;
}
Point Point::operator++(int){
++_x;++_y;
return *this;
}
Point Point::operator--(){
_x--;
_y--;
return *this;
}
Point Point::operator--(int){
--_x;
--_y;
return *this;
}
int main(){
Point A(6,7);
cout<<"Point+