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

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

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

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

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

c++经典课后习题整理

(1)设计一个Car类,他的数据成员要能描述一辆汽车的品牌、型号、出厂年份和价格,成员函数包括提供合适的途径来访问数据成员,在main()函数中定义类的对象并调用相应的成员函数

#include

#include

usingnamespacestd;

classCar

{

private:

stringPingpai;

stringXinghao;

intYear;

intPrice;

public:

voidset()

{

cout<<"shuruxiangguanxinxi"<

cout<<"pingpai:

";

cin>>Pingpai;

cout<<"xinghao:

";

cin>>Xinghao;

cout<<"chuchangnianfen:

";

cin>>Year;

cout<<"jiage:

";

cin>>Price;

}

voidshow()

{

cout<<"pingpai:

"<

cout<<"xinghao:

"<

cout<<"chuchangnianfen:

"<

cout<<"jiage:

"<

}

};

intmain()

{

Carcar;

car.set();

car.show();

return0;

}

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

#include

#include

usingnamespacestd;

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

{

private:

stringxuehao;

stringname;

stringtel;

stringdepartment;

public:

Student(stringx,stringn,stringt,stringd)

{

xuehao=x;

name=n;

tel=t;

department=d;

}

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

{

xuehao=x;

name=n;

tel=t;

department=d;

}

voiddisplay()

{

cout<<"xuehao="<

cout<<"name="<

cout<<"tel="<

cout<<"department="<

}

};

intmain()

{

stringa,b,c,d;

cin>>a;

cin>>b;

cin>>c;

cin>>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)}中的最高分和最低分……

#include

#include

usingnamespacestd;

classstudent

{

private:

char*name;

doublescore;

public:

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

{

tmp=s[i];

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

s[i+1]=tmp;

}

cout<<"分数由高到低排列:

";

for(i=0;i<4;i++)

cout<

cout<<"最高分:

"<

cout<<"最低分:

"<

}

intmain()

{

students[4]={student("zhang",78),student("wang",80),student("li",92),student("chen",50)};

order(s);

}

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

#include

#include

usingnamespacestd;

classVehicle

{

private:

intyear,month,day;

intprice;

public:

Vehicle()

{

cout<<"pleaseinputdate:

";

cout<<"year"<<","<<"month"<<","<<"day"<<","<

cin>>year;

cin>>month;

cin>>day;

cout<<"pleaseinputprice:

";

cin>>price;

cout<<"Vehicle"<

}

virtualvoidprint1()

{

cout<<"date:

"<

cout<<"price:

"<

}

};

classCar:

publicVehicle

{

public:

intamount;

Car()

{

cout<<"pleaseinputamountofpassager:

";

cin>>amount;

cout<<"car"<

}

voidprint2()

{

print1();

cout<<"amount:

"<

}

};

classTruck:

publicVehicle

{

public:

intton;

Truck()

{

cout<<"pleaseinputton:

";

cin>>ton;

cout<<"truck"<

}

voidprint3()

{

print1();

cout<<"ton:

"<

}

};

intmain()

{

VehicleV1;

V1.print1();

CarC1;

C1.print2();

TruckT1;

T1.print3();

return0;

}

(7-4)完整定义字符串类string,使用动态分配内存机制实现字符串存储,定义构造函数,析构函数,重载运算符“=”,"+",“+=”实现两个字符串的赋值,连接等功能。

#include

#include

usingnamespacestd;

classString

{

public:

String();

String(char*t);

String(String&t);

~String();

Stringoperator=(Stringt);

Stringoperator+(Stringt);

Stringoperator+=(Stringt);

voidShow()

{

cout<

}

private:

char*p;

};

String:

:

String()

{

p=newchar[1];

*p='\0';

}

String:

:

String(char*t)

{

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

strcpy(p,t);

}

String:

:

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;

}

StringString:

:

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:

:

~String()

{

delete[]p;

}

StringString:

:

operator=(Stringt)

