c++经典课后习题整理Word下载.docx

上传人:b****2 文档编号:4125571 上传时间:2023-05-02 格式:DOCX 页数:21 大小:19.49KB
下载 相关 举报
c++经典课后习题整理Word下载.docx_第1页
第1页 / 共21页
c++经典课后习题整理Word下载.docx_第2页
第2页 / 共21页
c++经典课后习题整理Word下载.docx_第3页
第3页 / 共21页
c++经典课后习题整理Word下载.docx_第4页
第4页 / 共21页
c++经典课后习题整理Word下载.docx_第5页
第5页 / 共21页
c++经典课后习题整理Word下载.docx_第6页
第6页 / 共21页
c++经典课后习题整理Word下载.docx_第7页
第7页 / 共21页
c++经典课后习题整理Word下载.docx_第8页
第8页 / 共21页
c++经典课后习题整理Word下载.docx_第9页
第9页 / 共21页
c++经典课后习题整理Word下载.docx_第10页
第10页 / 共21页
c++经典课后习题整理Word下载.docx_第11页
第11页 / 共21页
c++经典课后习题整理Word下载.docx_第12页
第12页 / 共21页
c++经典课后习题整理Word下载.docx_第13页
第13页 / 共21页
c++经典课后习题整理Word下载.docx_第14页
第14页 / 共21页
c++经典课后习题整理Word下载.docx_第15页
第15页 / 共21页
c++经典课后习题整理Word下载.docx_第16页
第16页 / 共21页
c++经典课后习题整理Word下载.docx_第17页
第17页 / 共21页
c++经典课后习题整理Word下载.docx_第18页
第18页 / 共21页
c++经典课后习题整理Word下载.docx_第19页
第19页 / 共21页
c++经典课后习题整理Word下载.docx_第20页
第20页 / 共21页
亲,该文档总共21页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

c++经典课后习题整理Word下载.docx

《c++经典课后习题整理Word下载.docx》由会员分享,可在线阅读,更多相关《c++经典课后习题整理Word下载.docx(21页珍藏版)》请在冰点文库上搜索。

c++经典课后习题整理Word下载.docx

(2)设计一个学生类Student,拥有的数据成员是学号、姓名、电话号码、所属院系,成员函数包括访问和修改这些属性,在main()中定义对象,并输出相关信息

#include<

classStudent//学号、姓名、电话号码、所属院系,成员函数包括访问和修改这些属性

stringxuehao;

stringname;

stringtel;

stringdepartment;

Student(stringx,stringn,stringt,stringd)

{

xuehao=x;

name=n;

tel=t;

department=d;

}

voidchange(stringx,stringn,stringt,stringd)//修改属性

voiddisplay()

xuehao="

xuehao<

name="

name<

tel="

tel<

department="

department<

stringa,b,c,d;

a;

b;

c;

d;

Students(a,b,c,d);

s.display();

return0;

(3)设计一个学生类,包含学生呢个姓名、成绩(char*name;

doublescore),设计一个友元函数,比较学生成绩的高低,并求出下一组学生:

{Stu("

zhang"

),78,Stu("

wang"

80),Stu("

li"

65),Stu("

chen"

50)}中的最高分和最低分……

classstudent

char*name;

doublescore;

student()

student(char*na,doublesc)

name=na;

score=sc;

friendvoidorder(students[]);

voidorder(students[])

studenttmp;

inti,j;

for(j=0;

j<

3;

j++)

for(i=0;

i<

3-j;

i++)

if(s[i].score<

s[i+1].score)

tmp=s[i];

s[i]=s[i+1];

s[i+1]=tmp;

分数由高到低排列:

4;

s[i].name<

"

s[i].score<

最高分:

s[0].name<

s[0].score<

最低分:

s[3].name<

s[3].score<

students[4]={student("

78),student("

80),student("

92),student("

50)};

order(s);

(4)编写程序,定义机动车类Vehicle,包括的数据成员有出厂日期和售价并定义成员函数可以设置这些数据成员,再定义print()然后定义car类,增加乘客数量,truck类增加载重吨数.......

classVehicle

intyear,month,day;

intprice;

Vehicle()

cout<

pleaseinputdate:

year"

"

month"

day"

cin>

year;

month;

day;

pleaseinputprice:

price;

Vehicle"

virtualvoidprint1()

date:

year<

month<

day<

price:

price<

classCar:

publicVehicle

intamount;

Car()

pleaseinputamountofpassager:

amount;

car"

voidprint2()

print1();

amount:

amount<

classTruck:

intton;

Truck()

pleaseinputton:

ton;

truck"

voidprint3()

ton:

ton<

VehicleV1;

V1.print1();

CarC1;

C1.print2();

TruckT1;

T1.print3();

(7-4)完整定义字符串类string,使用动态分配内存机制实现字符串存储,定义构造函数,析构函数,重载运算符“=”,"

+"

“+=”实现两个字符串的赋值,连接等功能。

classString

String();

String(char*t);

String(String&

t);

~String();

Stringoperator=(Stringt);

Stringoperator+(Stringt);

Stringoperator+=(Stringt);

voidShow()

p<

char*p;

String:

:

String()

p=newchar[1];

*p='

\0'

String(char*t)

p=newchar[strlen(t)+1];

strcpy(p,t);

String(String&

t)

p=newchar[strlen(t.p)+1];

strcpy(p,t.p);

StringString:

operator+(Stringt)

char*pt;

pt=newchar[strlen(p)+strlen(t.p)+1];

strcpy(pt,p);

strcat(pt,t.p);

Stringtemp(pt);

delete[]pt;

returntemp;

operator+=(Stringt)

char*i;

i=newchar[strlen(p)+1];

strcpy(i,p);

delete[]p;

