c++经典课后习题整理.docx
- 文档编号:2194363
- 上传时间:2023-05-02
- 格式:DOCX
- 页数:21
- 大小:19.49KB
c++经典课后习题整理.docx
《c++经典课后习题整理.docx》由会员分享,可在线阅读,更多相关《c++经典课后习题整理.docx(21页珍藏版)》请在冰点文库上搜索。
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 template { 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<
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 经典 课后 习题 整理