第九章关于类和对象的进一步讨论1.构造函数和函数的作用是什么?什么时候需要自己定义构造函数和函数?略2.分析下面的程序,写出其运行时的输出结果。#include<iostream>usingnamespacestd;classDate{public:Date(int,int,int);Date(int,int);Date(int);Date();voiddisplay();private:intmonth;intday;intyear;};Date::Date(intm,intd,inty):month(m),day(d),year(y){}Date::Date(intm,intd):month(m),day(d){year=2005;}Date::Date(intm):month(m){day=1;year=2005;}Date::Date(){month=1;day=1;year=2005;}voidDate::display(){cout<<month<<"/"<<day<<"/"<<year<<endl;}intmain(){Dated1(10,13,2005);Dated2(12,30);Dated3(10);Dated4;d1.display();d2.display();d3.display();d4.display();return0;}/*运行结果:*10/13/2005*12/30/2005*10/1/2005*1/1/2005*/3.如果将第2题中程序的第5行改为用默认参数,即Date(int=1,int=1,int=2005);分析程序有元问题,上机编译,分析出错信息,修改程序使之能通过编译。要求保留上面一行给出的构造函数,同时能输出与第2题的程序相同的输出结果。#include<iostream>96第九章关于类和对象的进一步讨论usingnamespacestd;classDate{public:Date(int=1,int=1,int=2005);voiddisplay();private:intmonth;intday;intyear;};Date::Date(intm,intd,inty):month(m),day(d),year(y){}voidDate::display(){cout<<month<<"/"<<day<<"/"<<year<<endl;}intmain(){Dated1(10,13,2005);Dated2(12,30);Dated3(10);Dated4;d1.display();d2.display();d3.display();d4.display();return0;}/*只保留一个构造函数,运行结果:*10/13/2005*12/30/2005*10/1/2005*1/1/2005*/4.建立一个数组,内放5个学生的数据(学号、成绩),用指针指向数组首无数,输出1,3,5个学生的数据。#include<iostream>#include<iomanip>usingnamespacestd;classStudent{public:intnum;//studentnumberdoublescore;//score};istream&operator>>(istream&input,Student&s){cout<<"num:";input>>s.num;cout<<"score:";input>>s.score;returninput;}ostream&operator<<(ostream&output,Student&s){97第九章关于类和对象的进一步讨论output<<setw(8)<<s.num<<setw(8)<<setiosflags(ios::fixed)<<setprecision(1)<<s.score;returnoutput;}intmain(){Studentstu[5],*p;for(intcnt=0;cnt<5;cnt++)cin>>stu[cnt];cout<<"The1,3,5thstudent'sinformationsare:"<<endl;for(p=stu;p<stu+5;p+=2)cout<<*p<<endl;return0;}/**num:1001*score:98*num:1002*score:93*num:1004*score:92*num:1005*score:100*num:1003*score:98*The1,3,5thstudent'sinformationsare:*100198.0*100492.0*100398.0*/5.建立一个对象数组,内放5个学生的数据(学号,成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。#include<iostream>#include<iomanip>usingnamespacestd;classStudent{private:intnum;//studentnumberdoublescore;//scorepublic:friendistream&operator>>(istream&input,Student&s);friendostream&operator<<(ostream&output,Student&s);friendvoidmax(Student*,int);};istream&operator>>(istream&input,Student&s){cout<<"num:";input>>s.num;98第九章关于类和对象的进一步讨论cout<<"score:";input>>s.score;returninput;}ostream&operator<<(ostream&output,Student&s){output<<setw(8)<<s.num<<setw(8)<<setiosflags(ios::fixed)<<setprecision(1)<<s.score;returnoutput;}voidmax(Student*stu,intn){Student*p,*max;for(max=stu,p=stu+1;p<stu+n;p++)if(p->score>max->score)max=p;cout<<"Thestudentwhohasthehighestscoreis:"<<endl;cout<<*max<<endl;}intmain(){Studentstu[5];for(intcnt=0;cnt<5;cnt++)cin>>stu[cnt];max(stu,5);return0;}/*运行结果*num:1001*score:92*num:1002*score:92*num:1003*score:93*num:1004*score:94*num:1005*score:91*Thestudentwhohasthehighestscoreis:*100494.0*/6.阅读下面程序,分析其执行过程,写出输出结果。#include<iostream>usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}//constructorvoidchange(intn,floats){num=n;score=s;}//assignvaluestotheclassvoiddisplay(){cout<<num<<""<<score<<endl;}//displaynumandscoreseperatedwithablanckspace99第九章关于类和对象的进一步讨论private:intnum;floatscore;};intmain(){Studentstud(101,78.5);stud.display();stud.change(101,80.5);stud.display();return0;}/*运行结果:*10178.5*10180.5*/7.将第6题的程序分别作以下修改,分析所修改部分的含义以及编译和运行的情况。(1)将main函数第2行改为constStudentstud(101,78.5);将stud对象定义为常对象,则其数据成员不可改变。编译将出错。(2)在上问的基础上修改程序,使之能正常运行,用change函数修改数据成员num和score的值。#include<iostream>usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}//constructorvoidchange(intn,floats)const{num=n;score=s;}//assignvaluestotheclassvoiddisplay()const{cout<<num<<""<<score<<endl;}//displaynumandscoreseperatedwithablackspaceprivate:mutableintnum;mutablefloatscore;};intmain(){constStudentstud(101,78.5);stud.display();stud.change(101,80.5);stud.display();return0;}/*运行结果:*10178.5*10180.5*/(3)将main函数改为intmain(){Studentstud(101,78.5);Student*p=&stud;100第九章关于类和对象的进一步讨论p->display();p->change(101,80.5);p->display();return0;}其他部分仍同第6题的程序。#include<iostream>usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}//constructorvoidchange(intn,floats){num=n;score=s;}//assignvaluestotheclassvoiddisplay(){cout<<num<<""<<score<<e