p=newchar[strlen(t.p)+strlen(p)+1];

strcpy(p,i);

strcat(p,t.p);

return*this;

~String()

delete[]p;

operator=(Stringt)

if(this==&

return*this;

voidmain()

Strings1("

hello!

);

Strings2("

when."

Strings3;

s3=s2+s1;

s3+=s2;

s2=s1;

s3.Show();

s2.Show();

(8-4)根据程序定义一个求3个数之间最大值的函数模板max。

iostream.h>

string.h>

template<

typenameT>

Tmax(Ta,Tb,Tc)

return(a>

b?

a:

b)>

c?

(a>

b):

max(11,29,22)<

max(3.14f,28.3f,6.7f)<

max('

c'

'

b'

a'

)<

(5)有一个学生类student,包括学生姓名、成绩,设计一个友元函数,输出成绩对应的等级:

大于等于90:

优;

80~90:

良;

70~79:

中;

60!

69:

及格;

小于60:

不及格。

classstudent

charname[10];

intdeg;

student(charna[],intd)

strcpy(name,na);

deg=d;

}

friendvoidtrans(student&

s)

if(s.deg>

=90)

s.name<

s.deg<

优"

elseif(s.deg>

=80)

良"

=70)

中"

=60)

及格"

else

不及格"

voidmain()

studentst[]={student("

王华"

李明"

张伟"

62),student("

孙强"

88)};

输出结果:

姓名"

成绩"

等级"

for(inti=0;

i++)

trans(st[i]);

(6)编写一个程序,设计一个Point类,包括学号、姓名和成绩等私有数据成员,不含任何成员函数,只将main()设置为该类的友元函数。

classpoint

intno;

charname[10];

intdeg;

public:

friendvoidmain();

pointp;

输入学号:

p.no;

姓名:

p.name;

成绩:

p.deg;

输出结果"

学生:

p.name<

学号:

p.no<

成绩为:

p.deg<

(7)创建一个学生类,包括学号和成绩,编程输入和显示学生信息,建立一个人类,包括姓名,性别,年龄,并作为学生的基类.

#include<

classPerson

charsex;

intage;

voidinput()

请输入姓名:

name;

请输入性别:

sex;

请输入年龄:

age;

姓名:

性别:

sex<

年龄:

age<

classStudent:

publicPerson

charsno[10];

intscore;

Person:

input();

请输入学号:

sno;

请输入成绩:

score;

display();

学号;

sno<

成绩:

score<

Students1;

s1.input();

s1.display();

(8)按要求编程:

某学校教授和讲师的月工资计算办法规定如下:

教授每月固定工资为4000元。

讲师每月工资与讲课学时数有关,计算方法是每学时50元,另加补助1000元。

编程显示某个教员的月工资数目。

classTeacher

virtualintSalary()=0;

virtualvoidPrint(int)=0;

classProfessor:

publicTeacher

charname[20];

intlessons;

Professor()

cout<

请输入姓名:

cin>

//字符串中不能有空格

intSalary()

return4000;

voidPrint(intmoney)

职称:

教授姓名:

薪水:

money<

endl<

classLecturer:

Lecturer()

请输入课时:

lessons;

return(1000+lessons*50);

讲师姓名:

薪水:

intmoney=0;

Professort;

money=t.Salary();

t.Print(money);

Lecturerl;

money=l.Salary();

l.Print(money);

(9)1.给定一个int型数n,编程实现按不同进制输出,包括十进制数、八进制数、十六进制数。

要求使用纯虚函数print()。

十进制数(dec)十六进制数(hex)八进制数(oct)

math.h>

charch[16]={'

0'

1'

2'

3'

4'

5'

6'

7'

8'

9'

A'

B'

C'

D'

E'

F'

},output[20];

intinput,temp,n,i=0;

请输入你想要转换的十进制数:

input;

请输入要转换的进制(2<

=n<

=16):

n;

input=fabs(input);

while(input)

他一边说一边笑。

尖尖的铅笔闪闪的星星蓝蓝的天空temp=input%n;

input=input/n;

output[i++]=ch[temp];

温暖的春天炎热的夏天凉爽的秋天寒冷的冬天}

“越”的使用output[i]='

母亲着急认真.得意主意同意因为阳光办法亲情爱情cout<

转换为"

n<

进制后为:

提手旁:

找、扫、把、拉for(;

i>

=0;

i--)

一(间)书房一(群)羊一(个)人一(头)牛cout<

output[i];

(收)——(放)(去)——(来、回)死——(活)(答)——(问)return0;

2、仔细观察画面,弄懂图意。

一(孔)石桥一(只)船一(颗)枣一(把)伞两个或两个以上的函数,具有相同的函数名,但是形参的个数或者类型不同,编译器根据实参和形参的类型及个数进行最佳匹配,自动确定调用哪个函数,这就是函数的重载。

重写和重载,他们其实是实现类型多态(同一种方法,不同的对象会产生不同的结果)特性的基本技术之一.重写也叫做覆写,它存在与有继承关系的子类中,是在子类中重新定义父类具有相同方法签名的函数,使之有不同的实现.要理解重写和重载的本质区别,实现要理解什么叫做具有相同签名的方法.其实很简单,就是指相同的方法名,相同的参数列表(参数个数以及相同位置的参数类型相同)以及相同的函数返回值.重写的一个重要特征就是必须要求方法的签名相同.重写的基本特征:

1.是一种运行时多态,又称为动态联编,只有在运行时才知道调用的是哪个方法.

2.存在于有继承关系的子类中,只有被virtual和abstract标记的虚方法和抽象方法才能被直接重写.3.用关键字override来标记.

4.重写这个方法的类的父类中有一个与这个方法签名相同的方法.

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2