实验报告类的重载继承和多态doc.docx
《实验报告类的重载继承和多态doc.docx》由会员分享,可在线阅读,更多相关《实验报告类的重载继承和多态doc.docx(6页珍藏版)》请在冰豆网上搜索。
![实验报告类的重载继承和多态doc.docx](https://file1.bdocx.com/fileroot1/2023-2/25/a0c63fcf-20e7-4d64-b182-35232b4f1c34/a0c63fcf-20e7-4d64-b182-35232b4f1c341.gif)
实验报告类的重载继承和多态doc
实验报告类的重载继承和多态
篇一:
实验三_类的重载继承和多态_()
篇二:
实验三_类的重载继承和多态
篇三:
C++实验报告(类和对象重载与继承多态和模板异常和流)
C++实验报告
目录
C++实验报告....................................................................................................................................4
实验一:
类和对象...................................................................................................................4
1......................................................................................................................................4
代码...................................................................................................................................4
运行结果...........................................................................................................................5
2.........................................................................................................................................5
思路:
.......................................................................................................................5
代码...........................................................................................................................5
运行结果...................................................................................................................8
3.........................................................................................................................................8
思路...........................................................................................................................8
代码...........................................................................................................................8
运行结果...................................................................................................................9
实验二重载和继承.................................................................................................................9
1.........................................................................................................................................9
思路.........................................................................................................................10
代码.........................................................................................................................10
运行结果.................................................................................................................13
问题分析:
.............................................................................................................13
2.......................................................................................................................................13
思路:
.....................................................................................................................13
代码.........................................................................................................................14
运行结果.................................................................................................................15
实验三多态与模板...............................................................................................................16
思路.........................................................................................................................16
代码.........................................................................................................................16
运行结果.................................................................................................................17
2.......................................................................................................................................17
思路.........................................................................................................................17
代码.........................................................................................................................18
运行结果.................................................................................................................19
3.......................................................................................................................................19
思路.........................................................................................................................19
代码.........................................................................................................................19
运行结果.................................................................................................................21
问题及分析.............................................................................................................21
实验四异常和流...................................................................................................................21
1.......................................................................................................................................21
思路.........................................................................................................................21
代码.........................................................................................................................21
运行结果.................................................................................................................22
2.......................................................................................................................................23
思路.........................................................................................................................23
代码.........................................................................................................................23
运行结果.................................................................................................................27
实验总结:
.............................................................................................................................28
C++实验报告
实验一:
类和对象
实验目的:
能够根据软件需要,对客观实体进行抽象(封装)和代码实现。
实验环境:
VisualC++6.0
实验内容:
1.设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。
代码如下:
#include
usingnamespacestd;
structpoint
{
floatx;
floaty;
};
classRectangle//矩形类
{
pointlowerleft;//矩形左下角点的坐标
pointupperright;//矩形右上角点的坐标
public:
voidget();//输入矩形左下角点和右上角点的坐标
floatarea();//计算矩形面积
};
voidRectangle:
:
get()
{
cout cout实验报告类的重载继承和多态) cin>>lowerleft.x>>lowerleft.y;
cout cin>>upperright.x>>upperright.y;
}
floatRectangle:
:
area()
{
return(upperright.x-lowerleft.x)*(upperright.y-lowerleft.y);
}
intmain()
{
Rectanglejuxing;
juxing.get();
floatresult;
result=juxing.area();
cout return0;
}
运行结果如下:
2.设计并测试一个“人员”类。
属性包括:
编号、性别、出生日期、身份证号等等。
其中“出生日期”定义为一个“日期”类内嵌子对象。
用成员函数实现对人员信息的录入和显示。
要求包括:
构造函数和析构函数、拷贝构造函数、内联成员函数。
思路:
构造函数包括默认构造函数、带参构造函数、拷贝构造函数,在对象创建时调用构造函数,当用一个已存在的类实例去初始化另一个新创建的类实例时,系统会调用拷贝构造函数。
在对象消除时调用析构函数。
内联函数使用inline标识,在编译阶段,内联函数的函数体代码部分会被嵌入到每一个调用语句处,在程序执行阶段,不会发生程序的控制转移,而是直接运行在编译时嵌入的代码。
代码如下:
#include
#include
usingnamespacestd;
classdate//日期类
{
intyear,month,day;
public:
date(){year=0;month=0;day=0;}//默认构造函数
date(inty,intm,intd);//带参构造函数
~date(){};//析构函数