{

if(this==&t)

return*this;

delete[]p;

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

strcpy(p,t.p);

return*this;

}

voidmain()

{

Strings1("hello!

");

Strings2("when.");

Strings3;

s3=s2+s1;

s3+=s2;

s2=s1;

s3.Show();

s2.Show();

}

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

#include

#include

templateTmax(Ta,Tb,Tc)

{

return(a>b?

a:

b)>c?

(a>b?

a:

b):

c;

}

intmain()

{

cout<

cout<

cout<

return0;

}

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

大于等于90:

优;80~90:

良;70~79:

中;60!

69:

及格;小于60:

不及格。

#include

#include

classstudent

{

charname[10];

intdeg;

public:

student(charna[],intd)

{

strcpy(name,na);

deg=d;

}

friendvoidtrans(student&s)

{

if(s.deg>=90)

cout<

elseif(s.deg>=80)

cout<

elseif(s.deg>=70)

cout<

elseif(s.deg>=60)

cout<

else

cout<

}

};

voidmain()

{

studentst[]={student("王华",78),student("李明",92),student("张伟",62),student("孙强",88)};

cout<<"输出结果:

"<

cout<<"姓名"<<""<<"成绩"<<""<<"等级"<

for(inti=0;i<4;i++)

trans(st[i]);

}

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

#include

classpoint

{

intno;

charname[10];

intdeg;

public:

friendvoidmain();

};

voidmain()

{

pointp;

cout<<"输入学号:

";

cin>>p.no;

cout<<"姓名:

";

cin>>p.name;

cout<<"成绩:

";

cin>>p.deg;

cout<<"输出结果"<

cout<<"学生:

"<

cout<<"学号:

"<

cout<<"成绩为:

"<

}

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

#include

classPerson

{

charname[10];

charsex;

intage;

public:

voidinput()

{

cout<<"请输入姓名:

";

cin>>name;

cout<<"请输入性别:

";

cin>>sex;

cout<<"请输入年龄:

";

cin>>age;

}

voiddisplay()

{

cout<<"姓名:

"<

"<

"<

}

};

classStudent:

publicPerson

{

charsno[10];

intscore;

public:

voidinput()

{

Person:

:

input();

cout<<"请输入学号:

";

cin>>sno;

cout<<"请输入成绩:

";

cin>>score;

}

voiddisplay()

{

Person:

:

display();

cout<<"学号;"<

"<

}

};

voidmain()

{

Students1;

s1.input();

s1.display();

}

(8)按要求编程:

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

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

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

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

#include

classTeacher

{

public:

virtualintSalary()=0;

virtualvoidPrint(int)=0;

};

classProfessor:

publicTeacher

{

private:

charname[20];

intlessons;

public:

Professor()

{

cout<<"请输入姓名:

";

cin>>name;//字符串中不能有空格

}

intSalary()

{

return4000;

}

voidPrint(intmoney)

{

cout<<"职称:

教授姓名:

"<

"<

}

};

classLecturer:

publicTeacher

{

private:

charname[20];

intlessons;

public:

Lecturer()

{

cout<<"请输入姓名:

";

cin>>name;

cout<<"请输入课时:

";

cin>>lessons;

}

intSalary()

{

return(1000+lessons*50);

}

voidPrint(intmoney)

{

cout<<"职称:

讲师姓名:

"<

"<

}

};

voidmain()

{

intmoney=0;

Professort;

money=t.Salary();

t.Print(money);

Lecturerl;

money=l.Salary();

l.Print(money);

}

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

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

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

#include

#include

intmain()

{

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;

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

";

cin>>input;

cout<<"请输入要转换的进制(2<=n<=16):

";

cin>>n;

input=fabs(input);

while(input)

他一边说一边笑。

{

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

input=input/n;

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

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

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

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

";

提手旁:

找、扫、把、拉for(;i>=0;i--)

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

cout<

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

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

}

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

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

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

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

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

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

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

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

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