C++课后习题第八章-第十二章_第1页
C++课后习题第八章-第十二章_第2页
C++课后习题第八章-第十二章_第3页
C++课后习题第八章-第十二章_第4页
C++课后习题第八章-第十二章_第5页
已阅读5页,还剩81页未读 继续免费阅读

下载本文档

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

文档简介

第八章请检查下面的程序,找出其中的错误(先不要上机,在纸面上作人工检查),并改正。然后上机调试,使之能正常运行。运行时从键盘输入时、分、秒的值,检查输出是否正确。原文:#include<iostream>usingnamespacestd;classTime{ voidset_time(void) ; voidshow_time(void); inthour; intminute; intsec;};Timet;intmain(){ set_time(); show_time(); return0;}voidset_time(void) { cin>>t.hour; cin>>t.minute; cin>>t.sec;}voidshow_time(void){ cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;}改:#include<iostream>usingnamespacestd;classTime{ public://成员改为公用的 inthour; intminute; intsec;};Timet;voidset_time(void) {//在main函数之前定义 cin>>t.hour; cin>>t.minute; cin>>t.sec;}voidshow_time(void){//在main函数之前定义 cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;}intmain(){ set_time(); show_time(); return0;}改写例8.1程序,要求:将数据成员改为私有的;将输入和输出的功能改为由成员函数实现;在类体内定义成员函数;#include<iostream>usingnamespacestd;classTime{ public: voidset_time(void){ cin>>hour; cin>>minute; cin>>sec; } voidshow_time(void){ cout<<hour<<":"<<minute<<":"<<sec<<endl; } private: inthour; intminute; intsec;};Timet;intmain(){ t.set_time(); t.show_time(); return0;}在第2题的基础上进行如下修改:在类体内声明成员函数,而在类外定义成员函数。#include<iostream>usingnamespacestd;classTime{ public: voidset_time(void); voidshow_time(void); private: inthour; intminute; intsec;};voidTime::set_time(void){ cin>>hour; cin>>minute; cin>>sec;}voidTime::show_time(void){ cout<<hour<<":"<<minute<<":"<<sec<<endl;}Timet;intmain(){ t.set_time(); t.show_time(); return0;}在第8.3.3节中分别给出了包含类定义的头文件student.h,包含成员函数定义的源文件studnet.cpp以及包含主函数的源文件main.cpp。请对该程序完善化,在类中增加一个对数据成员赋初值的成员函数set_value。原文8.3.3:#include<iostream>usingnamespacestd;classStudent{ public: voiddisplay(){ cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl; }; private: intnum; charname[20]; charsex;};intmain(){ Studentstud; stud.display(); return0;}改:main.cpp#include<iostream>#include"student.h"usingnamespacestd;intmain(){ Students; s.SetValue(); s.Display(); return0;}Student.husingnamespacestd;classStudent{ public: voidDisplay(); voidSetValue(); private: intnum; charname[20]; charsex;};Student.cpp#include<iostream>#include"student.h"usingnamespacestd;voidStudent::Display(){ cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl;}voidStudent::SetValue(){ cin>>num>>name>>sex;}将例8.4改写为一个多文件的程序:将类定义放在头文件arraymax.h中;将成员函数定义放在源文件arrymax.cpp中;主函数放在源文件file1.cpp中。原文例8.4:#include<iostream>usingnamespacestd;classArray_max{ public: voidset_value(); voidmax_value(); voidshow_value(); private: intarray[10]; intmax;};voidArray_max::set_value(){ inti; for(i=0;i<10;i++) cin>>array[i];}voidArray_max::max_value(){ inti; max=array[0]; for(i=1;i<10;i++) if(array[i]>max)max=array[i];}voidArray_max::show_value(){ cout<<"max="<<max<<endl;}intmain(){ Array_max arrmax; arrmax.set_value(); arrmax.max_value(); arrmax.show_value(); return0;}改:main.cpp#include<iostream>#include"arraymax.h"usingnamespacestd;intmain(){ ArrayMaxarrmax; arrmax.SetValue(); arrmax.MaxValue(); arrmax.ShowMax(); return0;}arraymax.hclassArrayMax{ public: voidSetValue();//设置数组元素值 voidMaxValue();//找出最大值 voidShowMax();//输出最大值 private: intarray[10];//整型数组 intmax;//数组最大值};arraymax.cpp#include<iostream>#include"arraymax.h"usingnamespacestd;voidArrayMax::SetValue(){ inti; for(i=0;i<10;i++){ cin>>array[i]; }}voidArrayMax::MaxValue(){ inti; max=array[0]; for(i=1;i<10;i++){ if(max<array[i]){ max=array[i]; } }}voidArrayMax::ShowMax(){ cout<<"max="<<max<<endl;}需要求3个长方柱的体积,请编写一个基于对象的程序。数据成员包括length(长)、width(宽)和weight(高)。要求用成员函数实现以下功能。由键盘分别输入3个长方柱的长、宽和高。计算长方柱的体积;输出3个长方柱的体积。#include<iostream>usingnamespacestd;classBox{ public: voidget_value(); floatvolume(); voiddisplay(); public: floatlengh; floatwidth; floatheight;};voidBox::get_value(){ cout<<"pleaseinputlengh,width,height:"; cin>>lengh;//由键盘分别输入3个长方柱的长、宽和高。 cin>>width; cin>>height;}floatBox::volume(){ return(lengh*width*height);//计算长方体体积。}voidBox::display(){ cout<<volume()<<endl;}intmain(){ Boxbox1,box2,box3;//输出3个长方体的体积。 box1.get_value(); cout<<"volmueofbax1is"; box1.display(); box2.get_value(); cout<<"volmueofbax2is"; box2.display(); box3.get_value(); cout<<"volmueofbax3is"; box3.display(); return0;}第九章分析下面的程序,写出其运行时的输出结果。#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;}如果将第2题程序的第5行改为用默认参数,即Date(int=1;int=1;int=2005);分析程序有无问题。上机编译,分析出错信息,修改程序使之能通过编译。要求保留上面一行给出的构造函数,同时能输出与第2题程序相同的输出结果。改:#include<iostream>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;}建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5学生的数据。#include<iostream>usingnamespacestd;classStudent{ public: Student(intn,floats):num(n),score(s){} voiddisplay(); private: intnum; floatscore;};voidStudent::display(){ cout<<"num="<<num<<""<<"score="<<score<<endl;}intmain(){ Studentstud[5]={ Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5) }; Student*p=stud; for(inti=0;i<=2;p=p+2,i++) p->display(); return0;}建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针做函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。#include<iostream>usingnamespacestd;classStudent{ public: Student(intn,floats):num(n),score(s){} intnum; floatscore;};intmain(){ Studentstud[5]={ Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5) }; voidmax(Student*); Student*p=&stud[0]; max(p); return0;}voidmax(Student*arr){ floatmax_score=arr[0].score; intk=0; for(inti=1;i<5;i++) if(arr[i].score>max_score){ max_score=arr[i].score; k=i; } cout<<"num="<<arr[k].num<<""<<"max_score="<<max_score<<endl;}阅读下面程序,分析其执行过程,写出输出结果#include<iostream>usingnamespacestd;classStudent{ public: Student(intn,floats):num(n),score(s){}voidchange(intn,floats){ num=n; score=s; }voiddisplay(){ cout<<num<<""<<score<<endl; } private: intnum; floatscore;};intmain(){ Studentstud(101,78.5); stud.display(); stud.change(101,80.5); stud.display(); return0;}将第6题程序分别作一下修改,分析所修改部分的含义以及编译和运行的情况。将main函数第2行改为constStudentstud(01,78.5);在(1)的基础上修改程序,使之能正常运行,用change函数修改数据成员mun和score的值。将main函数改为intmain(){Studentstud(101,78.5);Student*p=&stud;>display();P->change(101,80.5);P-display();return0;}其他部分仍同第6题程序。在(2)的基础上将main函数第3行改为constStudent*p=&stud;再把main函数第3行改为Student*constp=&stud;(1)[Error]passing'constStudent'as'this'argumentof'voidStudent::display()'discardsqualifiers[-fpermissive][Error]passing'constStudent'as'this'argumentof'voidStudent::change(int,float)'discardsqualifiers[-fpermissive][Error]passing'constStudent'as'this'argumentof'voidStudent::display()'discardsqualifiers[-fpermissive](2)

