您好,欢迎访问三七文档
1.(考察动态内存和对象类数组的知识点)定义一个工程师类Engineer,其成员数据包括编号id、姓名name和职称position,实现一个打印自身信息的成员函数printEngineer,定义一个包含四个工程师对象的对象数组,并使用new运算符申请两个匿名对象。调用printEngineer打印所有对象的信息。#includeiostreamusingnamespacestd;classEngineer{public:charid[10];charname[10];charposition[10];public:Engineer(chars1[10],chars2[10],chars3[10]){*strcpy(id,s1);*strcpy(name,s2);*strcpy(position,s3);}voidprintEngineer(){coutTheidoftheengineerisidendl;coutThenameoftheengineerisnameendl;coutThepositionoftheengineerispositionendl;}};intmain(){EngineerEngineerarray[4]={Engineer(123,John,Beijing),Engineer(234,Peter,Shanghai),Engineer(345,Paul,Guangdong),Engineer(456,James,Weihai)};Engineer*pEngineer5=newEngineer(567,Lily,Suzhou);Engineer*pEngineer6=newEngineer(678,Lucy,Nanjing);Engineerarray[0].printEngineer();Engineerarray[1].printEngineer();Engineerarray[2].printEngineer();Engineerarray[3].printEngineer();pEngineer5-printEngineer();pEngineer6-printEngineer();deletepEngineer5;return0;}2.(考察静态成员数据或静态成员函数的知识点)定义一个员工类Employee,其成员数据包括编号id、姓名name和年龄age,在构造函数中增加相关代码实现记录总员工人数和计算所有员工的平均年龄。要求:给出带所有成员数据的构造函数和析构函数,人数和平均年龄实现为静态成员数据。#includeiostreamusingnamespacestd;classEmployee{public:charid[10];charname[10];doubleage;staticdoublenumberofemployee;staticdoubletotal;staticdoubleaverage;public:Employee(chars1[10],chars2[10],ints){*strcpy(id,s1);*strcpy(name,s2);numberofemployee++;total=total+s;}~Employee(){numberofemployee--;}};doubleEmployee::numberofemployee=0;doubleEmployee::total=0;doubleEmployee::average=0;intmain(){Employeeemployee1(123,John,34);Employeeemployee2(234,Mark,35);Employeeemployee3(345,Peter,42);Employeeemployee4(456,Paul,29);Employeeemployee5(567,James,37);Employee::average=Employee::total/Employee::numberofemployee;coutTheaverageoftheageisEmployee::averageendl;return0;}3.(考察友元函数或友元类的知识点)定义一个经理类Manager,其成员数据包括编号id、姓名name和年龄age,均声明为private访问属性。再定义一个员工类Employee,其成员数据及其访问属性与经理类相同。将Manager类声明为Employee类的友元类,并在Manager类中定义一个函数访问Employee类的私有数据成员并进行打印输出。头文件:classEmployee{private:charid[10];charname[10];intage;public:Employee(chars1[10],chars2[10],ints3);friendclassManager;};YY1.cpp:#includeYY.h#includeiostreamusingnamespacestd;Employee::Employee(chars1[10],chars2[10],ints3){*strcpy(id,s1);*strcpy(name,s2);age=s3;};YY2.cpp:#includeYY.h#includeiostreamusingnamespacestd;classManager{private:charid[10];charname[10];intage;public:Manager(chars1[10],chars2[10],ints3){*strcpy(id,s1);*strcpy(name,s2);age=s3;}voidprint(){EmployeeEmployee1(123,Peter,23);coutTheidoftheEmployeeisEmployee1.idendl;coutThenameoftheEmployeeisEmployee1.nameendl;coutTheageoftheEmployeeisEmployee1.ageendl;}};intmain(){ManagerManager1(234,John,45);Manager1.print();return0;}4.(考察组合关系实现的知识点)一个项目团队Team由一名业务经理BusinessManager、一名技术经理TechnicalManager和五个普通员工Employee组成。基于组合关系由成员类实现的思想,编码实现一个项目团队。为了简化实现,业务经理类BusinessManage、技术经理类TechnicalManager和普通员工类Employee的成员数据只包括编号id、姓名name和年龄age。要求:业务经理类BusinessManage、技术经理类TechnicalManager和普通员工类Employee均给出带所有成员数据的构造函数和析构函数,并在main函数中通过Team的成员函数打印该团队所有成员的信息。通过在每个类的构造函数和析构函数中设置打印信息,总结构造函数和析构函数的调用顺序。#includeiostreamusingnamespacestd;classBusinessManager{public:charid[10];charname[10];intage;public:BusinessManager(){coutThisisprintedbytheBusinessManagerclassconstructor.endl;}~BusinessManager(){coutThisisprintedbytheBusinessManagerclassdestructor.endl;}voidsetBusinessManager(chars1[10],chars2[10],ints3){*strcpy(id,s1);*strcpy(name,s2);age=s3;}};classTechnicalManager{public:charid[10];charname[10];intage;public:TechnicalManager(){coutThisisprintedbytheechnicalTManagerclassconstructor.endl;}~TechnicalManager(){coutThisisprintedbytheTechnicalManagerclassdestructor.endl;}voidsetTechnicalManager(chars1[10],chars2[10],ints3){*strcpy(id,s1);*strcpy(name,s2);age=s3;}};classEmployee{public:charid[10];charname[10];intage;public:Employee(){coutThisisprintedbytheEmployeeclassconstructor.endl;}~Employee(){coutThisisprintedbytheEmployeeclassdestructor.endl;}voidsetEmployee(chars1[10],chars2[10],ints3){*strcpy(id,s1);*strcpy(name,s2);age=s3;}};classTeam{public:Team(){}voidprint(){coutposition:businessmanagerid:businessManager1.idname:businessManager1.nameage:businessManager1.ageendl;coutposition:technicalmanagerid:technicalManager1.idname:technicalManager1.nameage:technicalManager1.ageendl;coutposition:employeeid:employee1.idname:employee1.nameage:employee1.ageendl;coutposition:emoloyeeid:employee2.idname:employee2.nameage:employee2.ageendl;coutposition:emoloyeeid:employee3.idname:employee3.nameage:employee3.ageendl;coutposition:emoloyeeid:employee4.idname:employee4.nameage:employee4.ageendl;coutposition:emoloyeeid:employee5.idname:employee5.nameage:employee5.ageendl;}BusinessManagerbusinessManager1;TechnicalManagertechnicalManager1;Employeeemployee1;Employeeemployee2;Employeeemployee3;Employeeemployee4;Employeeemployee5;};intmain(){Teamteam1;team1.businessManager1.setBusinessManager(123,John,43);team1.technicalManag
本文标题:第9次实验
链接地址:https://www.777doc.com/doc-5597631 .html