最新c++面向对象课后答案第8章

合集下载

C面向对象程序设计课后习题答案1~14章

C面向对象程序设计课后习题答案1~14章
c1=getchar(); //将输入的第一个字符赋给c1
c2=getchar(); //将输入的第二个字符赋给c2
cout<<"用putchar函数输出结果为:";
putchar(c1);
putchar(44);
putchar(c2);
cout<<endl;
cout<<"用cout语句输出结果为:";
putchar(c2);
cout<<endl;
cout<<"用cout语句输出结果为:";
cout<<c1<<c2<<endl;
return 0;
}
3-4-1、#include <iostream>
using namespace std;
int main ( )
{char c1,c2;
cout<<"请输入两个字符c1,c2:";
cout<<"please enter score of student:";
cin>>score;
while (score>100||score<0)
{cout<<"data error,enter data again.";
cin>>score;
}
switch(int(score/10))
{case 10:
else m=y;
if (z<m) m=z;
return(m);
}
1-8、#include <iostream>

c语言-C程序设计(第四版)谭浩强_课后习题答案第8章

c语言-C程序设计(第四版)谭浩强_课后习题答案第8章

C程序设计(第四版)谭浩强_课后习题答案第8章第8章善于利用指针2208.1指针是什么2208.2指针变量2228.2.1使用指针变量的例子2228.2.2怎样定义指针变量2238.2.3怎样引用指针变量2248.2.4指针变量作为函数参数2268.3通过指针引用数组2308.3.1数组元素的指针2308.3.2在引用数组元素时指针的运算2318.3.3通过指针引用数组元素2338.3.4用数组名作函数参数2378.3.5通过指针引用多维数组2458.4通过指针引用字符串2558.4.1字符串的引用方式2558.4.2字符指针作函数参数2598.4.3使用字符指针变量和字符数组的比较2638.5指向函数的指针2668.5.1什么是函数指针2668.5.2用函数指针变量调用函数2668.5.3怎样定义和使用指向函数的指针变量2688.5.4用指向函数的指针作函数参数2708.6返回指针值的函数2748.7指针数组和多重指针2778.7.1什么是指针数组2778.7.2指向指针数据的指针2808.7.3指针数组作main函数的形参2828.8动态内存分配与指向它的指针变量2858.8.1什么是内存的动态分配2858.8.2怎样建立内存的动态分配2858.8.3void指针类型2878.9有关指针的小结288习题2918-1#include <stdio.h>int main(){ void swap(int *p1,int *p2);int n1,n2,n3;int *p1,*p2,*p3;printf("input three integer n1,n2,n3:");scanf("%d,%d,%d",&n1,&n2,&n3);p1=&n1;p2=&n2;p3=&n3;if(n1>n2) swap(p1,p2);if(n1>n3) swap(p1,p3);if(n2>n3) swap(p2,p3);printf("Now,the order is:%d,%d,%d\n",n1,n2,n3); return 0;}void swap(int *p1,int *p2){int p;p=*p1; *p1=*p2; *p2=p;}#include <stdio.h>#include <string.h>int main(){void swap(char *,char *);char str1[20],str2[20],str3[20];printf("input three line:\n");gets(str1);gets(str2);gets(str3);if(strcmp(str1,str2)>0) swap(str1,str2);if(strcmp(str1,str3)>0) swap(str1,str3);if(strcmp(str2,str3)>0) swap(str2,str3);printf("Now,the order is:\n");printf("%s\n%s\n%s\n",str1,str2,str3);return 0;}void swap(char *p1,char *p2){char p[20];strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);}8-3#include <stdio.h>int main(){ void input(int *);void max_min_value(int *);void output(int *);int number[10];input(number);max_min_value(number);output(number);return 0;}void input(int *number){int i;printf("input 10 numbers:");for (i=0;i<10;i++)scanf("%d",&number[i]);}void max_min_value(int *number){ int *max,*min,*p,temp;max=min=number;for (p=number+1;p<number+10;p++)if (*p>*max) max=p;else if (*p<*min) min=p;temp=number[0];number[0]=*min;*min=temp;if(max==number) max=min;temp=number[9];number[9]=*max;*max=temp; }void output(int *number){int *p;printf("Now,they are: ");for (p=number;p<number+10;p++)printf("%d ",*p);printf("\n");}8-4#include <stdio.h>int main(){void move(int [20],int,int);int number[20],n,m,i;printf("how many numbers?");scanf("%d",&n);printf("input %d numbers:\n",n);for (i=0;i<n;i++)scanf("%d",&number[i]);printf("how many place you want move?"); scanf("%d",&m);move(number,n,m);printf("Now,they are:\n");for (i=0;i<n;i++)printf("%d ",number[i]);printf("\n");return 0;}void move(int array[20],int n,int m){int *p,array_end;array_end=*(array+n-1);for (p=array+n-1;p>array;p--)*p=*(p-1);*array=array_end;m--;if (m>0) move(array,n,m);}8-5#include <stdio.h>int main(){int i,k,m,n,num[50],*p;printf("\ninput number of person: n="); scanf("%d",&n);p=num;for (i=0;i<n;i++)*(p+i)=i+1;i=0;k=0;m=0;while (m<n-1){if (*(p+i)!=0) k++;if (k==3){*(p+i)=0;k=0;m++;}i++;if (i==n) i=0;}while(*p==0) p++;printf("The last one is NO.%d\n",*p); return 0;}8-6#include <stdio.h>int main(){int length(char *p);int len;char str[20];printf("input string: ");scanf("%s",str);len=length(str);printf("The length of string is %d.\n",len); return 0;}int length(char *p){int n;n=0;while (*p!='\0'){n++;p++;}return(n);}8-7#include <stdio.h>#include <string.h>int main(){void copystr(char *,char *,int);int m;char str1[20],str2[20];printf("input string:");gets(str1);printf("which character that begin to copy?"); scanf("%d",&m);if (strlen(str1)<m)printf("input error!");else{copystr(str1,str2,m);printf("result:%s\n",str2);}return 0;}void copystr(char *p1,char *p2,int m){int n;n=0;while (n<m-1){n++;p1++;}while (*p1!='\0'){*p2=*p1;p1++;p2++;}*p2='\0';}8-8#include <stdio.h>int main(){int upper=0,lower=0,digit=0,space=0,other=0,i=0; char *p,s[20];printf("input string: ");while ((s[i]=getchar())!='\n') i++;p=&s[0];while (*p!='\n'){if (('A'<=*p) && (*p<='Z'))++upper;else if (('a'<=*p) && (*p<='z'))++lower;else if (*p==' ')++space;else if ((*p<='9') && (*p>='0'))++digit;else++other;p++;}printf("upper case:%d lower case:%d",upper,lower);printf(" space:%d digit:%d other:%d\n",space,digit,other); return 0;}8-9#include <stdio.h>int main(){void move(int *pointer);int a[3][3],*p,i;printf("input matrix:\n");for (i=0;i<3;i++)scanf("%d %d %d",&a[i][0],&a[i][1],&a[i][2]);p=&a[0][0];move(p);printf("Now,matrix:\n");for (i=0;i<3;i++)printf("%d %d %d\n",a[i][0],a[i][1],a[i][2]);return 0;}void move(int *pointer){int i,j,t;for (i=0;i<3;i++)for (j=i;j<3;j++){t=*(pointer+3*i+j);*(pointer+3*i+j)=*(pointer+3*j+i);*(pointer+3*j+i)=t;}}8-10-1#include <stdio.h>int main(){void change(int *p);int a[5][5],*p,i,j;printf("input matrix:\n");for (i=0;i<5;i++)for (j=0;j<5;j++)scanf("%d",&a[i][j]);p=&a[0][0];change(p);printf("Now,matrix:\n");for (i=0;i<5;i++){for (j=0;j<5;j++)printf("%d ",a[i][j]);printf("\n");}return 0;}void change(int *p){int i,j,temp;int *pmax,*pmin;pmax=p;pmin=p;for (i=0;i<5;i++)for (j=i;j<5;j++){if (*pmax<*(p+5*i+j)) pmax=p+5*i+j;if (*pmin>*(p+5*i+j)) pmin=p+5*i+j;}temp=*(p+12);*(p+12)=*pmax;*pmax=temp;temp=*p;*p=*pmin;*pmin=temp;pmin=p+1;for (i=0;i<5;i++)for (j=0;j<5;j++)if (((p+5*i+j)!=p) && (*pmin>*(p+5*i+j))) pmin=p+5*i+j;temp=*pmin;*pmin=*(p+4);*(p+4)=temp;pmin=p+1;for (i=0;i<5;i++)for (j=0;j<5;j++)if (((p+5*i+j)!=(p+4))&&((p+5*i+j)!=p)&&(*pmin>*(p+5*i+j)))pmin=p+5*i+j;temp=*pmin;*pmin=*(p+20);*(p+20)=temp;pmin=p+1;for (i=0;i<5;i++)for (j=0;j<5;j++)if (((p+5*i+j)!=p) && ((p+5*i+j)!=(p+4)) && ((p+5*i+j)!=(p+20)) && (*pmin>*(p+5*i+j)))pmin=p+5*i+j;temp=*pmin;*pmin=*(p+24);*(p+24)=temp;}8-10-2#include <stdio.h>int main(){void change(int *p);int a[5][5],*p,i,j;printf("input matrix:\n");for (i=0;i<5;i++)for (j=0;j<5;j++)scanf("%d",&a[i][j]);p=&a[0][0];change(p);printf("Now,matrix:\n");for (i=0;i<5;i++){for (j=0;j<5;j++)printf("%d ",a[i][j]);printf("\n");}return 0;}void change(int *p) //交换函数{int i,j,temp;int *pmax,*pmin;pmax=p;pmin=p;for (i=0;i<5;i++) //找最大值和最小值的地址,并赋给pmax,pmin for (j=i;j<5;j++){if (*pmax<*(p+5*i+j)) pmax=p+5*i+j;if (*pmin>*(p+5*i+j)) pmin=p+5*i+j;}temp=*(p+12); //将最大值与中心元素互换*(p+12)=*pmax;*pmax=temp;temp=*p; //将最小值与左上角元素互换*p=*pmin;*pmin=temp;pmin=p+1;//将a[0][1]的地址赋给pmin,从该位置开始找最小的元素for (i=0;i<5;i++) //找第二最小值的地址赋给pminfor (j=0;j<5;j++){if(i==0 && j==0) continue;if (*pmin > *(p+5*i+j)) pmin=p+5*i+j;}temp=*pmin; //将第二最小值与右上角元素互换*pmin=*(p+4);*(p+4)=temp;pmin=p+1;for (i=0;i<5;i++) //找第三最小值的地址赋给pminfor (j=0;j<5;j++){if((i==0 && j==0) ||(i==0 && j==4)) continue;if(*pmin>*(p+5*i+j)) pmin=p+5*i+j;}temp=*pmin; // 将第三最小值与左下角元素互换*pmin=*(p+20);*(p+20)=temp;pmin=p+1;for (i=0;i<5;i++) // 找第四最小值的地址赋给pminfor (j=0;j<5;j++){if ((i==0 && j==0) ||(i==0 && j==4)||(i==4 && j==0)) continue;if (*pmin>*(p+5*i+j)) pmin=p+5*i+j;}temp=*pmin; //将第四最小值与右下角元素互换*pmin=*(p+24);*(p+24)=temp;}8-11-1#include <stdio.h>#include <string.h>int main(){void sort(char s[][6]);int i;char str[10][6];printf("input 10 strings:\n");for (i=0;i<10;i++)scanf("%s",str[i]);sort(str);printf("Now,the sequence is:\n"); for (i=0;i<10;i++)printf("%s\n",str[i]);return 0;}void sort(char s[10][6]){int i,j;char *p,temp[10];p=temp;for (i=0;i<9;i++)for (j=0;j<9-i;j++)if (strcmp(s[j],s[j+1])>0){strcpy(p,s[j]);strcpy(s[j],s[+j+1]);strcpy(s[j+1],p);}}8-11-2#include <stdio.h>#include <string.h>int main(){void sort(char (*p)[6]);int i;char str[10][6];char (*p)[6];printf("input 10 strings:\n");for (i=0;i<10;i++)scanf("%s",str[i]);p=str;sort(p);printf("Now,the sequence is:\n"); for (i=0;i<10;i++)printf("%s\n",str[i]);return 0;}void sort(char (*s)[6]){int i,j;char temp[6],*t=temp;for (i=0;i<9;i++)for (j=0;j<9-i;j++)if (strcmp(s[j],s[j+1])>0){strcpy(t,s[j]);strcpy(s[j],s[+j+1]);strcpy(s[j+1],t);}}8-12#include <stdio.h>#include <string.h>int main(){void sort(char *[]);int i;char *p[10],str[10][20];for (i=0;i<10;i++)p[i]=str[i];printf("input 10 strings:\n");for (i=0;i<10;i++)scanf("%s",p[i]);sort(p);printf("Now,the sequence is:\n"); for (i=0;i<10;i++)printf("%s\n",p[i]);return 0;}void sort(char *s[]){int i,j;char *temp;for (i=0;i<9;i++)for (j=0;j<9-i;j++)if (strcmp(*(s+j),*(s+j+1))>0){temp=*(s+j);*(s+j)=*(s+j+1);*(s+j+1)=temp;}}8-13#include<stdio.h>#include<math.h>int main(){float integral(float(*)(float),float,float,int);//对integarl函数的声明float fsin(float); //对fsin函数的声明float fcos(float); //对fcos函数的声明float fexp(float); //对fexp函数的声明float a1,b1,a2,b2,a3,b3,c,(*p)(float);int n=20;printf("input a1,b1:");scanf("%f,%f",&a1,&b1);printf("input a2,b2:");scanf("%f,%f",&a2,&b2);printf("input a3,b3:");scanf("%f,%f",&a3,&b3);p=fsin;c=integral(p,a1,b1,n);printf("The integral of sin(x) is:%f\n",c);p=fcos;c=integral(p,a2,b2,n);printf("The integral of cos(x) is:%f\n",c);p=fexp;c=integral(p,a3,b3,n);printf("The integral of exp(x) is:%f\n",c);return 0;}float integral(float(*p)(float),float a,float b,int n){int i;float x,h,s;h=(b-a)/n;x=a;s=0;for(i=1;i<=n;i++){x=x+h;s=s+(*p)(x)*h;}return(s);}float fsin(float x){return sin(x);}float fcos(float x){return cos(x);}float fexp(float x){return exp(x);}8-14#include <stdio.h>int main(){void sort (char *p,int m);int i,n;char *p,num[20];printf("input n:");scanf("%d",&n);printf("please input these numbers:\n");for (i=0;i<n;i++)scanf("%d",&num[i]);p=&num[0];sort(p,n);printf("Now,the sequence is:\n");for (i=0;i<n;i++)printf("%d ",num[i]);printf("\n");return 0;}void sort (char *p,int m) // 将n个数逆序排列函数{int i;char temp, *p1,*p2;for (i=0;i<m/2;i++){p1=p+i;p2=p+(m-1-i);temp=*p1;*p1=*p2;*p2=temp;}}8-15#include <stdio.h>int main(){void avsco(float *,float *);void avcour1(char (*)[10],float *);void fali2(char course[5][10],int num[],float *pscore,float aver[4]); void good(char course[5][10],int num[4],float *pscore,float aver[4]); int i,j,*pnum,num[4];float score[4][5],aver[4],*pscore,*paver;char course[5][10],(*pcourse)[10];printf("input course:\n");pcourse=course;for (i=0;i<5;i++)scanf("%s",course[i]);printf("input NO. and scores:\n");printf("NO.");for (i=0;i<5;i++)printf(",%s",course[i]);printf("\n");pscore=&score[0][0];pnum=&num[0];for (i=0;i<4;i++){scanf("%d",pnum+i);for (j=0;j<5;j++)scanf("%f",pscore+5*i+j);}paver=&aver[0];printf("\n\n");avsco(pscore,paver); // 求出每个学生的平均成绩avcour1(pcourse,pscore); // 求出第一门课的平均成绩printf("\n\n");fali2(pcourse,pnum,pscore,paver); // 找出2门课不及格的学生printf("\n\n");good(pcourse,pnum,pscore,paver); // 找出成绩好的学生return 0;}void avsco(float *pscore,float *paver) // 求每个学生的平均成绩的函数{int i,j;float sum,average;for (i=0;i<4;i++){sum=0.0;for (j=0;j<5;j++)sum=sum+(*(pscore+5*i+j)); //累计每个学生的各科成绩average=sum/5; //计算平均成绩*(paver+i)=average;}}void avcour1(char (*pcourse)[10],float *pscore) // 求第一课程的平均成绩的函数{int i;float sum,average1;sum=0.0;for (i=0;i<4;i++)sum=sum+(*(pscore+5*i)); //累计每个学生的得分average1=sum/4; //计算平均成绩printf("course 1:%s average score:%7.2f\n",*pcourse,average1);}void fali2(char course[5][10],int num[],float *pscore,float aver[4])// 找两门以上课程不及格的学生的函数{int i,j,k,labe1;printf(" ==========Student who is fail in two courses======= \n"); printf("NO. ");for (i=0;i<5;i++)printf("%11s",course[i]);printf(" average\n");for (i=0;i<4;i++){labe1=0;for (j=0;j<5;j++)if (*(pscore+5*i+j)<60.0) labe1++;if (labe1>=2){printf("%d",num[i]);for (k=0;k<5;k++)printf("%11.2f",*(pscore+5*i+k));printf("%11.2f\n",aver[i]);}}}void good(char course[5][10],int num[4],float *pscore,float aver[4]) // 找成绩优秀学生(各门85以上或平均90分以上)的函数{int i,j,k,n;printf(" ======Students whose score is good======\n");printf("NO. ");for (i=0;i<5;i++)printf("%11s",course[i]);printf(" average\n");for (i=0;i<4;i++){n=0;for (j=0;j<5;j++)if (*(pscore+5*i+j)>85.0) n++;if ((n==5)||(aver[i]>=90)){printf("%d",num[i]);for (k=0;k<5;k++)printf("%11.2f",*(pscore+5*i+k));printf("%11.2f\n",aver[i]);}}}8-16#include <stdio.h>int main(){char str[50],*pstr;int i,j,k,m,e10,digit,ndigit,a[10],*pa;printf("input a string:\n");gets(str);pstr=&str[0]; /*字符指针pstr置于数组str 首地址*/pa=&a[0]; /*指针pa置于a数组首地址*/ndigit=0; /*ndigit代表有多少个整数*/i=0; /*代表字符串中的第几个字符*/j=0;while(*(pstr+i)!='\0'){if((*(pstr+i)>='0') && (*(pstr+i)<='9'))j++;else{if (j>0){digit=*(pstr+i-1)-48; /*将个数位赋予digit*/k=1;while (k<j) /*将含有两位以上数的其它位的数值累计于digit*/{e10=1;for (m=1;m<=k;m++)e10=e10*10; /*e10代表该位数所应乘的因子*/digit=digit+(*(pstr+i-1-k)-48)*e10; /*将该位数的数值\累加于digit*/k++; /*位数K自增*/}*pa=digit; /*将数值赋予数组a*/ndigit++;pa++; /*指针pa指向a数组下一元素*/j=0;}}i++;}if (j>0) /*以数字结尾字符串的最后一个数据*/ {digit=*(pstr+i-1)-48; /*将个数位赋予digit*/k=1;while (k<j) /* 将含有两位以上数的其它位的数值累加于digit*/{e10=1;for (m=1;m<=k;m++)e10=e10*10; /*e10代表位数所应乘的因子*/digit=digit+(*(pstr+i-1-k)-48)*e10; /*将该位数的数值累加于digit*/k++; /*位数K自增*/}*pa=digit; /*将数值赋予数组a*/ndigit++;j=0;}printf("There are %d numbers in this line, they are:\n",ndigit);j=0;pa=&a[0];for (j=0;j<ndigit;j++) /*打印数据*/printf("%d ",*(pa+j));printf("\n");return 0;}8-17#include<stdio.h>int main(){int strcmp(char *p1,char *p2);int m;char str1[20],str2[20],*p1,*p2;printf("input two strings:\n");scanf("%s",str1);scanf("%s",str2);p1=&str1[0];p2=&str2[0];m=strcmp(p1,p2);printf("result:%d,\n",m);return 0;}int strcmp(char *p1,char *p2) //两个字符串比较函数{int i;i=0;while(*(p1+i)==*(p2+i))if (*(p1+i++)=='\0') return(0); //相等时返回结果0return(*(p1+i)-*(p2+i)); //不等时返回结果为第一个不等字符ASCII码的差值}8-18#include <stdio.h>int main(){char *month_name[13]={"illegal month","January","February","March","April", "May","June","july","August","September","October", "November","December"};int n;printf("input month:\n");scanf("%d",&n);if ((n<=12) && (n>=1))printf("It is %s.\n",*(month_name+n));elseprintf("It is wrong.\n");return 0;}8-19-1#include <stdio.h>#define NEWSIZE 1000 //指定开辟存区的最大容量char newbuf[NEWSIZE]; //定义字符数组newbufchar *newp=newbuf; //定义指针变量newp,指向可存区的始端char *new(int n) //定义开辟存区的函数new,开辟存储区后返回指针{if (newp+n<=newbuf+NEWSIZE) // 开辟区未超过newbuf数组的大小{newp+=n; // newp指向存储区的末尾return(newp-n); // 返回一个指针,它指向存区的开始位置}elsereturn(NULL); // 当存区不够分配时,返回一个空指针}8-19-2#include <stdio.h>#define NEWSIZE 1000char newbuf[NEWSIZE];char *newp=newbuf;void free(char *p) //释放存区函数{if (p>=newbuf && p< newbuf + NEWSIZE)newp=p;}8-20#define LINEMAX 20 /*定义字符串的最大长度*/int main(){int i;char **p,*pstr[5],str[5][LINEMAX];for (i=0;i<5;i++)pstr[i]=str[i]; /*将第i个字符串的首地址赋予指针数组pstr 的第i个元素*/ printf("input 5 strings:\n");for (i=0;i<5;i++)scanf("%s",pstr[i]);p=pstr;sort(p);printf("strings sorted:\n");for (i=0;i<5;i++)printf("%s\n",pstr[i]);}sort(char **p) /*冒泡法对5个字符串排序函数*/{int i,j;char *temp;for (i=0;i<5;i++){for (j=i+1;j<5;j++){if (strcmp(*(p+i),*(p+j))>0) /*比较后交换字符串地址*/{temp=*(p+i);*(p+i)=*(p+j);*(p+j)=temp;}}}return 0;}8-21#include<stdio.h>int main(){void sort(int **p,int n);int i,n,data[20],**p,*pstr[20];printf("input n:\n");scanf("%d",&n);for (i=0;i<n;i++)pstr[i]=&data[i]; //将第i个整数的地址赋予指针数组pstr 的第i个元素printf("input %d integer numbers:",n);for (i=0;i<n;i++)scanf("%d",pstr[i]);p=pstr;sort(p,n);printf("Now,the sequence is:\n");for (i=0;i<n;i++)printf("%d ",*pstr[i]);printf("\n");return 0;}void sort(int **p,int n){int i,j,*temp;for (i=0;i<n-1;i++){for (j=i+1;j<n;j++){if (**(p+i)>**(p+j)) //比较后交换整数地址{temp=*(p+i);*(p+i)=*(p+j);*(p+j)=temp;}}}}。

C 面向对象程序设计第8章剖析

C  面向对象程序设计第8章剖析

• 好处: 程序员阅读代码时能够直接看到错误处理情况,确定是否实现 了正确的错误检查。
• 问题: 代码中受到错误处理的“污染”,使应用程序本身的代码更加 晦涩难懂,难以看出代码功能是否正确实现。这样就使代码的理解和
维护更加困难。
面向对象中的异常处理
面向对象中,程序员经常做的是一些工具(类的设计 与实现) 这些工具能够检测出错误,但往往不知道该如何处理 错误。错误的处理是由工具的使用者决定
异常的处理语法

throw 表达式;

说明:throw后的表达式表示抛出异常的类型,可以是一个 变量、常量或一个对象。throw语句在语法上与return语句 相似。

try-catch结构描述如下:
try { 被检查语句 } catch(异常类型声明) { 异常处理语句 }
try和catch后的“{ }”不 能省略,即使块中只有 1
条语句也不能省略。
2018/10/25
13
异常语法说明-1

1. try块可以包含任何C++语句,甚至可以包含整个 函数,但是try块必须包含能够抛出异常的语句。 2. try 块和catch块作为一个整体出现,且catch必须 紧跟在try块之后,中间不能插入其他语句。 3. try块可以单独出现,而无catch块,表示无论是否 有异常都不进行处理。 4.一个try-catch结构中,只能有一个try块,可以有 多个catch块,每个catch块的异常信息必须是不同的 数据类型,以使和不同异常信息匹配。
2 divide by zero It’s Over
catch (int) {cout << "divide by zero" << endl; } cout << "It’s Over" << endl; return 0; }

