实验5静态成员友元对象成员_第1页
实验5静态成员友元对象成员_第2页
实验5静态成员友元对象成员_第3页
实验5静态成员友元对象成员_第4页
实验5静态成员友元对象成员_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验五静态成员、友元、对象成员1、实验目的:1) 掌握静态数据成员的定义及使用;2) 掌握静态成员函数的定义及使用。3) 掌握友员函数的定义和使用。4) 理解对象成员的概念5) 掌握对彖成员的初始化方法2、实验内容:2.1分析下面的程序,指岀程序运行的结果。1)p132 习题 3.14, 3.15, 3.20, 3.23, 3.24, 3.25, 3.26 代码:3.14# include<iostream.h>class bpublic:b()b(int i,int j)x=i;y=j;void printb()cout«x«m/,«y«e

2、ndl;private:int x,y;;class apublic:a()a(int i ,int j);void printa();private:b c;;a:a(int i,int j):c(i,j)void a:printa()(c.printb();int main()(aa(7,8);a.printa();return;程序运行结果:7,83.15程序代码:# include<iostream.h>class apublic:void set(int j)x=i;y=j;int get_y()return y;private:int x,y;class b

3、oxpublic:void set(int l,int p) lcngth=l;width=w;label.set(s,p);int ger_area()return length*width;)private:int length,width;a label;int main()box b;bset(4,6 丄 20);cout«b.gcr_arca()«cndl; return 0;程序运行结果:243.20程序代码:# include<iostream.h>class aclasspublic:aclass()total+;)acl

4、ass()total-;int gcttotal()return total;private:static int total;int aclass:total=0;int main()aclass ol,o2,o3;cout«ol .gettotal()«,robjects in existencenn; aclass *p;p=new aclass;if(!p)cout«mallocation errornn;return 1;cout«olgettotal();cout«nobjects in existence after alloca

5、tionnh; delete p;cout«ol.gettotal();cout«nobjccts in existence after allocation'n”; return 0;程序运行结果:3 objects in existence4 objects in existence after allocation3 objects in existence after allocation3.24程序代码:# include<iostream.h>class nprivate:int a;static int b;public:n(int a)a

6、=a;b+=a;static void fl(n m);void n:fl(n m)cout«ma=n«m.a«endl; cout«hb=n«b«endl;intn:b=0;int main()n p(5),q(9);n:fl(p);n:fl(q);return 0;程序运行结果:a=5b=14a=9b=143.26程序代码:# include<iostream.h>class mint a; static int b;public:m(int a)a=a;b+=a; cout«nconstructingh&#

7、171;endl;static void fl(m m);m()cout«,destructingn,';voidm:fl(m m)cout«" a="«m. a«endl;cout«"b=m«b«endl;int m:b=0;int main()mp(5),q(9);m:fl(p);m:fl(q);return 0;程序运行结果:constructingconstructinga=5b=14destructinga=9b=14destructingdestructingdestruct

8、ing#include<iostream.h>class sampleint a.b;static int c;public:sample()a=b=o;c+;void show() cout«"a=,«a«,;«"b=,'«b«7«"c=,«c«endl;;int sample: :c=l;void main()sample al,a2;al.show();a2.show();程序运行结果如下:a=0,b=0,c=3a=0,b=0,c=33)# incl

9、ude<iostream.h>#include<stdio.h>class bprivate:int a,b;public:b()b(int i,int j)a=i;b 二j;void showb()cout«ma=n«a«m;r«'f«ub=u«b«endl;class a private:b c;public:a()a(int i,intj):c(i,j)void showa()c.showb();;void main()aal(5,6); al.showa();程序运行结果:a=5,b=

10、622编写并调试程序。1) 、编写一个名为score的类,用于统计一个班的学生成绩。其中使用一个静态成员数据 tals来存储总分及一个静态成员函数re_totals()返回该总分。并编写相应的测试程序。程序编写如下:#include<iostream.h>class scoreint mark10;/m存10个学生的成绩public:static int tals;/记录总分static int num;/记录学生人数static int re_tals()/返回总分 return tals; void sct()/从键盘输入10个学生的成绩,保存到数组中,并用tals记录总分,n

11、um统计 人数for(int i=0;i<10;i+)cin»marki; tals+=marki; nu m+;int get_num()/获取学生成绩个数 return num; ;int score: :tals=o;int score:num=0;void main()score t;cout«hplease input the scores of the class:n«endl;t.set();cout«nthc number of the scores is: n«t.gct_num()«cndl; cout

12、71;hthe total score of the class is: m«t.re_tals()«endl;程序运行结果:please input the scores of the class:123456789 1()the number of the scores is: 10the total score of the class is: 552)、编写一个程序求直角坐标中一个点到直线的距离。要求设计一个点类point,含冇两个 公有成员数据x和y,代表点的坐标。另一个类为直线类line,它有三个私有成员数据a,b,c。 分别代表直线方程ax+by+c=0的系数

13、。在类line中设计一个友元函数distance计算一个点到 直线的距离。点(x,y)到直线ax+by+c二0的距离distance的计算公式如下:distance=|ax+by+c|/sqit(a*a+b*b)请根据卜面给出的部分程序编写直线类line及其成员两数,以纽成完整的程序。#in clude<iostream.h>#include<math.h>class pointpublic:float x,y;point(float i,float j)x=i;y 二j;;class lincprivate:int a.b.c;public:line(int i,in

14、t j,int k)(a=i;b=j;c=k;friend double distance(point &pjine &1);double distance(point &pjine &1)return fabs(l.a*p.x4-l.b*p.y+l.c)/sqrt(l.a*l.a+l.b*l.b);void main()point pl(2,4);line ll(l,2,3);cout«,'distance=,«distance(pl,ll)<<endl;程序运行结果:distancc=5.813783) 、编写一类cp

15、oint表示一个点的信息。在此基础上编写一个表示三角形的类tria,顶点为其 对象成员。编写完整的程序输出三角形的面积。要求用友元函数来实现三角形面积的输出。 程序如下:# include<iostream.h>#include<math.h>class cpointpublic:float al,bl;cpoint(float i,float j)al=i;bl二j;;class tri adouble ll,l2,l3,s;public:double area;cpoint a,b,c;tria(float a,float b,float c,float d,flo

16、at e,float f):a( a,b),b(c,d),c(e,f) ll=sqrt(a.al-b.al)*(a.al-b.al)+(a.bl-b.bl)*(a.bl-b.bl); l2=sqrt(a.a 1 -c.a l)*(a.a 1 -c.a l)+(a.b i -c.b 1 )*( a.b 1 -c.b 1); l3=sqrt(b.a 1 -c.a 1 )*(b.a 1 -c.a 1 )+(b.b 1 -c.b l)*(b.bl -c.b 1); s=(ll+l2+l3)/2);arca=sqrt(s*(s-l 1 )*(s-l2)*(s-l3);friend void print(tria d);;void print(tria d)三角形面积的输出void main()tria d(4.0,2.0,3.0,4.0,5.0,6.0);print(d);程序运行结果:4) >已定义了类cdate:#include <iostream.h>class cdatcint year,month,date;public:cdatet(int,intent);void print();cdate:

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论