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

实验静态成员1、实验目的: 1)掌握静态数据成员的定义及使用; 2)掌握静态成员的定义及使用。)掌握友员函数的定义和使用。)理解对象成员的概念 )掌握对象成员的初始化方法2、实验内容: 21 分析下面的程序,指出程序运行的结果。程序运行结果: #includeiostream.h class Sample { int a,b; static int c; public: Sample() { a=b=0; c++; } void show() { couta=a,b=b,c=cendl; } }; int Sample::c=1; void main() { Sample a1,a2; a1.show(); a2.show(); } 程序运行结果如下: a=0,b=0,c=3 a=0,b=0,c=3 3) #includeiostream.h #includestdio.h class B { private: int a,b; public: B() {} B(int i,int j) { a=i; b=j; } void showB() { couta=a,\tb=bendl; } }; class A { private: B c; public: A(){} A(int i,int j):c(i,j) {} void showA() { c.showB(); } }; void main() { A a1(5,6); a1.showA(); } 程序运行结果:2.2编写并调试程序。 编写一个名为score的类,用于统计一个班的学生成绩。其中使用一个静态成员数据tals来存储总分及一个静态成员函数re_totals()返回该总分。并编写相应的测试程序。 程序编写如下: #includeiostream.h class score { public: static int tals;//记录总分 static int num;//记录学生人数 static int re_tals()//返回总分 { return tals; } void set()//从键盘输入10个学生的成绩,保存到数组中,并用tals记录总分,num统计人数 { for(int i=0;i10;i++) { cinmark[i]; tals=tals+mark[i]; num++; } } int get_num()//获取学生成绩个数 { return num; } }; int score::tals=0; int score::num=0; void main() { score t; coutPlease input the scores of the class:endl; t.set(); coutThe number of the scores is: t.get_num()endl; coutThe total score of the class is: t.re_tals()endl; }程序运行结果: Please input the scores of the class: 1 2 3 4 5 6 7 8 9 10 The number of the scores is:10 The total score of the class is:55 2)、编写一个程序求直角坐标中一个点到直线的距离。要求设计一个点类point,含有两个公有成员数据x和y,代表点的坐标。另一个类为直线类line,它有三个私有成员数据a,,c。分别代表直线方程ax+by+c=0的系数。在类line中设计一个友元函数distance计算一个点到直线的距离。点(x,y)到直线ax+by+c=0的距离distance的计算公式如下: distance=|ax+byc|/sqrt(a*a+b*b) 请根据下面给出的部分程序编写直线类line及其成员函数,以组成完整的程序。 #includeiostream #includemath.h using namespace std; class line; class point { float x,y; point(float i,float j) { x=i; y=j; } }; class line { float a,b,c; line(float m,float n,float p) { a=m,b=n;c=p; } public: friend float distance(const point s,cons

文档评论(0)

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

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

1亿VIP精品文档

相关文档