- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
实验4_构造函数与析构函数汇
HYPERLINK /491457415/blog/item/edfcfd1fc7b230f7e0fe0bd4.html \t _blank 实验四????? HYPERLINK /491457415 \t _blank 构造函数与析构函数
1、实验目的:
1)掌握构造函数和析构函数定义方法;
2)掌握构造函数的重载、具有默认值的构造函数的使用方法;
3)加深对构造函数的特殊用途的理解。
2)分析下面程序中的对象传递,写出输出结果。
#includeiostream.h
class Cylinder
{
public:
???Cylinder(){}
???Cylinder(double r,double h);
???void setcylinder(double r,double h);
???double getradius(){return radius;}
???double getheight(){return height;}
???double volume();
???double surface_area();
private:
???double radius;
???double height;
};
const double PI=3.1415926;
Cylinder::Cylinder(double r,double h)
{
???radius=r;
???height=h;
}
void Cylinder::setcylinder(double r,double h)
{
???radius=r;
???height=h;
}
double Cylinder::volume()
{
???double vol;
???vol=PI*radius*radius*height;
???return vol;
}
double Cylinder::surface_area()
{
???double area;
???area=2*PI*radius*height+2*PI*radius*radius;
???return area;
}
void main()
{
???Cylinder cylinder1(7.0,12.0),cylinder2;
???cylinder2.setcylinder(12.3,18.7);
???coutthe radius of cylinder1 is:\tcylinder1.getradius()endl;
???coutthe height of cylinder1 is:\tcylinder1.getheight()endl;
???coutthe volume of cylinder1 is:\tcylinder1.volume()endl;
???coutthe surfacearea of cylinder1 is:\tcylinder1.surface_area()endl;
???coutthe radius of cylinder2 is:\tcylinder2.getradius()endl;
???coutthe height of cylinder2 is:\tcylinder2.getheight()endl;
???coutthe volume of cylinder2 is:\tcylinder2.volume()endl;
???coutthe surfacearea of cylinder2 is:\tcylinder2.surface_area()endl;
}
运行结果:
3)根据注释将下面的程序补充完整
#includeiostream.h
class test
{
private:
???int num;
???double f1;
Public:
test();
test(int a,double b);//声明重载的构造函数
???int get_num(){return num;}
???double get_f1(){return f1;}
};
test::test(int a,double b)
{
num=a;
f1=b;
}
//定义重载的构造函数
int main()
{
???test a;
couta.get_num()endl;
couta.get_f1()endl;
test b(2,5.5);
coutb.get_num()endl;
coutb.get_f1()endl;
return 0;
}
2.2 编写并调试程序
编写一个实现两个数相减的类的测试程序,请写出类的定义,构成一个完整的程序,要求调用类的构造函数和析构函数时均有明确的输出信息。
#incl
您可能关注的文档
最近下载
- 湖北省总工会考试试题及答案.doc VIP
- 住院医师规范化培训-医学检验真题库_25.docx VIP
- 房贷提前还清协议书.docx VIP
- 公司治理教学课件(完整版).ppt VIP
- 青光眼小梁切除术.ppt VIP
- 药物设计软件:Discovery Studio二次开发all.docx VIP
- 2025年郑州澍青医学高等专科学校单招语文测试模拟题库汇编.docx VIP
- GTBO41100-2017B 项目运营手册成本弦.pdf VIP
- 2024-2025学年初中道德与法治七年级上册(2024)统编版(部编版)(2024)教学设计合集.docx
- 2025卫星互联网承载网技术白皮书.docx VIP
文档评论(0)