【免费下载】c面向对象程序设计课后习题答案谭浩强版

【免费下载】c面向对象程序设计课后习题答案谭浩强版

对全部高中资料试卷电气设备,在安装过程中以及安装结束后进行高中资料试卷调整试验;通电检查所有设备高中资料电试力卷保相护互装作置用调与试相技互术关,系电,力根通保据过护生管高产线中工敷资艺设料高技试中术卷资,配料不置试仅技卷可术要以是求解指,决机对吊组电顶在气层进设配行备置继进不电行规保空范护载高与中带资负料荷试下卷高问总中题体资,配料而置试且时卷可,调保需控障要试各在验类最;管大对路限设习度备题内进到来行位确调。保整在机使管组其路高在敷中正设资常过料工程试况中卷下,安与要全过加,度强并工看且作护尽下关可都于能可管地以路缩正高小常中故工资障作料高;试中对卷资于连料继接试电管卷保口破护处坏进理范行高围整中,核资或对料者定试对值卷某,弯些审扁异核度常与固高校定中对盒资图位料纸置试,.卷保编工护写况层复进防杂行腐设自跨备动接与处地装理线置,弯高尤曲中其半资要径料避标试免高卷错等调误,试高要方中求案资技,料术编试交写5、卷底重电保。要气护管设设装线备备置敷4高、调动设中电试作技资气高,术料课中并3中试、件资且包卷管中料拒含试路调试绝线验敷试卷动槽方设技作、案技术,管以术来架及避等系免多统不项启必方动要式方高,案中为;资解对料决整试高套卷中启突语动然文过停电程机气中。课高因件中此中资,管料电壁试力薄卷高、电中接气资口设料不备试严进卷等行保问调护题试装,工置合作调理并试利且技用进术管行,线过要敷关求设运电技行力术高保。中护线资装缆料置敷试做设卷到原技准则术确:指灵在导活分。。线对对盒于于处调差,试动当过保不程护同中装电高置压中高回资中路料资交试料叉卷试时技卷,术调应问试采题技用,术金作是属为指隔调发板试电进人机行员一隔,变开需压处要器理在组;事在同前发一掌生线握内槽图部内 纸故,资障强料时电、,回设需路备要须制进同造行时厂外切家部断出电习具源题高高电中中源资资,料料线试试缆卷卷敷试切设验除完报从毕告而,与采要相用进关高行技中检术资查资料和料试检,卷测并主处且要理了保。解护现装场置设。备高中资料试卷布置情况与有关高中资料试卷电气系统接线等情况,然后根据规范与规程规定,制定设备调试高中资料试卷方案。

