请打开考生文件夹下的解决方案文件proj2,该工程中包含一个程序文件main.cpp,其中有日期类Date、人员类Person及排序函数sortByName和主函数main的定义。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义和函数定义。此程序的正确输出结果应为: 按姓名排序 排序前 张三男 出生日期:1978年4月20日 王五女 出生日期:1965年8月3日 杨六女 出生日期:1965年9月5日 李四男 出生日期:1973年5月30日 排序后: 李四 男 出生日期:1973年5月30日 王五 女 出生日期:1965年8月3日 杨六 女 出生日期:1965年9月5日 张三 男 出生日期:1978年4月20日 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#includeiostreamusing namespace std;class Date{ //日期类 int year , month , day;//年、月、日public: Date(int year,int month,int day):year(year),month(month),day(day){} int getYear()const{return year;} int getMonth()const{return month;} int getDay()const{return day;}},class Person{ //人员类 char name[14];//姓名 bool is male;//性别,为true时表示男性 Date birth date;//出生日期public: Person(char*name,bool ismale,Date birth date)//********found******** :_____________ { strcpy(this-name,name); } const char*getName()const{returnR name;} bool isMale()conSt{return is_male;} Date getBirthdate()conSt{return birth date ;} //利用strcmp()函数比较姓名,返回一个正数、0或负数,分别表示大于、等于、小于 int compareName(const Personp)const{//********found********_____________) void Show(){ Coutendl; coutname' '(is_male?"男":"女")""出生日期:"birth date.getYear()"年"//显示出生年//********found********____________//显示出生月birth date.getDay()"日";//显示出生日 } }; void sortByName(Person ps[], int size){ //将人员数组按姓名排列为升序 for(int i=0 ; isize一1 ; i++){//采用选择排序算法 int m=i; for(int j=i+1;jsize;j++) if(ps[j].compareName(ps[m])0) m=j; if(mi){ Person P=ps[m]; ps[m]=ps[i]; ps[i]=p ; } }}int main(){ Person Staff[]={ Person("张三",true,Date(1978,4,20)), Person("王五",false,Date(1965,8,3)), Person("杨六",false,Date(1965,9,5)), Person("李四",true,Date(1973,5,30)) }; const int size = siZeof(staff)/sizeof(staff[0]); int i; coutendl"按姓名排序"; coutendl"排序前:"; for(i=0;isize ; i++)staff[i].show(); sortByName(staff,Size); Coutendlendl"排序后:"; for(i=0;isize;i++) staff[i].show(); coutendl; return 0;}
请打开考生文件夹下的解决方案文件proj3,其中声明的是一个人员信息类,补充编制程序,使其功能完整。在main函数中给出了一组测试数据,此种情况下程序的输出应该是:Zhang 20 Tsinghua。 注意:只能在函数address_change的“//*******333*******”和“//*******666*******”之间填入若干语句,不要改动程序中的其他内容。 程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。//proj3.h#includeiostream#includestringusing namespace std;Class Person{ char name[20]; int age; char*address; public: Person(){age=0;address=0;} void name_change(char*_name);//名字修改函数 void age change(int_age);//年龄修改函数 void address_change(char*_add);//地址修改函数 void info_display();//人员信息显示 ~Person();///析构函数};void writeToFile(conSt char*path);proj3.cpp#includeiostream#includestring#include"proj3.h"using namespace std; void Person::name change(char*name) { strcpy(name, name);}void Person..age_change(int_age){ age=_age;}void Person::address change(char* add){ if(address!=NULL)delete[] address;//*******333*******//*******666*******}void Person::info display(){ coutname'\t' age'\t'; if(addresS!=NULL) coutaddressendl; coutendl;}Person::~Person(){ if(address!=NULL) delete[]address ;}void msin() PerSon p1; p1.name change("Zhang"); p1.age change(20); p1.address_change("Tsing-hua University"); p1.address~change("Tsing-hua"); p1.info_di splay(); writeToFiie(" ");}
请打开考生文件夹下的解决方案文件proj2,其中在编辑窗口内显示的主程序文件中定义有类Base和Derived,以及主函数main。程序文本中位于每行“//****found****”下面的一行内有一处或多处下画线标记,请在每个下画线标记处填写合适的内容,并删除下画线标记。经修改后运行程序,得到的输出应为: sum=55。 注意:只在横线处填写适当的代码,不要改动程序中的其他内容。#includeiostreamusing namespace std;class Base{ public: Base(int m1,int m2) { metal=m1;mem2=m2; } int sum()(return mem1+mere2;) private: int mem1 ,mem2;//基类的数据成员};//派生类Derived从基类Base公有继承//*******found*******class Derired:{public: //构造函数声明 Derived(int m1,int m2,int m3); //sum函数定义,要求返回meta1、mem2和mem3之和//*******found*******int sum(){return____________+mem3;}private: int mem3; //派生类本身的数据成员};//构造函数的类外定义,要求由m1和m2分别初始化mem1和mem2,由m3初始化mem3//*******found*******__________Derived(int m1,int m2,int m3)://*******found*******_________,mem3(m3){}int main(){ Base a(4,6); Derived b(10,15,20); int sum=a.sum()+b.sum(); Cout"sum="sumendl; return 0;}
使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:(1)定义私有常量PI, 请在注释∥********1********后添加适当的语句。(2)完成类的私有常量PI的赋值,以及完成对半径radius的赋值。请在注释∥********2********后添加适当的语句。(3)完成计算圆面积的函数GetArea0。请在注释∥********3********后添加适当的语句。(4)完成计算圆周长的函数GetGirth0。请在注释∥********4********后添加适当的语句。注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#includeioStream.h#define CONST PI 3.141592Class CCircle{private: double radius; ∥********1********public: ∥********2******** CCircle(int radius) {this一radius=radius; ) double GetArea() { ∥********3******** } double GetGirth() { ∥********4******** } void SetRadius(int m) { radius=m; }};int msin(){ CCircle circle(5); coutci rcle.GetArea()end1; coutCircle.GetGirth()endl; circle.SetRadius(10); coutCircle.GetArea()end1; coutcircle.GetGirth()endl; return 0;}
使用VC6打开考生文件夹下的源程序文件modi1.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为: 100 37 32 注意:错误的语句在//******error******的下面,修改该语句即可。1 #include2 //******error******3 void main(4 {5 //******error******6 int m=0142;7 //******error******8 int n=0X27;9 int q=32;10 cout11 cout12 cout13 return;14 }
请打开考生文件夹下的解决方案文件proj2,此工程中包含一个程序文件main.cpp,其中有“部门”类Department和“职工”类Staff的定义,还有主函数main的定义。在主函数中定义了两个“职工”对象,他们属于同一部门。程序展示,当该部门改换办公室后,这两个人的办公室也同时得到改变。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 改换办公室前: 职工号:0789 姓 名:张三 部 门:人事处 办公室:521 职工号:0513 姓 名:李四 部 门:人事处 办公室:521 改换办公室后: 职工号:0789 姓 名:张三 部 门:人事处 办公室:311 职工号:0513 姓 名:李四 部 门:人事处 办公室:311 注意:只在横线处填写适当的代码,不要改动程序中的其他内容。#includeiostreamusing namespace std;class Department{ //“部门”类public: Department(const char *name,const char*Office){ strcpy(this-name,name);//********found******** } const char*getName()const{return name;} //返回部门名称//********found******** const char *getOffice()const{____________}//返回办公室房号 void change()fficeTo (const char*office){ //改换为指定房号的另一个办公室 strcpy(this-Office,office); }private: char name[20]; //部门名称 char office[20]; //部门所在办公室房号}; class Staff{ //“职工”类public://********found******** Staff(const char*my id,const char*my_name,Departmentmy dept):____________{ strcpy(this-staff_id,my id); strcpy(this-name,my name); } const char*getID()const{return staff id;} const char*getName()const {return name;} Department getDepartment()const{return dept;}private: char staff id[10]; //职工号 char name[20]; //姓名 Departmentdept; /所在部门};void showStaff(StaffStaff){ cout"职工号:"staff.getID()" "; cout"姓名:"staff.getName()" "; cout"部门:"staff.getDepartment().getName()" "; cout"办公室:"staff.getDepartment().getOffice()endl;}int msin(){ Department dept("人事处","521"); Staff Zhang("0789","张三",dept),Li("0513","李四",dept); cout"改换办公室前:"endl; showStaff(Zhang); showStaff(Li); //人事处办公室由521搬到311//***********found*********** cout"改换办公室后:"endl; showStaff(Zhang); showStaff(Li); return 0;}
请打开考生文件夹下的解决方案文件proj3,其中定义的Matrix是一个用于表示矩阵的类。成员函数max_value的功能是求出所有矩阵元素中的最大值。例如,若有3×3矩阵则调用max_value函数,返回值为3。请编写成员函数max_value。 要求: 补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。//Matrix.h#include#includeusing namespace std;const int M=18;Const int N=18;class Matrix{ int array[M][N];public: Matrix(){} int getElement(int i,int j)const{return array[i][j];} void setElement(int i,int j,int value){array[i][j]=value ;) int max value()const; void show(const char,* s)const { coutvoid readFromFile(const char*f,Matrixm){ ifstream infile(f); if(infile.fail()){cerr"打开输入文件失败!";return;) int k; for(int i=0;ik; m.setElement(i,j,k); }}int Matrix::max value()const{//***********333***********//***********666***********}int main(){ Matrix m; readFromFile(" ",m); m.show("Matfix:"); cout
请打开考生文件夹下的解决方案文件proj2,此工程包含有一个源程序文件proj2.cpp。其中定义了Base1类、Base2类和Derived类。 Base1是一个抽象类,其类体中声明了纯虚函数Show。Base2类的构造函数负责动态分配一个字符数组,并将形参指向的字符串复制到该数组中,复制功能要求通过调用strcpy函数来实现。Derived类以公有继承方式继承Base1类,以私有继承方式继承Base2类。在Derived类的构造函数的成员初始化列表中调用Base类的构造函数。 请在程序中的横线处填写适当的代码,然后删除横线,以完成Base1、Base2和Derived类的功能。此程序的正确输出结果应为: I'm a derived class. 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。1 //proj2.cpp2 #includeiostream3 #includecstring4 using namespace std;5 class basel{6 public:7 //********found******** 下列语句需要声明纯虚函数Show8 ___________;9 };1011 class Base2 {12 Drotected:13 char*p;14 Base2(const char*s)15 {16 p=new char[strlen(s)+1];17 //********found******** 下列语句将形参指向的字符串常量复制到该类的字符数组中18 ___________;19 }20 ~Base2(){de2ete[] p;}21 };2223 //********found******** De rived类公有继承Base1,私有继承Base2类24 class Derired:____________{25 public:26 //********found******** 以下构造函数调用Base2类构造函数27 Derived(const char *s) :__________28 { )29 void Show()30 {cout _p endl; }31 };3233 int main()34 {35 Base1*pb=new Derived("I' m a derived class.");36 pb-Show();37 delete pb;38 return 0;39 }