实验4_构造函数与析构函数汇.doc

  1. 1、本文档共4页,可阅读全部内容。
  2. 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
实验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

文档评论(0)

jiupshaieuk12 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:6212135231000003

1亿VIP精品文档

相关文档