C语言课后习题答案第八章解析

C语言课后习题答案第八章解析

作业八:函数程序设计答案{ double z; z 二x+y; return z; }(重要)3.以下正确的说法是A_°在C 语言中A)B) C) D)4.若调用一个函数,且此函数中没有return 语句,则正确的说法是D_。

没有返回值返回若干个系统默认值 能返回一个用户所希望的函数值 返回一个不确定的值(重要)5. 以下不正确的说法是B_° C 语言规定A) B)C) D)6. C 语言规定,简单变量做实参时,它和对应形参之间的数据传递方式是隹^A) B) C) D)7.以下程序有语法性错误,有关错误原因的正确说法是C_。

main()(一)选择丿 (30分) 1.以下正确的函数定义形式是 A) B)C) D) double double double double fun (int x, int fun (int x;int fun (int x, intfun(int x, y);y) y) y); 2.以下正确的函数形式是D_。

A) B) C) D) double fun (int x, int { z 二x+y; return z; } fun (int x, y){ int z: return z; } fun (x, y){ int x,y; double z;double fun (int x, inty) z 二x+y; return z; } y)实参和与其对应的形参各占用独立的存储单元 实参和与其对应的形参共占用一个存储单元 只有当实参和与其对应的形参同名时才共占用存储单元 形参是虚拟的,不占用存储单元该函数A) B) C) D)实参可以是常量、变量或表达式 形参可以是常量、变量或表达式 实参可以为任意类型形参应与其对应的实参类型一致 地址传递 单向值传递山实参传给形参,再山形参传回给实参 由用户指定传递方式int G=5, k;void Prt.char ();k=Prt_char(G);}A)语句void prt.char ();有错,它是函数调用语句,不能用void 说明B)变量名不能使用大写字母C)函数说明和函数调用语句之间有矛盾D)函数名不能使用下划线8.C语言允许函数值类型缺省定义,此时该函数值隐含的类型是A)float 型B)int 型C)long 型D)double 型9.C语言规定,函数返回值的类型是Lil D_oA)return语句中的表达式类型所决定B)调用该函数时的主调函数类型所决定C)调用该函数时系统临时决定D)在定义该函数时所指定的函数类型所决定10.下面函数调用语句含有实参的个数为」func((expl,exp2), (exp3,exp4,exp5));A) 1 B) 2 C) 4 D) 5 (重要)11.以下程序的功能是计算函数F(x, y, z)二(x+y)/(x-y) + (z+y)/(z-y)的值,请选择填空。

第8-12章C++面向对象程序设计 作业参考答案

第8-12章C++面向对象程序设计 作业参考答案
cout <<"ready to listen carefully"<<endl;
}
void record()
{
cout <<"CD is recording"<<endl;
cout <<"working......"<<endl;
}
void stop()
{
cout <<"CD has been stopped"<<endl;
9_4
#include<iostream>
using namespace std;
template <class T>
void data_sort(T *data,int num)
{
for(int i=0;i<num-1;i++)
{
int k=i;
for(int j=i+1;j<num;j++)
{
if(data[k]>data[j])
deriver(int a);
int f(int a)
{
return 3*a;
}
private:
int value;
};
deriver::deriver(int a)
{
value=a;
}
void main()
{
e *p;
deriver obj(8);
p=&obj;
cout <<p->f(5)<<endl;

面向对象课后题答案

面向对象课后题答案
C. 使用内联函数,可以加快程序执行的速度,但会增加程序代码的大小
D. 使用内联函数,可以减小程序代码大小,但使程序执行的速度减慢
【结果分析】
内联函数主要是解决程序的运行效率问题。在程序编译时,编译系统将程序中出现内联函数调用的地方用函数体进行替换,进而减少了程序运行的时间,但会增加程序代码的大小。它是以空间换取时间,因此内联函数适用于功能不太复杂,但要求被频繁调用的函数。
B. 对象实际是功能相对独立的一段程序
C. 各个对象间的数据可以共享是对象的一大优点
D. 在面向对象的程序中,对象之间只能通过消息相互通信
【结果分析】
对象是计算机内存中的一块区域。在对象中,不但存有数据,而且存有代码,使得每个对象在功能上相互之间保持相对独立。对象之间存在各种联系,但它们之间只能通过消息进行通信。
C. C++对C语言进行了一些改进 D. C++和C语言都是面向对象的
【结果分析】
C语言是面向过程的。C++语言是一种经过改进的更为优化的C语言,是一种混合型语言,既面向过程也面向对象。
(6) 面向对象的程序设计将数据结构与( A )放在一起,作为一个相互依存、不可分割的整体来处理。
A. 算法 B. 信息 C. 数据隐藏 D. 数据抽象
四、 判断题
(1) 在高级程序设计语言中,一般用类来实现对象,类是具有相同属性和行为的一组对象的集合,它是创建对象的模板。( √ )
(2) C++语言只支持面向对象技术的抽象性、封装性、继承性等特性,而不支持多态性。( × )
【结果分析】
C++语言不仅支持面向对象技术的抽象性、封装性、继承性等特性,而且支持多态性。

C++程序设计基础课后答案 第八章复习课程

C++程序设计基础课后答案  第八章复习课程

C++程序设计基础课后答案第八章8.1 阅读下列程序,写出执行结果1. #include <iostream.h>class Bclass{ public:Bclass( int i, int j ) { x = i; y = j; }virtual int fun() { return 0 ; }protected:int x, y ;};class Iclass:public Bclass{ public :Iclass(int i, int j, int k):Bclass(i, j) { z = k; }int fun() { return ( x + y + z ) / 3; }private :int z ;};void main(){ Iclass obj( 2, 4, 10 );Bclass p1 = obj;cout << p1.fun() << endl;Bclass & p2 = obj ;cout << p2.fun() << endl;cout << p2.Bclass :: fun() << endl;Bclass *p3 = &obj;cout << p3 -> fun() << endl;}2. #include <iostream.h>class Base{ public:virtual void getxy( int i,int j = 0 ) { x = i; y = j; }virtual void fun() = 0 ;protected:int x , y;};class A: public Base{ public:void fun(){ cout<<"x = "<<x<<'\t'<<"y = x * x = "<<x*x<<endl; } };class B:public Base{ public:void fun(){ cout << "x = " << x << '\t' << "y = " << y << endl;cout << "y = x / y = " << x / y << endl;}};void main(){ Base * pb;A obj1;B obj2;pb = &obj1;pb -> getxy( 10 );pb -> fun();pb = &obj2;pb -> getxy( 100, 20 );pb -> fun();}8.2 思考题1.在C++中,使用类体系依靠什么机制实现程序运行时的多态?2.如果一个基类的虚函数被声明为私有成员函数,会有语法错误吗?可以在应用类体系时实现动态联编吗?请你验证一下。

C++面向对象程序设计答案1~8章_谭浩强

C++面向对象程序设计答案1~8章_谭浩强
return 0;
}
void change(int ,int )
{
int r1,r2,temp;
temp=r1;
r1=r2;
r2=temp;
}
13:
#include <iostream>
using namespace std;
int main()
{void sort(int &,int &,int &);
}
int main()
{Time t1;
t1.set_time();
t1.show_time();
return 0;
}
2:
#include <iostream>
using namespace std;
class Time
{public:
void set_time(void)
{cin>>hour;
cin>>minute;
int sec;
};
void Time::set_time() //在main函数之前定义
{
cin>>hour;
cin>>minute;
cin>>sec;
}
void Time::show_time() //在main函数之前定义
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
cout<<endl<<endl;
}
void sort(float a[])

C++面向对象程序设计教程课后习题答案

C++面向对象程序设计教程课后习题答案

// 定义变量
for (i = 0; i < n- 1; i++)
for (j = i + 1; j < n; j++)
if (a[i] > a[j])
{
// a[i]比a[j]更大
tem = a[i]; a[i] = a[j]; a[j] = tem;
}
}
……
a
9
*5.编一个程序,用同一个函数名对n个数据进行从小到大排序,数据类型可
system("PAUSE"); // 调用库函数system( ),输出提示信息
return 0;
// 返回a值, 返回操作系统
7
}
4.用变量的引用作函数形参,实现交换两个整型变量,要求编写测试程序。
参考程序:
#include <iostream>
// 编译预处理命令
using namespace std;
double c[] = {1.2, 3.1, 2.6, 5.8, 6.8, 9.8, 0.8, 6.2};// 定义c
int i, n = 8;
// 定义变量
Sort(a, n); cout << "a:"; for (i = 0; i < n; i++)
cout << a[i] << " "; cout << endl;
m = m > z ? m : z; // m,z的最大值
return m;
// 返回x,y,z的最大值
}
int main()
// 主函数main()

C++面向对象程序设计课后习题答案1~14章

C++面向对象程序设计课后习题答案1~14章

