c++类与对象.doc
上传人:sy****28 上传时间:2024-09-14 格式:DOC 页数:11 大小:121KB 金币:16 举报 版权申诉
预览加载中,请您耐心等待几秒...

c++类与对象.doc

c++类与对象.doc

预览

免费试读已结束,剩余 1 页请下载文档后查看

16 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

C++程序设计语言实验报告第4章类与对象报告书名称实验4-1类与对象1姓名林伯峥指导教师乐老师学号120352010008日期2011-10-10任务清单场景声明一个datatype(数据类型)类,能处理包含字符型、整型、浮点型3种类型数据,给出其构造函数。实验方法以下程序代码是上述场景的实现代码,仔细阅读这些代码,并给出详细注解。程序名:lab4_2.cpp#include<iostream>usingnamespacestd;classdatatype{private:enum{character,integer,floating_point}vartype;union{charc;inti;floatf;};public:datatype(charch){vartype=character;c=ch;}datatype(intii){vartype=integer;i=ii;}datatype(floatff){vartype=floating_point;f=ff;}voidprint();};voiddatatype::print(){switch(vartype){casecharacter:cout<<"字符型:"<<c<<endl;break;caseinteger:cout<<"整型:"<<i<<endl;break;casefloating_point:cout<<"浮点型:"<<f<<endl;break;}}main(){datatypeA('c'),B(12),C(1.44F);A.print();B.print();C.print();}实验预估时间30分钟实验结果#include<iostream>//预编译命令usingnamespacestd;//使用名字空间classdatatype{//datatype类型定义private://私有数据成员enum{character,integer,floating_point}vartype;//枚举定义union{charc;inti;floatf;};//构造函数public://外部接口datatype(charch)//构造字符型函数{vartype=character;c=ch;}datatype(intii)//构造整数行函数{vartype=integer;i=ii;}datatype(floatff)//构造浮点型函数{vartype=floating_point;f=ff;}voidprint();//声明print()型函数};voiddatatype::print()//定义print()型函数{switch(vartype)//分支循环{casecharacter:cout<<"字符型:"<<c<<endl;break;caseinteger:cout<<"整型:"<<i<<endl;break;casefloating_point:cout<<"浮点型:"<<f<<endl;break;}}main()//定义主函数{datatypeA('c'),B(12),C(1.44F);//定义变量值A.print();//调用函数B.print();//调用函数C.print();//调用函数}报告书名称实验4-2类与对象2姓名林伯睁指导教师学号日期任务清单场景声明一个Circle类,有数据成员Radius(半径)、成员函数GetArea(),计算圆的面积,构造一个Circle的对象进行测试。实验方法编写一个Circle类,要求有构造函数和析构函数,在main()函数中测试这些函数,观察构造函数和析构函数的的执行过程。程序名:lab4_2.cpp实验预估时间15分钟实验结果#include<iostream>usingnamespacestd;constfloatp=(float)3.14;classcircle{public:voidcircle::GetArea(floatr);~circle(){cout<<"调用析构函数"<<endl;}floatmain