c++经典课后习题整理.docx
- 文档编号:10994338
- 上传时间:2023-02-24
- 格式:DOCX
- 页数:16
- 大小:17.94KB
c++经典课后习题整理.docx
《c++经典课后习题整理.docx》由会员分享,可在线阅读,更多相关《c++经典课后习题整理.docx(16页珍藏版)》请在冰豆网上搜索。
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; (); (); return0; } (2)设计一个学生类Student,拥有的数据成员是学号、姓名、电话号码、所属院系,成员函数包括访问和修改这些属性,在main()中定义对象,并输出相关信息 #include #include usingnamespacestd; classStudentcore { 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; (); CarC1; (); TruckT1; (); 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+1]; strcpy(p,; } StringString: : operator+(Stringt) { char*pt; pt=newchar[strlen(p)+strlen+1]; strcpy(pt,p); strcat(pt,; Stringtemp(pt); delete[]pt; returntemp; } StringString: : operator+=(Stringt) { char*i; i=newchar[strlen(p)+1]; strcpy(i,p); delete[]p; p=newchar[strlen+strlen(p)+1]; strcpy(p,i); strcat(p,; return*this; } String: : ~String() { delete[]p; } StringString: : operator=(Stringt) { if(this==&t) return*this; delete[]p; p=newchar[strlen+1]; strcpy(p,; return*this; } voidmain() { Strings1("hello! "); Strings2("when."); Strings3; s3=s2+s1; s3+=s2; s2=s1; (); (); } (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>=90) cout<<<<""<<<<""<<"优"< elseif>=80) cout<<<<""<<<<""<<"良"< elseif>=70) cout<<<<""<<<<""<<"中"< elseif>=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>>; cout<<"姓名: "; cin>>; cout<<"成绩: "; cin>>; 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; (); (); } (8)按要求编程: 某学校教授和讲师的月工资计算办法规定如下: 教授每月固定工资为4000元。 讲师每月工资与讲课学时数有关,计算方法是每学时50元,另加补助1000元。 编程显示某个教员的月工资数目。 #include<> classTeacher { public: virtualintSalary()=0; virtualvoidPrint(int)=0; }; classProfessor: publicTeacher { private: charname[20]; intlessons; public: Professor() { cout<<"请输入姓名: "; cin>>name;定一个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+ 经典 课后 习题 整理
![提示](https://static.bdocx.com/images/bang_tan.gif)