第一章1-5、#include <iostream> using namespace std;int main(){cout<<"This"<<"is";cout<<"a"<<"C++";cout<<"program.";return 0;}1-6、#include <iostream> using namespace std;int main(){int a,b,c;a=10;b=23;c=a+b;cout<<"a+b=";cout<<c;cout<<endl;return 0;}1-7#include <iostream> using namespace std;int main(){int a,b,c;int f(int x,int y,int z); cin>>a>>b>>c;c=f(a,b,c);cout<<c<<endl;return 0;}int f(int x,int y,int z){int m;if (x<y) m=x;else m=y;if (z<m) m=z;return(m);} 1-8、#include <iostream>using namespace std;int main(){int a,b,c;cin>>a>>b;c=a+b;cout<<"a+b="<<a+b<<endl;return 0;}1-9、#include <iostream>using namespace std;int main(){int a,b,c;int add(int x,int y);cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl;return 0;}int add(int x,int y){int z;z=x+y;return(z);}第二章2-3、#include <iostream>using namespace std;int main(){char c1='a',c2='b',c3='c',c4='\101',c5='\116'; cout<<c1<<c2<<c3<<'\n';cout<<"\t\b"<<c4<<'\t'<<c5<<'\n';return 0;}2-4、#include <iostream>using namespace std;int main(){char c1='C',c2='+',c3='+';cout<<"I say: \""<<c1<<c2<<c3<<'\"';cout<<"\t\t"<<"He says: \"C++ is veryinteresting!\""<< '\n';return 0;}2-7、#include <iostream>using namespace std;int main(){int i,j,m,n;i=8;j=10;m=++i+j++;n=(++i)+(++j)+m;cout<<i<<'\t'<<j<<'\t'<<m<<'\t'<<n<<endl; return 0;}2-8、#include <iostream>using namespace std;int main(){char c1='C', c2='h', c3='i', c4='n', c5='a';c1+=4;c2+=4;c3+=4;c4+=4;c5+=4;cout<<"passwordis:"<<c1<<c2<<c3<<c4<<c5<<endl;return 0;}第三章3-2、#include <iostream>#include <iomanip>using namespace std;int main ( ){float h,r,l,s,sq,vq,vz;const float pi=3.1415926;cout<<"please enter r,h:";cin>>r>>h;l=2*pi*r;s=r*r*pi;sq=4*pi*r*r;vq=3.0/4.0*pi*r*r*r;vz=pi*r*r*h;cout<<setiosflags(ios::fixed)<<setiosflags(ios:: right)<<setprecision(2);cout<<"l= "<<setw(10)<<l<<endl;cout<<"s= "<<setw(10)<<s<<endl;cout<<"sq="<<setw(10)<<sq<<endl;cout<<"vq="<<setw(10)<<vq<<endl;cout<<"vz="<<setw(10)<<vz<<endl;return 0;}3-3、#include <iostream>using namespace std;int main (){float c,f;cout<<"请输入一个华氏温度:";cin>>f;c=(5.0/9.0)*(f-32); //注意5和9要用实型表示,否则5/9值为0cout<<"摄氏温度为:"<<c<<endl;return 0;};3-4、#include <iostream>using namespace std;int main ( ){char c1,c2;cout<<"请输入两个字符c1,c2:";c1=getchar(); //将输入的第一个字符赋给c1c2=getchar(); //将输入的第二个字符赋给c2cout<<"用putchar函数输出结果为:"; putchar(c1);putchar(c2);cout<<endl;cout<<"用cout语句输出结果为:";cout<<c1<<c2<<endl;return 0;}3-4-1、#include <iostream>using namespace std;int main ( ){char c1,c2;cout<<"请输入两个字符c1,c2:";c1=getchar(); //将输入的第一个字符赋给c1c2=getchar(); //将输入的第二个字符赋给c2cout<<"用putchar函数输出结果为:"; putchar(c1);putchar(44);putchar(c2);cout<<endl;cout<<"用cout语句输出结果为:";cout<<c1<<","<<c2<<endl;return 0;}3-5、#include <iostream>using namespace std;int main ( ){char c1,c2;int i1,i2; //定义为整型cout<<"请输入两个整数i1,i2:";cin>>i1>>i2;c1=i1;c2=i2;cout<<"按字符输出结果为:"<<c1<<" , "<<c2<<endl;return 0;}3-8、#include <iostream>using namespace std;int main ( ){ int a=3,b=4,c=5,x,y;cout<<(a+b>c && b==c)<<endl;cout<<(a||b+c && b-c)<<endl;cout<<(!(a>b) && !c||1)<<endl;cout<<(!(x=a) && (y=b) && 0)<<endl;cout<<(!(a+b)+c-1 && b+c/2)<<endl; return 0;}3-9-1、#include <iostream>using namespace std;int main ( ){int a,b,c;cout<<"please enter three integer numbers:";cin>>a>>b>>c;if(a<b)if(b<c)cout<<"max="<<c;elsecout<<"max="<<b;else if (a<c)cout<<"max="<<c;elsecout<<"max="<<a;cout<<endl;return 0;}3-9-2、#include <iostream>using namespace std;int main ( ){int a,b,c,temp,max ;cout<<"please enter three integer numbers:";cin>>a>>b>>c;temp=(a>b)?a:b; /* 将a和b中的大者存入temp中*/max=(temp>c)?temp:c; /* 将a和b中的大者与c比较,最大者存入max */cout<<"max="<<max<<endl;return 0;}3-10、#include <iostream>using namespace std;int main ( ){int x,y;cout<<"enter x:";cin>>x;if (x<1){y=x;cout<<"x="<<x<<", y=x="<<y;}else if (x<10) // 1≤x<10{y=2*x-1;cout<<"x="<<x<<", y=2*x-1="<<y;}else// x≥10{y=3*x-11;cout<<"x="<<x<<",y=3*x-11="<<y;}cout<<endl;return 0;}3-11、#include <iostream>using namespace std;int main (){float score;char grade;cout<<"please enter score of student:"; cin>>score;while (score>100||score<0){cout<<"data error,enter data again.";cin>>score;}switch(int(score/10)){case 10:case 9: grade='A';break;case 8: grade='B';break;case 7: grade='C';break;case 6: grade='D';break;default:grade='E';}cout<<"score is "<<score<<", grade is "<<grade<<endl;return 0;}3-12、#include <iostream>using namespace std;int main (){long int num;intindiv,ten,hundred,thousand,ten_thousand,pla ce;/*分别代表个位,十位,百位,千位,万位和位数*/cout<<"enter an integer(0~99999):"; cin>>num;if (num>9999)place=5;else if (num>999)place=4;else if (num>99)place=3;else if (num>9)place=2;else place=1;cout<<"place="<<place<<endl;//计算各位数字ten_thousand=num/10000;thousand=(int)(num-ten_thousand*10000)/1 000;hundred=(int)(num-ten_thousand*10000-tho usand*1000)/100;ten=(int)(num-ten_thousand*10000-thousan d*1000-hundred*100)/10;indiv=(int)(num-ten_thousand*10000-thousa nd*1000-hundred*100-ten*10);cout<<"original order:";switch(place){case5:cout<<ten_thousand<<","<<thousand<<","< <hundred<<","<<ten<<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<thousand<<ten _thousand<<endl;break;case4:cout<<thousand<<","<<hundred<<","<<ten <<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<thousand<<en dl;break;case3:cout<<hundred<<","<<ten<<","<<indiv<<en dl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<endl;break;case 2:cout<<ten<<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<endl;break;case 1:cout<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<endl;break;}return 0;}3-13-1、#include <iostream>using namespace std;int main (){ long i; //i为利润floatbonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*0.1; //利润为10万元时的奖金bon2=bon1+100000*0.075; //利润为20万元时的奖金bon4=bon2+100000*0.05; //利润为40万元时的奖金bon6=bon4+100000*0.03; //利润为60万元时的奖金bon10=bon6+400000*0.015; //利润为100万元时的奖金cout<<"enter i:";cin>>i;if (i<=100000)bonus=i*0.1;//利润在10万元以内按10%提成奖金else if (i<=200000)bonus=bon1+(i-100000)*0.075; //利润在10万元至20万时的奖金else if (i<=400000)bonus=bon2+(i-200000)*0.05; //利润在20万元至40万时的奖金else if (i<=600000)bonus=bon4+(i-400000)*0.03; //利润在40万元至60万时的奖金else if (i<=1000000)bonus=bon6+(i-600000)*0.015; //利润在60万元至100万时的奖金elsebonus=bon10+(i-1000000)*0.01; //利润在100万元以上时的奖金cout<<"bonus="<<bonus<<endl;return 0;}3-13-2、#include <iostream>using namespace std;int main (){long i;float bonus,bon1,bon2,bon4,bon6,bon10; int c;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;cout<<"enter i:";cin>>i;c=i/100000;if (c>10) c=10;switch(c){case 0: bonus=i*0.1; break;case 1: bonus=bon1+(i-100000)*0.075; break;case 2:case 3: bonus=bon2+(i-200000)*0.05;break;case 4:case 5: bonus=bon4+(i-400000)*0.03;break;case 6:case 7:case 8:case 9: bonus=bon6+(i-600000)*0.015; break;case 10: bonus=bon10+(i-1000000)*0.01;}cout<<"bonus="<<bonus<<endl;return 0;}3-14、#include <iostream>using namespace std;int main (){int t,a,b,c,d;cout<<"enter four numbers:";cin>>a>>b>>c>>d;cout<<"a="<<a<<", b="<<b<<", c="<<c<<",d="<<d<<endl;if (a>b){t=a;a=b;b=t;}if (a>c){t=a; a=c; c=t;}if (a>d){t=a; a=d; d=t;}if (b>c){t=b; b=c; c=t;}if (b>d){t=b; b=d; d=t;}if (c>d){t=c; c=d; d=t;}cout<<"the sorted sequence:"<<endl;cout<<a<<", "<<b<<", "<<c<<", "<<d<<endl; return 0;}3-15、#include <iostream>using namespace std;int main (){int p,r,n,m,temp;cout<<"please enter two positive integer numbers n,m:";cin>>n>>m;if (n<m){temp=n;n=m;m=temp; //把大数放在n中, 小数放在m中}p=n*m; //先将n和m的乘积保存在p中, 以便求最小公倍数时用while (m!=0) //求n和m 的最大公约数{r=n%m;n=m;m=r;}cout<<"HCF="<<n<<endl;cout<<"LCD="<<p/n<<endl; // p是原来两个整数的乘积return 0;}3-16、#include <iostream>using namespace std;int main (){char c;int letters=0,space=0,digit=0,other=0;cout<<"enter one line::"<<endl;while((c=getchar())!='\n'){if (c>='a' && c<='z'||c>='A' && c<='Z')letters++;else if (c==' ')space++;else if (c>='0' && c<='9')digit++;elseother++;}cout<<"letter:"<<letters<<", space:"<<space<<", digit:"<<digit<<", other:"<<other<<endl;return 0;}3-17、#include <iostream>using namespace std;int main (){int a,n,i=1,sn=0,tn=0;cout<<"a,n=:";cin>>a>>n;while (i<=n){tn=tn+a; //赋值后的tn为i个a 组成数的值sn=sn+tn; //赋值后的sn为多项式前i项之和a=a*10;++i;}cout<<"a+aa+aaa+...="<<sn<<endl;return 0;}3-18、#include <iostream>using namespace std;int main (){float s=0,t=1;int n;for (n=1;n<=20;n++){t=t*n; // 求n!s=s+t; // 将各项累加}cout<<"1!+2!+...+20!="<<s<<endl;return 0;}3-19、#include <iostream>using namespace std;int main (){int i,j,k,n;cout<<"narcissus numbers are:"<<endl;for (n=100;n<1000;n++){i=n/100;j=n/10-i*10;k=n%10;if (n == i*i*i + j*j*j + k*k*k)cout<<n<<" ";}cout<<endl;return 0;}3-20-1、#include <iostream>using namespace std;int main(){const int m=1000; // 定义寻找范围int k1,k2,k3,k4,k5,k6,k7,k8,k9,k10;int i,a,n,s;for (a=2;a<=m;a++) // a是2~1000之间的整数,检查它是否为完数{n=0; // n用来累计a的因子的个数s=a; // s用来存放尚未求出的因子之和,开始时等于afor (i=1;i<a;i++) // 检查i是否为a 的因子if (a%i==0) // 如果i是a的因子{n++; // n加1,表示新找到一个因子s=s-i; // s减去已找到的因子,s的新值是尚未求出的因子之和switch(n) // 将找到的因子赋给k1,...,k10{case 1:k1=i; break; // 找出的笫1个因子赋给k1case 2:k2=i; break; // 找出的笫2个因子赋给k2case 3:k3=i; break; // 找出的笫3个因子赋给k3case 4:k4=i; break; // 找出的笫4个因子赋给k4case 5:k5=i; break; // 找出的笫5个因子赋给k5case 6:k6=i; break; // 找出的笫6个因子赋给k6case 7:k7=i; break; // 找出的笫7个因子赋给k7case 8:k8=i; break; // 找出的笫8个因子赋给k8case 9:k9=i; break; // 找出的笫9个因子赋给k9case 10:k10=i; break; // 找出的笫10个因子赋给k10}}if (s==0) // s=0表示全部因子都已找到了{cout<<a<<" is a 完数"<<endl;cout<<"its factors are:";if (n>1) cout<<k1<<","<<k2; // n>1表示a至少有2个因子if (n>2) cout<<","<<k3; // n>2表示至少有3个因子,故应再输出一个因子if (n>3) cout<<","<<k4; // n>3表示至少有4个因子,故应再输出一个因子if (n>4) cout<<","<<k5; // 以下类似if (n>5) cout<<","<<k6;if (n>6) cout<<","<<k7;if (n>7) cout<<","<<k8;if (n>8) cout<<","<<k9;if (n>9) cout<<","<<k10;cout<<endl<<endl;}}return 0;}3-20-2、#include <iostream>using namespace std;int main(){int m,s,i;for (m=2;m<1000;m++){s=0;for (i=1;i<m;i++)if ((m%i)==0) s=s+i;if(s==m){cout<<m<<" is a完数"<<endl;cout<<"its factors are:";for (i=1;i<m;i++)if (m%i==0) cout<<i<<" ";cout<<endl;}}return 0;}3-20-3、#include <iostream>using namespace std;int main(){int k[11];int i,a,n,s;for (a=2;a<=1000;a++){n=0;s=a;for (i=1;i<a;i++)if ((a%i)==0){n++;s=s-i;k[n]=i; // 将找到的因子赋给k[1]┅k[10]}if (s==0){cout<<a<<" is a 完数"<<endl;cout<<"its factors are:";for (i=1;i<n;i++)cout<<k[i]<<" ";cout<<k[n]<<endl;}}return 0;}3-21、#include <iostream>using namespace std;int main(){int i,t,n=20;double a=2,b=1,s=0;for (i=1;i<=n;i++){s=s+a/b;t=a;a=a+b; // 将前一项分子与分母之和作为下一项的分子b=t; // 将前一项的分子作为下一项的分母}cout<<"sum="<<s<<endl;return 0;}3-22、#include <iostream>using namespace std;int main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2; // 第1天的桃子数是第2天桃子数加1后的2倍x2=x1;day--;}cout<<"total="<<x1<<endl;return 0;}3-23、#include <iostream>#include <cmath>using namespace std;int main(){float a,x0,x1;cout<<"enter a positive number:"; cin>>a; // 输入a的值x0=a/2;x1=(x0+a/x0)/2;do{x0=x1;x1=(x0+a/x0)/2;}while(fabs(x0-x1)>=1e-5);cout<<"The square root of "<<a<<" is "<<x1<<endl;return 0;}3-24、#include <iostream>using namespace std;int main(){int i,k;for (i=0;i<=3;i++) // 输出上面4行*号{for (k=0;k<=2*i;k++)cout<<"*"; // 输出*号cout<<endl; //输出完一行*号后换行}for (i=0;i<=2;i++) // 输出下面3行*号{for (k=0;k<=4-2*i;k++)cout<<"*"; // 输出*号cout<<endl; // 输出完一行*号后换行}return 0;}3-25、#include <iostream>using namespace std;int main(){char i,j,k; /* i是a的对手;j是b的对手;k是c的对手*/for (i='X';i<='Z';i++)for (j='X';j<='Z';j++)if (i!=j)for (k='X';k<='Z';k++)if (i!=k && j!=k)if (i!='X' && k!='X' && k!='Z')cout<<"A--"<<i<<"B--"<<j<<" C--"<<k<<endl;return 0;}第四章4-1、#include <iostream>using namespace std;int main(){int hcf(int,int);int lcd(int,int,int);int u,v,h,l;cin>>u>>v;h=hcf(u,v);cout<<"H.C.F="<<h<<endl;l=lcd(u,v,h);cout<<"L.C.D="<<l<<endl;return 0;}int hcf(int u,int v){int t,r;if (v>u){t=u;u=v;v=t;}while ((r=u%v)!=0){u=v;v=r;}return(v);}int lcd(int u,int v,int h){return(u*v/h);}4-2、#include <iostream>#include <math.h>using namespace std;float x1,x2,disc,p,q;int main(){void greater_than_zero(float,float);void equal_to_zero(float,float);void smaller_than_zero(float,float);float a,b,c;cout<<"input a,b,c:";cin>>a>>b>>c;disc=b*b-4*a*c;cout<<"root:"<<endl;if (disc>0){greater_than_zero(a,b);cout<<"x1="<<x1<<",x2="<<x2<<endl;}else if (disc==0){equal_to_zero(a,b);cout<<"x1="<<x1<<",x2="<<x2<<endl;}else{smaller_than_zero(a,b);cout<<"x1="<<p<<"+"<<q<<"i"<<endl;cout<<"x2="<<p<<"-"<<q<<"i"<<endl;}return 0;}void greater_than_zero(float a,float b) /* 定义一个函数,用来求disc>0时方程的根*/ {x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}void equal_to_zero(float a,float b) /* 定义一个函数,用来求disc=0时方程的根*/ {x1=x2=(-b)/(2*a);}void smaller_than_zero(float a,float b) /* 定义一个函数,用来求disc<0时方程的根*/ {p=-b/(2*a);q=sqrt(-disc)/(2*a);}4-3、#include <iostream>using namespace std;int main(){int prime(int); /* 函数原型声明*/int n;cout<<"input an integer:";cin>>n;if (prime(n))cout<<n<<" is a prime."<<endl;elsecout<<n<<" is not a prime."<<endl; return 0;}int prime(int n){int flag=1,i;for (i=2;i<n/2 && flag==1;i++)if (n%i==0)flag=0;return(flag);}4-4、#include <iostream>using namespace std;int main(){int fac(int);int a,b,c,sum=0;cout<<"enter a,b,c:";cin>>a>>b>>c;sum=sum+fac(a)+fac(b)+fac(c);cout<<a<<"!+"<<b<<"!+"<<c<<"!="<<sum<<e ndl;return 0;}int fac(int n){int f=1;for (int i=1;i<=n;i++)f=f*i;return f;}4-5、#include <iostream>#include <cmath>using namespace std;int main(){double e(double);double x,sinh;cout<<"enter x:";cin>>x;sinh=(e(x)+e(-x))/2;cout<<"sinh("<<x<<")="<<sinh<<endl; return 0;}double e(double x){return exp(x);}4-6、#include <iostream>#include <cmath>using namespace std;int main(){doublesolut(double ,double ,double ,double ); double a,b,c,d;cout<<"input a,b,c,d:";cin>>a>>b>>c>>d;cout<<"x="<<solut(a,b,c,d)<<endl;return 0;}double solut(double a,double b,double c,double d){double x=1,x0,f,f1;do{x0=x;f=((a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c;x=x0-f/f1;}while(fabs(x-x0)>=1e-5);return(x);}4-7、#include <iostream>#include <cmath>using namespace std;int main(){void godbaha(int);int n;cout<<"input n:";cin>>n;godbaha(n);return 0;}void godbaha(int n) {int prime(int);int a,b;for(a=3;a<=n/2;a=a+2){if(prime(a)){b=n-a;if (prime(b))cout<<n<<"="<<a<<"+"<<b<<endl;}}}int prime(int m){int i,k=sqrt(m);for(i=2;i<=k;i++)if(m%i==0) break;if (i>k) return 1;else return 0;}4-8、#include <iostream>using namespace std;int main(){int x,n;float p(int,int);cout<<"input n & x:";cin>>n>>x;cout<<"n="<<n<<",x="<<x<<endl;;cout<<"P"<<n<<"(x)="<<p(n,x)<<endl; return 0;}float p(int n,int x){if (n==0)return(1);else if (n==1)return(x);elsereturn(((2*n-1)*x*p((n-1),x)-(n-1)*p((n-2),x))/ n);}4-9、#include <iostream>using namespace std;int main(){void hanoi(int n,char one,char two,char three);int m;cout<<"input the number of diskes:"; cin>>m;cout<<"The steps of moving "<<m<<" disks:"<<endl;hanoi(m,'A','B','C');return 0;}void hanoi(int n,char one,char two,char three) //将n个盘从one座借助two座,移到three座{void move(char x,char y);if(n==1) move(one,three);else{hanoi(n-1,one,three,two);move(one,three);hanoi(n-1,two,one,three);}}void move(char x,char y){cout<<x<<"-->"<<y<<endl;}4-10、#include <iostream>using namespace std;int main(){void convert(int n);int number;cout<<"input an integer:";cin>>number;cout<<"output:"<<endl;if (number<0){cout<<"-";number=-number;}convert(number);cout<<endl;return 0;}void convert(int n){int i;char c;if ((i=n/10)!=0)convert(i);c=n%10+'0';cout<<" "<<c;}4-11、#include <iostream>using namespace std;int main(){int f(int);int n,s;cout<<"input the number n:";cin>>n;s=f(n);cout<<"The result is "<<s<<endl;return 0;}int f(int n){;if (n==1)return 1;elsereturn (n*n+f(n-1));}4-12、#include <iostream>#include <cmath>using namespace std;#define S(a,b,c) (a+b+c)/2#define AREA(a,b,c) sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c) -c))int main(){float a,b,c;cout<<"input a,b,c:";cin>>a>>b>>c;if (a+b>c && a+c>b && b+c>a)cout<<"area="<<AREA(a,b,c)<<endl; elsecout<<"It is not a triangle!"<<endl; return 0;}4-14、#include <iostream>using namespace std;//#define LETTER 1int main(){char c;cin>>c;#if LETTERif(c>='a' && c<='z')c=c-32;#elseif(c>='A' && c<='Z')c=c+32;#endifcout<<c<<endl;return 0;}4-15、#include <iostream>using namespace std;#define CHANGE 1int main(){char ch[40];cout<<"input text:"<<endl;;gets(ch);#if (CHANGE){for (int i=0;i<40;i++){if (ch[i]!='\0')if (ch[i]>='a'&& ch[i]<'z'||ch[i]>'A'&& ch[i]<'Z')ch[i]+=1;else if (ch[i]=='z'||ch[i]=='Z')ch[i]-=25;}}#endifcout<<"output:"<<endl<<ch<<endl;return 0;}4-16-1、#include <iostream>using namespace std;int a;int main(){extern int power(int);int b=3,c,d,m;cout<<"enter an integer a and its power m:"<<endl;cin>>a>>m;c=a*b;cout<<a<<"*"<<b<<"="<<c<<endl;d=power(m);cout<<a<<"**"<<m<<"="<<d<<endl; return 0;}4-16-2、extern int a;int power(int n){int i,y=1;for(i=1;i<=n;i++)y*=a;return y;}第五章5-1、#include <iostream>#include <iomanip>using namespace std;#include <math.h>int main(){int i,j,n,a[101];for (i=1;i<=100;i++)a[i]=i;a[1]=0;for (i=2;i<sqrt(100);i++)for (j=i+1;j<=100;j++){if(a[i]!=0 && a[j]!=0)if (a[j]%a[i]==0)a[j]=0; }cout<<endl;for (i=1,n=0;i<=100;i++){if (a[i]!=0){cout<<setw(5)<<a[i]<<" ";n++;}if(n==10){cout<<endl;n=0;}}cout<<endl;return 0;}5-2、#include <iostream>using namespace std;//#include <math.h>int main(){int i,j,min,temp,a[11];cout<<"enter data:"<<endl;for (i=1;i<=10;i++){cout<<"a["<<i<<"]=";cin>>a[i]; //输入10个数}cout<<endl<<"The original numbers:"<<endl;;for (i=1;i<=10;i++)cout<<a[i]<<" "; // 输出这10个数cout<<endl;;for (i=1;i<=9;i++) //以下8行是对10个数排序{min=i;for (j=i+1;j<=10;j++)if (a[min]>a[j]) min=j;temp=a[i]; //以下3行将a[i+1]~a[10]中最小者与a[i] 对换a[i]=a[min];a[min]=temp;}cout<<endl<<"The sorted numbers:"<<endl;for (i=1;i<=10;i++) // 输出已排好序的10个数cout<<a[i]<<" ";cout<<endl;return 0;}5-3、#include <iostream>using namespace std;int main(){int a[3][3],sum=0;int i,j;cout<<"enter data:"<<endl;;for (i=0;i<3;i++)for (j=0;j<3;j++)cin>>a[i][j];for (i=0;i<3;i++)sum=sum+a[i][i];cout<<"sum="<<sum<<endl;return 0;}5-4、#include <iostream>using namespace std;int main(){int a[11]={1,4,6,9,13,16,19,28,40,100};int num,i,j;cout<<"array a:"<<endl;for (i=0;i<10;i++)cout<<a[i]<<" ";cout<<endl;;cout<<"insert data:";cin>>num;if (num>a[9])a[10]=num;else{for (i=0;i<10;i++){if (a[i]>num){for (j=9;j>=i;j--)a[j+1]=a[j];a[i]=num;break;}}}cout<<"Now, array a:"<<endl;for (i=0;i<11;i++)cout<<a[i]<<" ";cout<<endl;return 0;}5-5、#include <iostream>using namespace std;int main(){ const int n=5;int a[n],i,temp;cout<<"enter array a:"<<endl;for (i=0;i<n;i++)cin>>a[i];cout<<"array a:"<<endl;for (i=0;i<n;i++)cout<<a[i]<<" ";for (i=0;i<n/2;i++) //循环的作用是将对称的元素的值互换{ temp=a[i];a[i]=a[n-i-1];a[n-i-1]=temp;}cout<<endl<<"Now,array a:"<<endl;for (i=0;i<n;i++)。

