c++程序设计实验辅导及习题解答-实验11.docx_第1页
c++程序设计实验辅导及习题解答-实验11.docx_第2页
c++程序设计实验辅导及习题解答-实验11.docx_第3页
c++程序设计实验辅导及习题解答-实验11.docx_第4页
c++程序设计实验辅导及习题解答-实验11.docx_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

实 验 十 一任务1:程序调试。类静态数据的应用。下列程序设计了一个职工类,职工类中包括职工姓名、薪水和所有职工的薪水总和allSalary。薪水总和员是所有职工对象薪水的统计和,它属于类而不属于某个对象,因而设置为static数据。 程序为:#include stdafx.h#include using namespace std;#include class Employee private: char name30; float salary; static float allSalary; public: Employee(char *n, float s) strcpy(name, n); salary = s; allSalary = allSalary + salary;Employee(void)static float GetAllSalary(void) return allSalary; ;float Employee:allSalary = 0;void main(void)Employee e1(张三, 4500);Employee e2(王五, 5200);Employee e3(李四, 2450);float all;all = Employee:GetAllSalary( ); cout AllSalary = all endl;任务2:程序设计。应用类静态数据实现类名对象间的数据访问。参照任务1,定义一个类,描述某人的姓名和具有的现金数量,此某人有4个子女A、B、C、D,当初父亲具有现金10000元,每年提供给这4个子女的钱分别为1000、500、2000、1800元。当四个子女访问一次后,父亲还剩多少现金?设计代码运行如下:#include stdafx.h#include using namespace std;#include class people private: char name30; float salary; static float allSalary; public: people(char *n, float s) strcpy(name, n); salary = s; allSalary = allSalary - salary;people(void)static float GetAllSalary(void) return allSalary; ;float people:allSalary = 10000;void main(void) people e1(A, 1000);people e2(B, 500);people e3(C, 2000); people e4(D, 1800);float all;all = people:GetAllSalary( ); cout 访问一次后还剩现金为: all endl;任务3:程序调试。用友元函数计算两点间的距离。#include stdafx.h#include using namespace std;#include class Pointpublic : Point(double xx ,double yy) x=xx ; y=yy; void Getxy() ; friend double Distance(Point &a,Point &b); private: double x,y; ;void Point:Getxy() cout(x ,y)endl; double Distance(Point &a , Point &b) double dx= a.x - b.x ; double dy= a.y - b.y ; return sqrt (dx*dx+dy*dy );void main() Point p1(3.0,4.0),p2(6.0,8.0) ; p1.Getxy(); p2.Getxy(); double d=Distance(p1,p2); coutDistance is dendl;调试上述程序,回答以下问题:(1) 函数Distance是否可以定义在类外?答:不能,友元函数一定要定义在类内。(2) 是否可以用对象调用函数Distance?答:不可以,友元函数是直接调用的。任务4:程序设计有一个向量类Vector,包括一个点的坐标位置x和y,设计两个友元函数,实现两个向量的加法和减法运算。程序设计如下:#include#include class vectorprivate:double x,y;public:vector(double a,double b)x=a;y=b;friend double b(const vector &s,const vector &s1);friend double b1(const vector &s2,const vector &s3);double b(const vector &s,const vector &s1)double dx=s.x+s1.x;double dy=s.y+s1.y;cout(dx,dy)endl;return 0;double b1(const vector &s2,const vector &s3)double x1=s2.x-s3.x;double y1=s2.y-s3.y;cout(x1,y1)endl;return 0;void main() vector p1(3.0,4.0),p2(6.0,8.0); double d=b(p1,p2); double d1=b1(p1,p2);任务5:程序设计。友元函数的应用。程序设计:Same Value()函数是类A和类B的友元函数,它用来比较两个类的对象的数据成员value1、value2是否相等,A、B类的对象的数据成员从键盘输入,并用main()进行测试。程序设计如下:#include stdafx.h#includeusing namespace std;class B;class Adouble x1,y1;public:A(double x,double y)x1=x;y1=y;friend int samevalue(A &s,B &ss);class Bdouble x2,y2;public:B(double a,double b)x2=a;y2=b;friend int samevalue(A &s,B &ss);int samevalue(A &s,B &ss) if(s.x1=ss.x2&s.y1=ss.y2)return 1;else return 0;void main()double a,b;cinab;A a1(a,b);double c,d;cincd;B b1(c,d);int y=samevalue(a1,b1);if(y=1)cout数据相等endl;elsecout数据不相等endl;任务6:程序设计。容器类的程序设计。程序设计:有一个日期类Date,它有私有数据y、m、d,此类的对象作为另一个类的私有成员,用以描述一个人的姓名、出生日期及性别,编写必要的成员函数后在main函数中进行测试。程序如下:#include stdafx.h#includeusing namespace std;#includeclass Dateint y,m,d;public: Date(int a,int b,int c)y=a;m=b;d=c;void show()cout出生年月是:y年m月d日endl;class peoplechar name15;Date date;char sex6;public:people(char *n1,int y1,int m1,int d1,char *se):

温馨提示

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

最新文档

评论

0/150

提交评论