面向对象程序设计C++实验报告书
- 格式:doc
- 大小:64.00 KB
- 文档页数:8
面向对象程序设计C++实验报告书
班级: 姓名: 学号:
课程名称 面向对象程序设计C++ 实验项目 重载运算符
C++的I/O操作 实验项目类型
验证 演示 综合 设计
指导教师 成 绩
一、实验目的
1.理解重载运算符的意义,掌握用成员函数、友元函数重载运算符的特点及重载运算符函数的调运方法;
2.理解多态的意义,掌握虚函数对对多态性的支持及调用方法,掌握抽象类的含义及其实现方式;
3.学会将文件作为流来处理的方式,掌握在C++环境下建立磁盘文件和读写磁盘文件的基本方法,掌握cin和cout标准输入输出流的作用,掌握文件流的定义格式,学习标准输入输出及格式控制,熟悉流类库中常用的类及其成员函数的用法
二、实验步骤
1.设计一个点类Point,实现点对象之间的各种运算,如两点是否相同,两点的位置加减等运算等,可以重载定义如下的一些操作符,其原型分别如下:
void offset(int,int); //提供对点的偏移
void offset(Point&); //重载,偏移量用Point类对象表示
bool operator==(Point&); //运算符重载,判断两个对象是否相同
bool operator!=(Point&); //运算符重载,判断两个运算符是否不相同
void operator+=(Point&); //运算符重载将两个对象相加
void operator-=(Point&); //运算符重载,将两个对象相减
Point operator+(Point&); //运算符重载,相加并将结果放在左操作数中
Point operator-(Point&); //运算符重载,相减并将结果放在左操作数中
2.现有一个容器类,其中定义了表示半径(radius)的成员,并定义了两个纯虚函数surface_area()和volume(),分别求出其表面积和体积;以此为基类,分别定义出正方体、球体和圆柱体,并在各自的派生类中分别完成其表面积和体积的计算,编写程序并验证之。
其中基类的原型如下:
class container{ √ 2 / 8 protected:
double radius;
public:
container(double radius)
{
container::radius=radius;
}
virtual double surface_area()=0;
virtual double volume()=0;
};
void main()
{ container *p;
cube obj1(10);
sphere obj2(6);
cylinder obj3(4,5);
p=&obj1;
cout<<”输出结果:”<
cout<<”正方体表面积;” cout<<”正方体体积:” p=&obj2; cout<<”球体表面积;” cout<<”球体体积:” p=&obj3; cout<<”圆柱体表面积;” cout<<”圆柱体体积:” } 3 / 8 三、上机过程原始记录(源程序等) 1、 #include "iostream.h" class Point { int x,y; public: Point() {x=y=0;} Point(int i,int j) {x=i;y=j;} Point(Point&); ~Point() {} void offset(int,int); //提供对点的偏移 void offset(Point&); //重载,偏移量用Point类对象表示 bool operator==(Point&);//运算符重载,判断两个对象是否相同 bool operator!=(Point&);//运算符重载,判断两个运算符是否不相同 void operator+=(Point&);//运算符重载将两个对象相加 void operator-=(Point&);//运算符重载,将两个对象相减 Point operator+(Point&);//运算符重载,相加并将结果放在左操作数中 Point operator-(Point&);//运算符重载,相减并将结果放在左操作数中 int getx() {return x;} int gety() {return y;} void disp() {cout<<"("< } }; Point::Point(Point& p) {x=p.x;y=p.y;} void Point::offset(int i,int j) {x+=i;y+=j;} void Point::offset(Point& p) { x+=p.getx();y+=p.gety(); } bool Point::operator==(Point& p) {if(x==p.getx()&&y==p.gety()) return 1; else return 0;} bool Point::operator!=(Point& p) { if(x!=p.getx()||y!=p.gety()) return 1; else return 0; } void Point::operator+=(Point& p) {x+=p.getx();y+=p.gety();} void Point::operator-=(Point& p) {x-=p.getx();y-=p.gety();} Point Point::operator+(Point& p) {this->x+=p.x; this->y+=p.y; return *this;} 4 / 8 Point Point::operator-(Point& p) {this->x-=p.x; this->y-=p.y; return *this;} void main() {Point p1(2,3),p2(3,4),p3(p2); cout<<"1:"; p3.disp(); p3.offset(10,10); cout<<"2:"; p3.disp(); cout<<"3:"<<(p2==p3)< cout<<"4:"<<(p2!=p3)< p3+=p1; cout<<"5:"; p3.disp(); p3-=p2; cout<<"6:"; p3.disp(); p3=p1+p3; //先将p3+p1的结果放在p1中然后赋给p3 cout<<"7:"; p3.disp(); p3=p1-p2; cout<<"8:"; p3.disp(); } ======================================================================== 2、 #include #include using namespace std; const double Pi = 3.14; class container { public: container(double r):radius(r){}; virtual double getSurfaceArea() = 0; virtual double getVol() = 0; protected: double radius; }; class cube : public container { public: cube(double r): container(r){}; virtual double getSurfaceArea(); virtual double getVol(); 5 / 8 private: }; double cube::getSurfaceArea() { return radius * radius * 6; } double cube::getVol() { return radius * radius * radius; } class sphere : public container { public: sphere(double r): container(r){}; virtual double getSurfaceArea(); virtual double getVol(); private: }; double sphere::getSurfaceArea() { return 4 * Pi * radius * radius; } double sphere::getVol() { return 4.0 / 3 * Pi * radius * radius * radius; } class cylinder: public container { public: cylinder(double r, double h): container(r), height(h){}; virtual double getSurfaceArea(); virtual double getVol(); private: double height; }; double cylinder::getSurfaceArea() { return 2 * Pi * radius * radius + 2 * Pi * height; } double cylinder::getVol() { return Pi * radius * radius * height; } int main() { container *p = new cube(1); cout << setw(10) << setiosflags(ios::fixed);