c--面向对象程序设计课后习题解答-谭浩强

c--面向对象程序设计课后习题解答-谭浩强

(5)第6行末尾少了一个分号。 (6)add函数中的retrun拼写错误,应为return。编译系统把retrun作为未声明的标识符而报错 ,因为retrun(z)会被认为是函数调用的形式。 (7)变量a和b未被赋值。 改正后的程序如下: #include using namespace std; int main( {int add(int x,int y; int a,b,c; cin >> a >> b; c=add(a,b; cout <<" a+b=" << c < return 0; } int add(int x,int y {int z; z=x+y; return(z; } 运行情况如下: 5 8↙ 13 10.输入以下程序,编译并运行,分析运行结果。 #include using namespace std; int main( { void sort(int x,int y,int z; int x,y,z;
1 5 3↙ (输入3个整数
1 (输出其中最小的数
8.在你所用的C++系统上,输入以下程序,进行编译,观察编译情况,如果有错误,请修改 程序,再进行编译,直到没有错误,然后进行连接和运行,分析运行结果。
int main( ; { int a,b; c=a+b; cout >>" a+b=" >> a+b; } 【解】 上机编译出错,编译出错信息告知在第2行出错,经检查,发现第1行的末尾多了一个分号,编译 系统无法理解第2行的花括号,导致报告第2行出错。将第1行的末尾的分号去掉,重新编译,编 译出错信息告知在第5行和第6行出错。第5行出错原因是cout未经声明,因为cout不是C++语言 提供的系统的关键字,而是输出流的对象,必须使用头文件iostream。第6行出错原因是main是i nt型函数,应返回一个整型值。将程序改为

最新c++面向对象课后答案第8章

最新c++面向对象课后答案第8章

最新c++面向对象课后答案第8章1.1在C++中,三种派生方式的说明符号为public、private、protected不加说明,则默认的派生方式为private。

1.2当公有派生时,基类的公有成员成为派生类的公有成员;保护成员成为派生类的保护成员;私有成员成为派生类的不能直接访问成员。

当保护派生时,基类的公有成员成为派生类的保护成员;保护成员成为派生类的保护成员;私有成员成为派生类的不能直接访问成员。

1.3 派生类的构造函数一般有3项工作要完成:首先基类初始化,其次成员对象初始化,最后执行派生类构造函数体。

1.4多继承时,多个基类中的同名的成员在派生类中由于标识符不唯一而出现二义性。

在派生类中采用虚基类或作用域分辨符来消除该问题。

2.简答题2.1 派生类如何实现对基类私有成员的访问?2.2什么是类型兼容规则?2.3派生类的构造函数是怎样的执行顺序,析构函数的执行顺序是如何实现的?2.4继承与组合之间的区别与联系是什么?2.5什么是虚基类?它有什么作用?含有虚基类的派生类的构造函数有什么要求,什么是最远派生类,建立一个含有虚基类的派生类的对象时,为什么由最远派生类的构造函数负责虚基类的初始化?3.选择题3.1下面对派生类的描述中,错误的是(D )。

A.一个派生类可以作为另外一个派生类的基类B.派生类至少有一个基类C.派生类的成员除了它自己的成员外,还包含了它的基类的成员D.派生类中继承的基类成员的访问权限到派生类中保持不变3.2下列对友元关系叙述正确的是(A)。

A.不能继承B.是类与类的关系C.是一个类的成员函数与另一个类的关系D.提高程序的运行效率3.3当保护继承时,基类的(B)在派生类中成为保护成员,不能通过派生类的对象来直接访问。

A.任何成员B.公有成员和保护成员C.公有成员和私有成员D.私有成员3.4设置虚基类的目的是(B)。

A.简化程序B.消除二义性C.提高运行效率D.减少目标代码3.5在公有派生情况下,有关派生类对象和基类对象的关系,不正确的叙述是(C )。

《C语言程序设计》课后习题答案第八章

《C语言程序设计》课后习题答案第八章

8.1 编写两个函数,分别求两个证书的最大公约数和最小公倍数,用主函数调用这两个函数并输出结果,两个整数由键盘输入。

void main(){ int Mgy(int x,int y);int Mgb(int z);int a,b,mgy,mgb;printf("请输入两个数:\n");scanf("%d,%d",&a,&b);mgy=Mgy(a,b);mgb=Mgb(a,b,mgy);printf("两个数的最大公约数为%d,最小公倍数为%d\n",mgy,mgb);}int Mgy(int x,int y){ int r,temp;if(x<y){ temp=x;x=y;y=temp;}while(x%y!=0){ r=x%y;x=y;y=r;}return y;}int Mgb(int x,int y,int z){ return (x*y/z);}8.2 求方程ax²+bx+c=0的根,用三个函数分别求当b²-4ac大于零、等于零和小于零时的根,8.3编写一个判素数的函数,在主函数输入一个整数,输出是否是素数的信息。

#include<math.h>void main(){ int Isprime(int a);int m,temp=0;printf("请输入一个数:\n");scanf("%d",&m);temp=Isprime(m);if(temp==0) printf("%d不是素数。

\n",m);else printf("%d是素数。

\n",m);}int Isprime(int a){ int i,k,flag;if(a==0||a==1) flag=0;else{ k=sqrt(a);for(i=2;i<=k;i++)if(a%i==0) flag=0; }return flag; }8.8 写一个函数,输入一个4位数字,要求输出这4个数字字符,但每两个数字间空一格空8.9编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符8.10 写一个函数,输入一行字符,将此字符串中最长的单词输出。

c 面向对象程序设计课后习题答案(谭浩强版)

c  面向对象程序设计课后习题答案(谭浩强版)

第一章5:#include <iostream> using namespace std;int main(){cout<<"This"<<"is"; cout<<"a"<<"C++";cout<<"program."<<endl; return 0;}6:#include <iostream> using namespace std;int main(){int a,b,c;a=10;b=23;c=a+b;cout<<"a+b=";cout<<c;cout<<endl;return 0;}7:#include <iostream> using namespace std;int main(){int a,b,c;int f(int x,int y,int z); cin>>a>>b>>c;c=f(a,b,c);cout<<c<<endl;return 0;}int f(int x,int y,int z){int m;else m=y;if (z<m) m=z;return(m);}8: #include <iostream> using namespace std;int main(){int a,b,c;cin>>a>>b;c=a+b;cout<<"a+b="<<a+b<<endl; return 0;}9:#include <iostream>using namespace std;int main(){int add(int x,int y);int a,b,c;cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl; return 0;}int add(int x,int y){int c;c=x+y;return(c);}10:#include <iostream>using namespace std;int main(){void sort(int x,int y,int z); int x,y,z;cin>>x>>y>>z;return 0;}void sort(int x, int y, int z){int temp;if (x>y) {temp=x;x=y;y=temp;} //{ }内3个语句的作用是将x和y的值互换) if (z<x) cout<<z<<','<<x<<','<<y<<endl;else if (z<y) cout<<x<<','<<z<<','<<y<<endl;else cout<<x<<','<<y<<','<<z<<endl;}11:#include <iostream>using namespace std;int main(){int max(int a,int b,int c=0);int a,b,c;cin>>a>>b>>c;cout<<"max(a,b,c)="<<max(a,b,c)<<endl;cout<<"max(a,b)="<<max(a,b)<<endl;return 0;}int max(int a,int b,int c){if(b>a) a=b;if(c>a) a=c;return a;}12:#include <iostream>using namespace std;int main(){void change(int ,int );int a,b;cin>>a>>b;if(a<b) change(a,b);cout<<"max="<<a<<" min="<<b<<endl;return 0;}void change(int ,int ){int r1,r2,temp;temp=r1;r1=r2;r2=temp;}13:#include <iostream>using namespace std;int main(){void sort(int &,int &,int &);int a,b,c,a1,b1,c1;cout<<"Please enter 3 integers:";cin>>a>>b>>c;a1=a;b1=b;c1=c;sort(a1,b1,c1);cout<<a<<" "<<b<<" "<<c<<" in sorted order is "; cout<<a1<<" "<<b1<<" "<<c1<<endl;return 0;}void sort(int &i,int &j,int &k){ void change(int &,int &);if (i>j) change(i,j);if (i>k) change(i,k);if (j>k) change(j,k);}void change(int &x,int &y){ int temp;temp=x;x=y;y=temp;}14:#include <iostream>#include <string>using namespace std;int main(){ string s1="week",s2="end";cout<<"s1="<<s1<<endl;cout<<"s2="<<s2<<endl;s1=s1+s2;cout<<"The new string is:"<<s1<<endl;return 0;}15:#include <iostream>#include <string>using namespace std;int main(){ string str;int i,n;char temp;cout<<"please input a string:";cin>>str;n=str.size();for(i=0;i<n/2;i++){temp=str[i];str[i]=str[n-i-1];str[n-i-1]=temp;}cout<<str<<endl;return 0;}16:#include <iostream>#include <string>using namespace std;int main(){ int i;string str[5]={"BASIC","C","FORTRAN","C++","PASCAL"}; void sort(string []);sort(str);cout<<"the sorted strings :"<<endl;for(i=0;i<5;i++)cout<<str[i]<<" ";cout<<endl;return 0;}void sort(string s[]){int i,j;string t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (s[i]>s[i+1]){t=s[i];s[i]=s[i+1];s[i+1]=t;}}17: #include <iostream>#include <string>using namespace std;int main(){long c[5]={10100,-123567, 1198783,-165654, 3456}; int a[5]={1,9,0,23,-45};float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 };void sort(int []);void sort(float []);void sort(long []);sort(a);sort(b);sort(c);return 0;}void sort(int a[]){int i,j,t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(long a[]){int i,j;long t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}void sort(float a[]){int i,j;float t;for (j=0;j<5;j++)for(i=0;i<5-j;i++)if (a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}18: #include <iostream>#include <string>using namespace std;template <typename T>void sort(T a[]){int i,j,min;T t;for(i=0;i<5;i++){min=i;for (j=i+1;j<5;j++)if(a[min]>a[j]) min=j;t=a[i]; a[i]=a[min]; a[min]=t;}cout<<"the sorted numbers :"<<endl;for(i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl<<endl;}int main(){ int a[5]={1,9,0,23,-45};float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 };long c[5]={10100,-123567, 1198783,-165654, 3456}; sort(a);sort(b);sort(c);return 0;}第二章1#include <iostream>using namespace std;class Time{public:void set_time();void show_time();private: //成员改为公用的int hour;int minute;int sec;};void Time::set_time() //在main函数之前定义{cin>>hour;cin>>minute;cin>>sec;}void Time::show_time() //在main函数之前定义{cout<<hour<<":"<<minute<<":"<<sec<<endl;}int main(){Time t1;t1.set_time();t1.show_time();return 0;}2:#include <iostream>using namespace std;class Time{public:void set_time(void){cin>>hour;cin>>minute;cin>>sec;void show_time(void){cout<<hour<<":"<<minute<<":"<<sec<<endl;}private: int hour;int minute;int sec;};Time t;int main(){t.set_time();t.show_time();return 0;}3:#include <iostream>using namespace std;class Time{public:void set_time(void);void show_time(void);private:int hour;int minute;int sec;};void Time::set_time(void){cin>>hour;cin>>minute;cin>>sec;}void Time::show_time(void){cout<<hour<<":"<<minute<<":"<<sec<<endl;}Time t;int main(){ t.set_time();t.show_time();return 0;}//xt2-4-1.cpp(main.cpp)#include <iostream>using namespace std;#include "xt2-4.h"int main(){Student stud;stud.set_value();stud.display();return 0;}//xt2-4-2.cpp(即student.cpp)#include "xt2-4.h" //在此文件中进行函数的定义#include <iostream>using namespace std; //不要漏写此行void Student::display( ){ cout<<"num:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;}void Student::set_value(){ cin>>num;cin>>name;cin>>sex;}5://xt2-5-1.cpp(file1.cpp)#include <iostream>#include "xt2-5.h"int main(){Array_max arrmax;arrmax.set_value();arrmax.max_value();arrmax.show_value();return 0;}//xt2-5-2.cpp(arraymax.cpp)#include <iostream>using namespace std;#include "xt2-5.h"void Array_max::set_value()for (i=0;i<10;i++)cin>>array[i];}void Array_max::max_value(){int i;max=array[0];for (i=1;i<10;i++)if(array[i]>max) max=array[i];}void Array_max::show_value(){cout<<"max="<<max<<endl;}6:解法一#include <iostream>using namespace std;class Box{public:void get_value();float volume();void display();public:float lengh;float width;float height;};void Box::get_value(){ cout<<"please input lengh, width,height:"; cin>>lengh;cin>>width;cin>>height;}float Box::volume(){ return(lengh*width*height);}void Box::display(){ cout<<volume()<<endl;}{Box box1,box2,box3;box1.get_value();cout<<"volmue of bax1 is ";box1.display();box2.get_value();cout<<"volmue of bax2 is ";box2.display();box3.get_value();cout<<"volmue of bax3 is ";box3.display();return 0;}解法二:#include <iostream>using namespace std;class Box{public:void get_value();void volume();void display();public:float lengh;float width;float height;float vol;};void Box::get_value(){ cout<<"please input lengh, width,height:"; cin>>lengh;cin>>width;cin>>height;}void Box::volume(){ vol=lengh*width*height;}void Box::display(){ cout<<vol<<endl;}int main(){Box box1,box2,box3;box1.get_value();box1.volume();cout<<"volmue of bax1 is ";box1.display();box2.get_value();box2.volume();cout<<"volmue of bax2 is ";box2.display();box3.get_value();box3.volume();cout<<"volmue of bax3 is ";box3.display();return 0;}第三章2:#include <iostream>using namespace std;class Date{public:Date(int,int,int);Date(int,int);Date(int);Date();void display();private:int month;int day;int year;};Date::Date(int m,int d,int y):month(m),day(d),year(y) { }Date::Date(int m,int d):month(m),day(d){year=2005;}Date::Date(int m):month(m){day=1;year=2005;}{month=1;day=1;year=2005;}void Date::display(){cout<<month<<"/"<<day<<"/"<<year<<endl;}int main(){Date d1(10,13,2005);Date d2(12,30);Date d3(10);Date d4;d1.display();d2.display();d3.display();d4.display();return 0;}3:#include <iostream>using namespace std;class Date{public:Date(int=1,int=1,int=2005);void display();private:int month;int day;int year;};Date::Date(int m,int d,int y):month(m),day(d),year(y) { }void Date::display(){cout<<month<<"/"<<day<<"/"<<year<<endl;}int main(){Date d1(10,13,2005);Date d2(12,30);Date d4;d1.display();d2.display();d3.display();d4.display();return 0;}4:#include <iostream>using namespace std;class Student{public:Student(int n,float s):num(n),score(s){}void display();private:int num;float score;};void Student::display(){cout<<num<<" "<<score<<endl;}int main(){Student stud[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(int i=0;i<=2;p=p+2,i++)p->display();return 0;}5:#include <iostream>using namespace std;class Student{public:Student(int n,float s):num(n),score(s){}int num;float score;};void main(){Student stud[5]={Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5)};void max(Student* );Student *p=&stud[0];max(p);}void max(Student *arr){float max_score=arr[0].score;int k=0;for(int i=1;i<5;i++)if(arr[i].score>max_score) {max_score=arr[i].score;k=i;} cout<<arr[k].num<<" "<<max_score<<endl;}6:#include <iostream>using namespace std;class Student{public:Student(int n,float s):num(n),score(s){}void change(int n,float s) {num=n;score=s;}void display(){cout<<num<<" "<<score<<endl;} private:int num;float score;};int main(){Student stud(101,78.5);stud.display();stud.change(101,80.5);stud.display();return 0;}7: 解法一#include <iostream>using namespace std;class Student{public:Student(int n,float s):num(n),score(s){}void change(int n,float s) {num=n;score=s;}void display() {cout<<num<<" "<<score<<endl;}//可改为:void display() const {cout<<num<<" "<<score<<endl;} private:int num;float score;};int main(){const Studentstud(101,78.5);stud.display();//stud.change(101,80.5);stud.display();return 0;}解法二:#include <iostream>using namespace std;class Student{public:Student(int n,float s):num(n),score(s){}void change(int n,float s) const {num=n;score=s;}void display() const {cout<<num<<" "<<score<<endl;}private:mutable int num;mutable float score;};int main(){const Student stud(101,78.5);stud.display();stud.change(101,80.5);stud.display();return 0;}解法三:#include <iostream>using namespace std;class Student{public:Student(int n,float s):num(n),score(s){}void change(int n,float s) {num=n;score=s;}void display() {cout<<num<<" "<<score<<endl;} private:int num;float score;};int main(){Student stud(101,78.5);Student *p=&stud;p->display();p->change(101,80.5);p->display();return 0;}8:#include <iostream>using namespace std;class Student{public:Student(int n,float s):num(n),score(s){}void change(int n,float s) {num=n;score=s;}void display() {cout<<num<<" "<<score<<endl;} private:int num;float score;};int main(){Student stud(101,78.5);void fun(Student&);fun(stud);return 0;}void fun(Student &stu){stu.display();stu.change(101,80.5);stu.display();}9:#include <iostream>using namespace std;class Product{public:Product(int n,int q,float p):num(n),quantity(q),price(p){};void total();static float average();static void display();private:int num;int quantity;float price;static float discount;static float sum;static int n;};void Product::total(){float rate=1.0;if(quantity>10) rate=0.98*rate;sum=sum+quantity*price*rate*(1-discount);n=n+quantity;}void Product::display(){cout<<sum<<endl;cout<<average()<<endl;}float Product::average(){return(sum/n);}float Product::discount=0.05;float Product::sum=0;int Product::n=0;int main(){Product Prod[3]={Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5) };for(int i=0;i<3;i++)Prod[i].total();Product::display();return 0;}10:#include <iostream>using namespace std;class Date;class Time{public:Time(int,int,int);friend void display(const Date &,const Time &); private:int hour;int minute;int sec;};Time::Time(int h,int m,int s){hour=h;minute=m;sec=s;}class Date{public:Date(int,int,int);friend void display(const Date &,const Time &); private:int month;int day;int year;};Date::Date(int m,int d,int y){month=m;day=d;year=y;}void display(const Date &d,const Time &t){cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;}int main(){Time t1(10,13,56);Date d1(12,25,2004);display(d1,t1);return 0;}11:#include <iostream>using namespace std;class Time;class Date{public:Date(int,int,int);friend Time;private:int month;int day;int year;};Date::Date(int m,int d,int y):month(m),day(d),year(y){ }class Time{public:Time(int,int,int);void display(const Date &);private:int hour;int minute;int sec;};Time::Time(int h,int m,int s):hour(h),minute(m),sec(s){ }void Time::display(const Date &d){cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;cout<<hour<<":"<<minute<<":"<<sec<<endl;}int main(){Time t1(10,13,56);Date d1(12,25,2004);t1.display(d1);return 0;}12:#include <iostream>using namespace std;template<class numtype>class Compare{public:Compare(numtype a,numtype b);numtype max();numtype min();private:numtype x,y;};template <class numtype>Compare<numtype>::Compare(numtype a,numtype b){x=a;y=b;}template <class numtype>numtype Compare<numtype>::max(){return (x>y)?x:y;}template <class numtype>numtype Compare<numtype>::min(){return (x<y)?x:y;}int main(){Compare<int> cmp1(3,7);cout<<cmp1.max()<<" is the Maximum of two integer numbers."<<endl;cout<<cmp1.min()<<" is the Minimum of two integer numbers."<<endl<<endl; Compare<float> cmp2(45.78,93.6);cout<<cmp2.max()<<" is the Maximum of two float numbers."<<endl;cout<<cmp2.min()<<" is the Minimum of two float numbers."<<endl<<endl; Compare<char> cmp3('a','A');cout<<cmp3.max()<<" is the Maximum of two characters."<<endl;cout<<cmp3.min()<<" is the Minimum of two characters."<<endl;return 0;}第四章1:#include <iostream>using namespace std;class Complex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}double get_real();double get_imag();void display();private:double real;double imag;};double Complex::get_real(){return real;}double Complex::get_imag(){return imag;}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complex operator + (Complex &c1,Complex &c2){return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag()); }int main(){Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;cout<<"c3=";c3.display();return 0;}2:#include <iostream>using namespace std;class Complex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}Complex operator+(Complex &c2);Complex operator-(Complex &c2);Complex operator*(Complex &c2);Complex operator/(Complex &c2);void display();private:double real;double imag;};Complex Complex::operator+(Complex &c2){Complex c;c.real=real+c2.real;c.imag=imag+c2.imag;return c;}Complex Complex::operator-(Complex &c2){Complex c;c.real=real-c2.real;c.imag=imag-c2.imag;return c;}Complex Complex::operator*(Complex &c2){Complex c;c.real=real*c2.real-imag*c2.imag;c.imag=imag*c2.real+real*c2.imag;return c;}Complex Complex::operator/(Complex &c2){Complex c;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); return c;}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}int main(){Complex c1(3,4),c2(5,-10),c3;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();return 0;}3:#include <iostream> //用VC++时改为∶#include <iostream.h> using namespace std; //用VC++时为取消此行class Complex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}Complex operator+(Complex &c2);Complex operator+(int &i);friend Complex operator+(int&,Complex &);void display();private:double real;double imag;};Complex Complex::operator+(Complex &c){return Complex(real+c.real,imag+c.imag);}Complex Complex::operator+(int &i){return Complex(real+i,imag);}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complex operator+(int &i,Complex &c){return Complex(i+c.real,c.imag);}int main(){Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;cout<<"c1+c2=";c3.display();c3=i+c1;cout<<"i+c1=";c3.display();c3=c1+i;cout<<"c1+i=";c3.display();return 0;}4:#include <iostream>using namespace std;class Matrix //定义Matrix类{public:Matrix(); //默认构造函数friend Matrix operator+(Matrix &,Matrix &); //重载运算符“+”void input(); //输入数据函数void display(); //输出数据函数private:int mat[2][3];};Matrix::Matrix() //定义构造函数{for(int i=0;i<2;i++)for(int j=0;j<3;j++)mat[i][j]=0;}Matrix operator+(Matrix &a,Matrix &b) //定义重载运算符“+”函数{Matrix c;for(int i=0;i<2;i++)for(int j=0;j<3;j++){c.mat[i][j]=a.mat[i][j]+b.mat[i][j];}return c;}void Matrix::input() //定义输入数据函数{cout<<"input value of matrix:"<<endl;for(int i=0;i<2;i++)for(int j=0;j<3;j++)cin>>mat[i][j];}void Matrix::display() //定义输出数据函数{for (int i=0;i<2;i++){for(int j=0;j<3;j++){cout<<mat[i][j]<<" ";}cout<<endl;}}int main(){Matrix a,b,c;a.input();b.input();cout<<endl<<"Matrix a:"<<endl;a.display();cout<<endl<<"Matrix b:"<<endl;b.display();c=a+b; //用重载运算符“+”实现两个矩阵相加cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl;c.display();return 0;}5:#include <iostream.h>//using namespace std;class Matrix{public:Matrix();friend Matrix operator+(Matrix &,Matrix &);friend ostream& operator<<(ostream&,Matrix&);friend istream& operator>>(istream&,Matrix&);private:int mat[2][3];};Matrix::Matrix(){for(int i=0;i<2;i++)for(int j=0;j<3;j++)mat[i][j]=0;}Matrix operator+(Matrix &a,Matrix &b){Matrix c;for(int i=0;i<2;i++)for(int j=0;j<3;j++){c.mat[i][j]=a.mat[i][j]+b.mat[i][j];}return c;}istream& operator>>(istream &in,Matrix &m){cout<<"input value of matrix:"<<endl;for(int i=0;i<2;i++)for(int j=0;j<3;j++)in>>m.mat[i][j];return in;}ostream& operator<<(ostream &out,Matrix &m){for (int i=0;i<2;i++){for(int j=0;j<3;j++){out<<m.mat[i][j]<<" ";}out<<endl;}return out;}int main(){ Matrix a,b,c;cin>>a;cin>>b;cout<<endl<<"Matrix a:"<<endl<<a<<endl;cout<<endl<<"Matrix b:"<<endl<<b<<endl;c=a+b;cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl<<c<<endl; return 0;}6:#include <iostream>using namespace std;class Complex{public:Complex(){real=0;imag=0;}Complex(double r){real=r;imag=0;}Complex(double r,double i){real=r;imag=i;}operator double(){return real;}void display();private:double real;double imag;};void Complex::display(){cout<<"("<<real<<", "<<imag<<")"<<endl;}int main(){Complex c1(3,4),c2;double d1;d1=2.5+c1;cout<<"d1="<<d1<<endl;c2=Complex(d1);cout<<"c2=";c2.display();return 0;}7:#include <iostream>using namespace std;class Student{public:Student(int,char[],char,float);int get_num(){return num;}char * get_name(){return name;}char get_sex(){return sex;}void display(){cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<"\nscore:"<<score<<"\n\n";} private:int num;char name[20];char sex;float score;};Student::Student(int n,char nam[],char s,float so){num=n;strcpy(name,nam);sex=s;score=so;}class Teacher{public:Teacher(){}Teacher(Student&);Teacher(int n,char nam[],char sex,float pay);void display();private:int num;char name[20];char sex;float pay;};Teacher::Teacher(int n,char nam[],char s,float p){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;}void Teacher::display(){cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<"\npay:"<<pay<<"\n\n";}int main(){Teacher teacher1(10001,"Li",'f',1234.5),teacher2;Student student1(20010,"Wang",'m',89.5);cout<<"student1:"<<endl;student1.display();teacher2=Teacher(student1);cout<<"teacher2:"<<endl;teacher2.display();return 0;}第五章1:#include <iostream>using namespace std;class Student。

C课后习题答案第八章

C课后习题答案第八章
teacher::print();
}
virtualintwages()
{ intw=geth();
return(3000+40*w);
}
};
classlecturer:publicteacher
p3.out();
cout<<"连接以后的长度为"<<p3.len()<<endl;
}
3、#include"iostream.h"
classPoint
{
privateo:ublex,y;
public:
Point(doublea,doubleb)
intgeth()
{returnhour;}
voidprint()
{ cout<<"课时数是:"<<hour<<"小时"<<endl; }
virtualintwages()
{
return(0);
doublegetweight()
{ returnweight;}
chargetcolor()
{ returncolor; }
voidprint()
{ cout<<"age="<<age<<",weight="<<weight<<",color="<<color<<endl; } m};
p=&c;
p->print();
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.1在C++中,三种派生方式的说明符号为public、private、protected不加说明,则默认的派生方式为private。

1.2当公有派生时,基类的公有成员成为派生类的公有成员;保护成员成为派生类的保护成员;私有成员成为派生类的不能直接访问成员。

当保护派生时,基类的公有成员成为派生类的保护成员;保护成员成为派生类的保护成员;私有成员成为派生类的不能直接访问成员。

1.3 派生类的构造函数一般有3项工作要完成:首先基类初始化,其次成员对象初始化,最后执行派生类构造函数体。

1.4多继承时,多个基类中的同名的成员在派生类中由于标识符不唯一而出现二义性。

在派生类中采用虚基类或作用域分辨符来消除该问题。

2.简答题2.1 派生类如何实现对基类私有成员的访问?2.2什么是类型兼容规则?2.3派生类的构造函数是怎样的执行顺序,析构函数的执行顺序是如何实现的?2.4继承与组合之间的区别与联系是什么?2.5什么是虚基类?它有什么作用?含有虚基类的派生类的构造函数有什么要求,什么是最远派生类,建立一个含有虚基类的派生类的对象时,为什么由最远派生类的构造函数负责虚基类的初始化?3.选择题3.1下面对派生类的描述中,错误的是(D )。

A.一个派生类可以作为另外一个派生类的基类B.派生类至少有一个基类C.派生类的成员除了它自己的成员外,还包含了它的基类的成员D.派生类中继承的基类成员的访问权限到派生类中保持不变3.2下列对友元关系叙述正确的是(A)。

A.不能继承B.是类与类的关系C.是一个类的成员函数与另一个类的关系D.提高程序的运行效率3.3当保护继承时,基类的(B)在派生类中成为保护成员,不能通过派生类的对象来直接访问。

A.任何成员B.公有成员和保护成员C.公有成员和私有成员D.私有成员3.4设置虚基类的目的是(B)。

A.简化程序B.消除二义性C.提高运行效率D.减少目标代码3.5在公有派生情况下,有关派生类对象和基类对象的关系,不正确的叙述是(C )。

A.派生类的对象可以赋给基类的对象B.派生类的对象可以初始化基类的引用C.派生类的对象可以直接访问基类中的成员D.派生类的对象的地址可以赋给指向基类的指针3.6有如下类定义:class MyBASE{int k;public:void set(int n) {k=n;}int get( ) const {return k;}};class MyDERIVED: protected MyBASE{protected;int j;public:void set(int m,int n){MyBASE::set(m);j=n;}int get( ) const{return MyBASE::get( )+j;}};则类MyDERIVED中保护成员个数是(B)。

A.4 B.3 C.2 D.13.7程序如下:#include<iostream>using namespace std;class A {public:A( ) {cout<<”A”;}};class B {public:B( ) {cout<<”B”;} };class C: public A{B b;public:C( ) {cout<<”C”;}};int main( ) {C obj; return 0;}执行后的输出结果是(D)。

A.CBA B.BAC C.ACB D.ABC3.8类O定义了私有函数F1。

P和Q为O的派生类,定义为class P: protected O{…};class Q: public O{…}。

(C)可以访问Fl。

A.O的对象B.P类内C.O类内D.Q类内3.9有如下类定义:class XA{int x;public:XA(int n) {x=n;}};class XB: public XA{int y;public:XB(int a,int b);};在构造函数XB的下列定义中,正确的是(B)。

A.XB::XB(int a,int b):x(a),y(b){ }B.XB::XB(int a,int b):XA(a),y(b) { }C.XB::XB(int a,int b):x(a),XB(b){ }D.XB::XB(int a,int b):XA(a),XB(b){ } 3.10有如下程序:#include<iostream>using namespace std;class Base{private:void fun1( ) const {cout<<”fun1”;}protected:void fun2( ) const {cout<<”fun2”;}public:void fun3( ) const {cout<<”fun3”;}};class Derived : protected Base{public:void fun4( ) const {cout<<”fun4”;}};int main(){Derived obj;obj.fun1( ); //①obj.fun2( ); //②obj.fun3( ); //③obj.fun4( ); //④}其中没有语法错误的语句是(D )。

A.①B.②C.③ D.④4.写出程序运行结果4.l#include<iostream>using namespace std;class B1{public:B1(int i){ cout<<”constructing B1 “<<i<<endl; } ~B1( ){ cout<<”destructing B1 “<<endl; }};class B2 {public:B2( ){ cout<<”constructing B3 *”<<endl; }~B2( ){ cout<<”destructing B3”<<endl; }};class C:public B2, virtual public B1 {int j;public:C(int a,int b,int c):B1(a),memberB1(b) ,j(c){}private:B1 memberB1;B2 memberB2;};int main( ){C obj(1,2,3);}constructing B1 1constructing B3 *constructing B1 2constructing B3 *destructing B3destructing B1destructing B3destructing B14.2#include<iostream>using namespace std;class B{public:void f1(){cout<<"B::f1"<<endl;}};class D:public B{public:void f1(){cout<<"D::f1"<<endl;}};void f(B& rb){rb.f1();}int main( ){D d;B b,&rb1=b,&rb2=d;f(rb1); f(rb2);return 0;}B::f1B::f15.编程题5.1定义一个Point类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area()。

#include<iostream>using namespace std;const double PI=3.14159;class Point {public:Point(double x=0, double y=0) {X=x;Y=y;}void ShowPoint() {cout<<"("<< X<<","<<Y<<")"<<endl;}private:double X,Y;};class Rectangle: public Point {public:Rectangle(double w,double h,double x,double y):Point(x,y){width=w,height=h;Area();}void Area() {area= width*height;}void ShowArea(){cout<<"Rectangle Area="<<area<<endl;}private:double width,height,area;};class Circle: public Point {public:Circle(double r,double x, double y):Point(x,y){radius=r;Area();}void Area() {area= PI*radius*radius;}void ShowArea(){cout<<"Circle Area="<<area<<endl;}private:double radius,area;};int main(){Rectangle r(10,8,0,0);Circle c(4,3,5);r.ShowArea();c.ShowArea();return 0;}5.2设计一个建筑物类Building,由它派生出教学楼Teach-Building和宿舍楼类Dorm-Building,前者包括教学楼编号、层数、教室数、总面积等基本信息,后者包括宿舍楼编号、层数、宿舍数、总面积和容纳学生总人数等基本信息。

#include<iostream>#include<cstring>using namespace std;enum AMPM{AM=1,PM};class Building{public:Building(char *);void ShowBuilding(){cout<<Name;}protected:char Name[30];};Building::Building(char *name){strcpy(Name,name);};class Teach_Building: public Building {public:Teach_Building(char *,int,int,int,int);void ShowTeach_Building(){Building::ShowBuilding();cout<<" No:"<<No;cout<<" Floors:"<<Floors;cout<<" ClassRooms:"<<ClassRooms;cout<<" Area:"<<Area<<endl;}protected:int No,Floors,ClassRooms,Area;};Teach_Building::Teach_Building(char *name,int no,int fl,int cr,int ar):Building(name){ No=no;Floors=fl;ClassRooms=cr;Area=ar;}class Dorm_Building:public Building {public:Dorm_Building(char *,int,int,int,int,int);void ShowDorm_Building(){Building::ShowBuilding();cout<<" No:"<<No;cout<<" Floors:"<<Floors;cout<<" DormRooms:"<<DormRooms;cout<<" Area:"<<Area;cout<<" StudentNum:"<<StudentNum<<endl;}protected:int No,Floors,DormRooms,Area,StudentNum;};Dorm_Building:: Dorm_Building(char *name,int no,int fl,int dr,int ar,int sn):Building(name){ No=no;Floors=fl;DormRooms=dr;Area=ar;StudentNum=sn;}int main(){Teach_Building tb("主教学楼",59,6,66,18000);Dorm_Building db("北苑男生宿舍",41,5,75,3750,300);tb.ShowTeach_Building();db.ShowDorm_Building();return 0;}5.3定义并描述一个Table类和一个Circle类,由它们共同派生出RoundTable类。

相关文档
最新文档