#include<iostream>usingnamespacestd;classStudent{ public: Student(intn,floats):num(n),score(s){} voidchange(intn,floats)const{ num=n; score=s; }voiddisplay()const{ cout<<num<<""<<score<<endl; } private: mutableintnum; mutablefloatscore;};intmain(){ constStudentstud(101,78.5); stud.display(); stud.change(101,80.5); stud.display(); return0;}(3)#include<iostream>usingnamespacestd;classStudent{ public: Student(intn,floats):num(n),score(s){} voidchange(intn,floats){ num=n; score=s; }voiddisplay(){ cout<<num<<""<<score<<endl; } private: intnum; floatscore;};intmain(){ Studentstud(101,78.5); Student*p=&stud; p->display(); p->change(101,80.5); p->display(); return0;}(4)#include<iostream>usingnamespacestd;classStudent{ public: Student(intn,floats):num(n),score(s){} voidchange(intn,floats){ num=n; score=s; } voiddisplay()const{ cout<<num<<""<<score<<endl; } private: intnum; floatscore;};intmain(){ Studentstud(101,78.5); constStudent*p=&stud; p->display(); stud.change(101,80.5); p->display(); return0;}(5)#include<iostream>usingnamespacestd;classStudent{ public: Student(intn,floats):num(n),score(s){} voidchange(intn,floats){ num=n; score=s; } voiddisplay(){ cout<<num<<""<<score<<endl; } private: intnum; floatscore;};intmain(){ Studentstud(101,78.5); Student*constp=&stud; p->display(); p->change(101,80.5); p->display(); return0;}修改第6题程序,增加一个fun函数,改写main函数。改为在fun函数中调用change和display函数。在fun函数中使用对象引用(Student&)作为形参。#include<iostream>usingnamespacestd;classStudent{ public: Student(intn,floats):num(n),score(s){}voidchange(intn,floats){ num=n; score=s; }voiddisplay(){ cout<<num<<""<<score<<endl; } private: intnum; floatscore;};voidfun(Student&stu){ stu.display(); stu.change(101,80.5); stu.display();}intmain(){ Studentstud(101,78.5); voidfun(Student&); fun(stud); return0;}商店销售某一商品,每天公布统一的折扣(discount)。同时允许销售人员在销售时灵活掌握售价(price),在此基础上,一次购10件以上者,还可以享受9.8折优惠。现已知当天3个销货员销售情况为销货员号(num)销货件数(quantity)销货单价(price)101523.51021224.5610310021.5请编写程序,计算出当日此商品的总销售款sum以及每件商品的平均售价。要求用静态数据成员和静态成员函数。(提示:将折扣discount,总销售款sum和商品销售总件数n声明为静态数据成员,再定义静态成员函数average(求平均售价)和display(输出结果))。#include<iostream>usingnamespacestd;classProduct{ public: Product(intn,intq,floatp):num(n),quantity(q),price(p){} voidtotal(); staticfloataverage(); staticvoiddisplay(); private: intnum; intquantity; floatprice; staticfloatdiscount; staticfloatsum; staticintn;};voidProduct::total(){ floatrate=1.0; if(quantity>10)rate=0.98*rate; sum=sum+quantity*price*rate*(1-discount); n=n+quantity;}voidProduct::display(){ cout<<sum<<endl; cout<<average()<<endl;}floatProduct::average(){ return(sum/n);}floatProduct::discount=0.05;floatProduct::sum=0;intProduct::n=0;intmain(){ ProductProd[3]={ Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5) }; for(inti=0;i<3;i++) Prod[i].total(); Product::display(); return0;}将例9.13程序中的display函数不放在Time类中,而作为类外的普通函数,然后分别在Time和Date类中将display声明为友元函数。在主函数中调用display函数,display函数分别引用Time和Date两个类的对象的私有数据,输出年、月、日和时、分、秒。#include<iostream>usingnamespacestd;classDate;classTime{ public: Time(int,int,int); friendvoiddisplay(constDate&,constTime&); private: inthour; intminute; intsec;};Time::Time(inth,intm,ints){ hour=h; minute=m; sec=s;}classDate{ public: Date(int,int,int); friendvoiddisplay(constDate&,constTime&); private: intmonth; intday; intyear;};Date::Date(intm,intd,inty){ month=m; day=d; year=y;}voiddisplay(constDate&d,constTime&t){ cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;}intmain(){ Timet1(10,13,56); Dated1(12,25,2004); display(d1,t1); return0;}将例9.13中的Time类声明为Date类的友元类,通过Time类中的display函数引用Date类对象的私有数据,输出年、月、日和时、分、秒。#include<iostream>usingnamespacestd;classTime;classDate{ public: Date(int,int,int); friendTime; private: intmonth; intday; intyear;};Date::Date(intm,intd,inty):month(m),day(d),year(y){}classTime{ public: Time(int,int,int); voiddisplay(constDate&); private: inthour; intminute; intsec;};Time::Time(inth,intm,ints):hour(h),minute(m),sec(s){}voidTime::display(constDate&d){ cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; cout<<hour<<":"<<minute<<":"<<sec<<endl;}intmain(){ Timet1(10,13,56); Dated1(12,25,2004); t1.display(d1); return0;}将例9.14改写为在类模板外定义各成员函数。#include<iostream>usingnamespacestd;template<classnumtype>classCompare{ public: Compare(numtypea,numtypeb); numtypemax(); numtypemin(); private: numtypex,y;};template<classnumtype>Compare<numtype>::Compare(numtypea,numtypeb){ x=a; y=b;}template<classnumtype>numtypeCompare<numtype>::max(){ return(x>y)?x:y;}template<classnumtype>numtypeCompare<numtype>::min(){ return(x<y)?x:y;}intmain(){ Compare<int>cmp1(3,7); cout<<cmp1.max()<<"istheMaximumoftwointegernumbers."<<endl; cout<<cmp1.min()<<"istheMinimumoftwointegernumbers."<<endl<<endl; Compare<float>cmp2(45.78,93.6); cout<<cmp2.max()<<"istheMaximumoftwofloatnumbers."<<endl; cout<<cmp2.min()<<"istheMinimumoftwofloatnumbers."<<endl<<endl; Compare<char>cmp3('a','A'); cout<<cmp3.max()<<"istheMaximumoftwocharacters."<<endl; cout<<cmp3.min()<<"istheMinimumoftwocharacters."<<endl; return0;}第十章定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。#include<iostream>usingnamespacestd;classComplex{ public: Complex(){ real=0; imag=0; }Complex(doubler,doublei){ real=r; imag=i; } doubleget_real(); doubleget_imag(); voiddisplay(); private: doublereal; doubleimag;};doubleComplex::get_real(){ returnreal;}doubleComplex::get_imag(){ returnimag;}voidComplex::display(){ cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complexoperator+(Complex&c1,Complex&c2){ return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());}intmain(){ Complexc1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<"c3="; c3.display(); return0;}定义一个复数类Complex,重载运算符“+”,“-”,“*”,“/”,使之能用于复数的加、减、乘和除。运算符重载函数作为Complex类的成员函数。编写程序,分别求两个复数之和、差、积和商。#include<iostream>usingnamespacestd;classComplex{ public: Complex(){ real=0; imag=0; }Complex(doubler,doublei){ real=r; imag=i; }Complexoperator+(Complex&c2); Complexoperator-(Complex&c2); Complexoperator*(Complex&c2); Complexoperator/(Complex&c2); voiddisplay(); private: doublereal; doubleimag;};ComplexComplex::operator+(Complex&c2){ Complexc; c.real=real+c2.real; c.imag=imag+c2.imag; returnc;}ComplexComplex::operator-(Complex&c2){ Complexc; c.real=real-c2.real; c.imag=imag-c2.imag; returnc;}ComplexComplex::operator*(Complex&c2){ Complexc; c.real=real*c2.real-imag*c2.imag; c.imag=imag*c2.real+real*c2.imag; returnc;}ComplexComplex::operator/(Complex&c2){ Complexc; c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); returnc;}voidComplex::display(){ cout<<"("<<real<<","<<imag<<"i)"<<endl;}intmain(){ Complexc1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<"c1+c2="; c3.display(); c3=c1-c2; cout<<"c1-c2="; c3.display(); c3=c1*c2; cout<<"c1*c2="; c3.display(); c3=c1/c2; cout<<"c1/c2="; c3.display(); return0;}定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以是其中有一个是整数,顺序任意。例如,c1+c2,i+c1和c1+i均合法(设i为整数,c1和c2为复数)。编写程序,分别求两个复数之和、整数和复数之和。#include<iostream>usingnamespacestd;classComplex{ public: Complex(){ real=0; imag=0; } Complex(doubler,doublei){ real=r; imag=i; } Complexoperator+(Complex&c2); Complexoperator+(int&i); friendComplexoperator+(int&,Complex&); voiddisplay(); private: doublereal; doubleimag;};ComplexComplex::operator+(Complex&c){ returnComplex(real+c.real,imag+c.imag);}ComplexComplex::operator+(int&i){ returnComplex(real+i,imag);}voidComplex::display(){ cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complexoperator+(int&i,Complex&c){ returnComplex(i+c.real,c.imag);}intmain(){ Complexc1(3,4),c2(5,-10),c3; inti=5; c3=c1+c2; cout<<"c1+c2="; c3.display(); c3=i+c1; cout<<"i+c1="; c3.display(); c3=c1+i; cout<<"c1+i="; c3.display(); return0;}有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加(如c=a+b)。#include<iostream>usingnamespacestd;classMatrix{ //定义Matrix类 public: Matrix(); //默认构造函数 friendMatrixoperator+(Matrix&,Matrix&); //重载运算符“+” voidinput(); //输入数据函数 voiddisplay(); //输出数据函数 private: intmat[2][3];};Matrix::Matrix(){ //定义构造函数 for(inti=0;i<2;i++) for(intj=0;j<3;j++) mat[i][j]=0;}Matrixoperator+(Matrix&a,Matrix&b){//定义重载运算符“+”函数 Matrixc; for(inti=0;i<2;i++) for(intj=0;j<3;j++){ c.mat[i][j]=a.mat[i][j]+b.mat[i][j]; } returnc;}voidMatrix::input(){//定义输入数据函数 cout<<"inputvalueofmatrix:"<<endl; for(inti=0;i<2;i++) for(intj=0;j<3;j++) cin>>mat[i][j];}voidMatrix::display(){ //定义输出数据函数 for(inti=0;i<2;i++){ for(intj=0;j<3;j++){ cout<<mat[i][j]<<""; } cout<<endl; }}intmain(){ Matrixa,b,c; a.input(); b.input(); cout<<endl<<"Matrixa:"<<endl; a.display(); cout<<endl<<"Matrixb:"<<endl; b.display(); c=a+b; //用重载运算符“+”实现两个矩阵相加 cout<<endl<<"Matrixc=Matrixa+Matrixb:"<<endl; c.display(); return0;}在第4题的基础上,重载流插入运算符“<<”和流提取运算符“>>”,使之能用于该矩阵的输入和输出。#include<iostream>usingnamespacestd;classMatrix{ public: Matrix(); friendMatrixoperator+(Matrix&,Matrix&); friendostream&operator<<(ostream&,Matrix&); friendistream&operator>>(istream&,Matrix&); private: intmat[2][3];};Matrix::Matrix(){ for(inti=0;i<2;i++) for(intj=0;j<3;j++) mat[i][j]=0;}Matrixoperator+(Matrix&a,Matrix&b){ Matrixc; for(inti=0;i<2;i++) for(intj=0;j<3;j++){ c.mat[i][j]=a.mat[i][j]+b.mat[i][j]; } returnc;}istream&operator>>(istream&in,Matrix&m){ cout<<"inputvalueofmatrix:"<<endl; for(inti=0;i<2;i++) for(intj=0;j<3;j++) in>>m.mat[i][j]; returnin;}ostream&operator<<(ostream&out,Matrix&m){ for(inti=0;i<2;i++){ for(intj=0;j<3;j++){ out<<m.mat[i][j]<<""; } out<<endl; } returnout;}intmain(){ Matrixa,b,c; cin>>a; cin>>b; cout<<endl<<"Matrixa:"<<endl<<a<<endl; cout<<endl<<"Matrixb:"<<endl<<b<<endl; c=a+b; cout<<endl<<"Matrix c = Matrix a + Matrixb:"<<endl<<c<<endl; return0;}请编写程序,处理一个复数与一个double数相加的运算,结果存放在一个double型的变量d1中,输出d1的值,再以复数形式输出此值。定义Complex(复数)类,在成员函数中包含重载类型转换运算符:operatordouble(){returnreal;}#include<iostream>usingnamespacestd;classComplex{ public: Complex(){ real=0; imag=0; } Complex(doubler){ real=r; imag=0; } Complex(doubler,doublei){ real=r; imag=i; } operatordouble(){ returnreal; } voiddisplay(); private: doublereal; doubleimag;};voidComplex::display(){ cout<<"("<<real<<","<<imag<<")"<<endl;}intmain(){ Complexc1(3,4),c2; doubled1; d1=2.5+c1; cout<<"d1="<<d1<<endl; c2=Complex(d1); cout<<"c2="; c2.display(); return0;}定义一个Teacher(教师)类和一个Student(学生)类,二者有一部分数据成员是相同的,例如num(号码),name(姓名),sex(性别)。编写程序,将一个Student对象(学生)转换为Teacher(教师)类,只将以上3个相同的数据成员移植过去。可以设想为:一位学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说任然是有用的,应当保留并成为其教师数据的一部分。#include<iostream>#include<string.h>usingnamespacestd;classStudent{ public: Student(int,char[],char,float); intget_num(){ returnnum; } char*get_name(){ returnname; } charget_sex(){ returnsex; } voiddisplay(){ cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<"\nscore:"<<score<<"\n\n"; } private: intnum; charname[20]; charsex; floatscore;};Student::Student(intn,charnam[],chars,floatso){ num=n; strcpy(name,nam); sex=s; score=so;}classTeacher{ public: Teacher(){} Teacher(Student&); Teacher(intn,charnam[],charsex,floatpay); voiddisplay(); private: intnum; charname[20]; charsex; floatpay;};Teacher::Teacher(intn,charnam[],chars,floatp){ num=n; strcpy(name,nam); sex=s; pay=p;}Teacher::Teacher(Student&stud){ num=stud.get_num(); strcpy(name,stud.get_name()); sex=stud.get_sex(); pay=1500;}voidTeacher::display(){ cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<"\npay:"<<pay<<"\n\n";}intmain(){ Teacherteacher1(10001,"Li",'f',1234.5),teacher2; Studentstudent1(20010,"Wang",'m',89.5); cout<<"student1:"<<endl; student1.display(); teacher2=Teacher(student1); cout<<"teacher2:"<<endl; teacher2.display(); return0;}第十一章将例11.1的程序片段补充和改写成一个完整、正确地程序,用公用继承方式。在程序中应包括输入数据的函数,在程序运行时输入num,name,sex,age和addr的值,程序应输出以上5个数据的值。#include<iostream>usingnamespacestd;classStudent{ public: voidget_value(){ cin>>num>>name>>sex; } voiddisplay(){ cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl; } private: intnum; charname[10]; charsex;};classStudent1:publicStudent{ public: voidget_value_1(){ get_value(); cin>>age>>addr; } voiddisplay_1(){ cout<<"age:"<<age<<endl;//引用派生类的私有成员,正确。 cout<<"address:"<<addr<<endl; }//引用派生类的私有成员,正确。 private: intage; charaddr[30];};int main(){ Student1stud1; stud1.get_value_1(); stud1.display(); stud1.display_1(); return0;}将例11.2的程序片段补充和改写成一个完整、正确地程序,用私有继承方式。在程序中应包括输入数据的函数,在程序运行时输入num,name,sex,age和addr的值,程序应输出以上5个数据的值。#include<iostream>usingnamespacestd;classStudent{ public: voidget_value(){ cin>>num>>name>>sex; } voiddisplay(){ cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl; } private: intnum; charname[10]; charsex;};classStudent1:privateStudent{ public: voidget_value_1(){ get_value(); cin>>age>>addr; } voiddisplay_1(){ display(); cout<<"age:"<<age<<endl;//引用派生类的私有成员,正确。 cout<<"address:"<<addr<<endl; }//引用派生类的私有成员,正确。 private: intage; charaddr[30];};intmain(){ Student1stud1; stud1.get_value_1(); stud1.display_1(); return0;}将例11.3的程序片段修改和补充成一个完整、正确地程序,用保护继承方式。在程序中应包括输入数据的函数。#include<iostream>usingnamespacestd;classStudent{//声明基类 public://基类公用成员 voidget_value(); voiddisplay(); protected://基类保护成员 intnum; charname[10]; charsex;};voidStudent::get_value(){ cin>>num>>name>>sex;}voidStudent::display(){ cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl;}classStudent1:protectedStudent{ //声明一个保护派生类 public: voidget_value_1(); voiddisplay1(); private: intage; charaddr[30];};voidStudent1::get_value_1(){ get_value(); cin>>age>>addr;}voidStudent1::display1(){ cout<<"num:"<<num<<endl;//引用基类的保护成员 cout<<"name:"<<name<<endl;//引用基类的保护成员 cout<<"sex:"<<sex<<endl;//引用基类的保护成员 cout<<"age:"<<age<<endl; //引用派生类的私有成员 cout<<"address:"<<addr<<endl; //引用派生类的私有成员}intmain(){ Student1stud1;//student1类的对象 stud1.get_value_1();//stud1的公用成员函数 stud1.display1();//stud1是派生类 return0;}修改例11.3的程序,改为公用继承方式。上机调试程序,使之能正确运行并得到正确地结果。对这两种继承方式作比较分析,考虑在什么情况下二者不能互相替代。#include<iostream>usingnamespacestd;classStudent{ //声明基类 public: //基类公用成员 voidget_value(); voiddisplay(); protected://基类保护成员 intnum; charname[10]; charsex;};voidStudent::get_value(){ cin>>num>>name>>sex;}voidStudent::display(){ cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl;}classStudent1:publicStudent{ //声明一个公用派生类 public: voidget_value_1(); voiddisplay1(); private: intage; charaddr[30];};voidStudent1::get_value_1(){ get_value(); cin>>age>>addr;}voidStudent1::display1(){ cout<<"num:"<<num<<endl;//引用基类的保护成员,合法 cout<<"name:"<<name<<endl;//引用基类的保护成员,合法 cout<<"sex:"<<sex<<endl;//引用基类的保护成员,合法 cout<<"age:"<<age<<endl;//引用派生类的私有成员,合法 cout<<"address:"<<addr<<endl;//引用派生类的私有成员,合法}intmain(){ Student1stud1;//stud1是派生类student1类的对象 stud1.get_value_1();//调用派生类对象stud1的公用成员函数get_value_1 stud1.display1();//调用派生类对象stud1的公用成员函数display1 return0;}有以下程序结构,请分析访问属性。在main函数中能否用b1.i,b1.j和b1.k引用派生类B对象b1中基类A的成员?派生类B中的成员函数能否调用基类A中的成员函数f1和f2?派生类B中的成员函数能否引用基类A中的数据成员i,j和k?能否在main函数中用c1.i,c1.j,c1.k,c1.m,c1.n和c1.p引用基类A的成员i,j和k,派生类B的成员m和n,以及派生类C的成员P?能否在main函数中用c1.f1(),c1.f2(),c1.f3()和c1.f4()调用f1,f2,f3和f4成员函数?派生类C的成员函数f4能否调用基类A中的成员函数f1,f2和派生类中的成员函数f3?classA{ //A为基类 public: voidf1(); inti; protected: voidf2(); intj; private: intk;};classB:publicA{ public: voidf3(); protected: intm; private: intn;//B为A的公用派生类};classC:publicB{ public: voidf4();//C为B的公用派生类 private: intp;};intmain(){ Aa1;//a1是基类A的对象 Bb1;//b1是派生类B的对象 Cc1;//c1是派生类C的对象 return0;}答案总结:(1)类里面能调用该类或基类的非私有成员变量或成员函数。(2)类外对象能调用该类或基类的公有成员变量或成员函数。有以下程序结构,请分析所有成员在各类的范围内的访问属性。#include<iostream>usingnamespacestd;classA{ public: voidf1(); protected: voidf2(); private: inti;};classB:publicA{ public: voidf3(); intk; private: intm;};classC:protectedB{ public: voidf4(); protected: intn; private: intp;};classD:privateC{ public: voidf5(); protected: intq; private: intr;};intmain(){ Aa1; Bb1; Cc1; Dd1; return0;}有如下程序,请完成下面工作:阅读程度,写出运行时输出的结果。然后上机运行,验证结果是否正确。分析程序执行过程,尤其是调用构造函数的过程。#include<iostream>usingnamespacestd;classA{ public: A(){ a=0; b=0; } A(inti){ a=i; b=0; } A(inti,intj){ a=i; b=j; } voiddisplay(){ cout<<"a="<<a<<"b="<<b; }private: inta; intb;};classB :publicA{ public: B(){ c=0; } B(inti):A(i){ c=0; } B(inti,intj):A(i,j){ c=0; } B(inti,intj,intk):A(i,j){ c=k; } voiddisplay1(){ display(); cout<<"c="<<c<<endl; } private: intc;};intmain(){ Bb1; Bb2(1); Bb3(1,3); Bb4(1,3,5); b1.display1(); b2.display1(); b3.display1(); b4.display1(); return0;}运行结果如下:a=0b=0c=0a=1b=0c=0a=1b=3c=0a=1b=3c=5有以下程序,请完成下面工作:阅读程序,写出运行时输出的结果。然后上机运行,验证结果是否正确。分析程序执行过程,尤其是调用构造函数和析构函数的过程。#include<iostream>usingnamespacestd;classA{ public: A(){ cout<<"constructingA"<<endl; }~A(){ cout<<"destructingA"<<endl; }};classB :publicA{ public: B(){ cout<<"constructingB"<<endl; }~B(){ cout<<"destructingB"<<endl; }};classC :publicB{ public: C(){ cout<<"constructingC"<<endl; }~C(){ cout<<"destructingC"<<endl; }};intmain(){ Cc1; return0;}分别声明Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)类。要求:在两个基类中都包含姓名、年龄、性别、地址和电话等数据成员。在Teacher类中还包含数据成员title(称职),在Cadre类中好包含数据成员post(职务)。在Teacher_Cadre类中还包含数据成员wages(工资)。对两个基类中的姓名、年龄、性别、地址和电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。在类体中声明成员函数,在类外定义成员函数。在派生类Tracher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址和电话,然后再用cout语句输出职务与工资。#include<string>#include<iostream>usingnamespacestd;classTeacher{ public: Teacher(stringnam,inta,chars,stringtit,stringad,stringt); voiddisplay(); protected: stringname; intage; charsex; stringtitle; stringaddr; stringtel;};Teacher::Teacher(stringnam,inta,chars,stringtit,stringad,stringt): name(nam),age(a),sex(s),title(tit),addr(ad),tel(t){}voidTeacher::display(){ cout<<"name:"<<name<<endl; cout<<"age"<<age<<endl; cout<<"sex:"<<sex<<endl; cout<<"title:"<<title<<endl; cout<<"address:"<<addr<<endl; cout<<"tel:"<<tel<<endl;}classCadre{ public: Cadre(stringnam,inta,chars,stringp,stringad,string t); voiddisplay(); protected: stringname; intage; charsex; stringpost; stringaddr; stringtel;};Cadre::Cadre(stringnam,inta,chars,stringp,stringad,stringt): name(nam),age(a),sex(s),post(p),addr(ad),tel(t){}voidCadre::display(){ cout<<"name:"<<name<<endl; cout<<"age:"<<age<<endl; cout<<"sex:"<<sex<<endl; cout<<"post:"<<post<<endl; cout<<"a

温馨提示

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

评论

0/150

提交评论