《c++程序设计实验辅导及习题解答-实验》由会员分享,可在线阅读,更多相关《c++程序设计实验辅导及习题解答-实验(11页珍藏版)》请在金锄头文库上搜索。
1、实 验 十 一任务 1:程序调试。类静态数据的应用。下列程序设计了一个职工类,职工类中包括职工姓名、薪水和所有职工的薪水总和 allSalary。薪水总和员是所有职工对象薪水的统计和,它属于类而不属于某个对象,因而设置为 static 数据。 程序为:#include stdafx.h#include using namespace std;#include class Employeeprivate:char name30;float salary;static float allSalary; public:Employee(char *n, float s)strcpy(name, n)
2、;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 using namespace std;#include class
3、peopleprivate: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);p
4、eople e3(C, 2000);people e4(D, 1800);float all;all = people:GetAllSalary( ); cout using namespace std;#include class Pointpublic :Point(double xx ,double yy) x=xx ; y=yy;void Getxy() ;friend double Distance(Point private:double x,y; ;void Point:Getxy() cout#include class vectorprivate:double x,y;pub
5、lic:vector(double a,double b)x=a;y=b;friend double b(const vector friend double b1(const vector ;double b(const vector &s,const vector &s1)double dx=s.x+s1.x;double dy=s.y+s1.y;coutusing namespace std;class B;class Adouble x1,y1;public:A(double x,double y)x1=x;y1=y;friend int samevalue(A ;class Bdou
6、ble x2,y2;public:B(double a,double b)x2=a;y2=b;friend int samevalue(A ;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)coutusing namespace std;#includeclass Dateint y,m,d;public:Da
7、te(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):date(y1,m1,d1)strcpy(name,n1);strcpy(sex,se);void show()cout姓名是:nameendl;cout性别是:sexendl;void main()people F(沧海一声笑,1996,8, 15 ,男);Date d(1991,8, 15);F.show();d.show();