《C++面向对象大题》由会员分享,可在线阅读,更多相关《C++面向对象大题(11页珍藏版)》请在金锄头文库上搜索。
1、1. 编写一个函数,用来实现对3 个数按由小到大的顺序排序,并在主函数中调用此函数。要求函数的形参用以下两种形式实现:(1)使用指针形参(2)使用引用形参(1)#include void swap(int* ,int*,int*);void main()int a,b,c;coutvv请输入三个正整数vvendl; cinabc;swap(&a,&b,&c);coutvva=vvavv,b=vvbvv,c=vvcvvendl;void swap(int*x,int*y,int*z)int t;if(*x*y)if(*x*z)if(*y*z)t=*x;t=*x;t=*y;*x=*y;*x=*z;
2、*y=*z;*y=t;*z=t;*z=t;(2)#include viostream.hvoid main()int a,b,c;coutvv请输入三个正整数vvendl;cinabc;swap(a,b,c);coutvva=vvavv,b=vvbvv,c=vvc;void swap(int&x,int&y,int&z)int t;if(xy)if(xz)if(yz) t=x;t=x;t=y;x=y;x=z;y=z;y=t;z=t;z=t;2. 使用重载函数求不同类型(int、double )的三个数的最大值(含缺省参数的函数)#include int swap(int,int,int=84)
3、;double swap(double,double,double); void main()coutswap(5,75)endl; coutswap(5.2,6.6,2.3)endl;double swap(double m,double n,double s) if(mn)m=n;if(ms)int swap(int x,int y,int z)if(xy)x=y;if(xz)x=z; m=s; return x; return m; 3. 使用函数模板求不同类型的三个数的最大值(含缺省参数的函数);#include using namespace std; template T max(
4、T a,T b,T c) if(ab)a=b;if(ac)a=c;coutvv最大值为vvavvendl;return 0;void main()max(78,20,48);max(80.7,7.7,6.0);4用函数模板方式设计一个函数模板sort,采用直接插入排序方式对数据进行排序,并对整数序列和字符序列排序。#include using namespace std;template void bubble(T *item,int n)T t;for(int i=1;i=i;j-)if(itemj-1itemj)t=itemj-1;itemj-1=itemj;itemj=t;int mai
5、n()char s=myfriend;bubble(s,strlen(s);coutThe sorted string is :sendl;int d=3,2,78,8,6,8,9;bubble(d,7);coutThe sorted numbers are:;for(int i=0;i7;i+) coutdiendl;return 0;5定一个学生类student,它含有私有数据成员:学号、姓名、年龄,及公有成员函数:setvalue()(用于设置三个私有数据成员的值)print()(用于输出三个数据成员的值)及构造函数,在主函数中定义student类的两个对象stu1和stu2,对stud
6、ent类进行测试。#include#includeclass Studentprivate:char number80;char name80;int age;public:void setvalue(char*nu,char *na,int ag) strcpy(number,nu);strcpy(name,na);age=ag;void print()coutnumberendl;coutnameendl; coutageendl;Student();void main()Student stu1;stu1.setvalue(090502011,Tom,34);stu1.print();S
7、tudent stu2;stu2.setvalue(09050206,Lily,96);stu2.print();6. 定义一个图形类,其中有保护类型的成员数据:高度好宽度,一个公有的构造函数。由该图形类建立两个派生类:矩形类和等腰三角形类。在每个派生类中都包含一个函数Area(),分别用来计算矩形和等腰三角形的面积。#include#includeusing namespace std;class Graphprotected:int height,width;public:Graph(int hei,int wid)height=hei;width=wid;class Rectangle:
8、public Graph ( 继承)public:Rectangle(int hei,int wid):Graph(hei,wid)int area()return height*width;void print() coutheight*widthendl;class Triangle:public Graphprivate:int thethird;double s;public:Triangle(int hei,int wid,int the):Graph(hei,wid),thethird(the) double area()s=(height+width+thethird)/2;re
9、turn sqrt(s*(s-height)*(s-width)*(s-thethird); void print() coutsqrt(s*(s-height)*(s-width)*(s-thethird)endl;void main()Rectangle re(6,8);re.print();Triangle tr(6,8,10);tr.area();tr.print ();7. 定义一个人员类CPeople,其属性有(保护类型):姓名、性别、年龄;从中派生出学生 类CStudent,添加属性:学号、入学时间和入学成绩;从CPeople类再派生出教师类 CTeacher,添加属性:职务、部
10、门、工作时间;由Cstudent类派生究生类CGraduate添 加属 性:研究方 向和导师, 由 CGraduate 和 CTeacher 共同派生出在职研 究生类 CGradOnWork,分别定义其中的构造函数和输出函数。主函数中定义各种类的对象,并完 成测试。#include #includeusing namespace std;class CPeopleprotected:string name;char sex;int age;public:CPeople(char *na,char s,int ag):name(na),sex(s),age(ag)coutv调用 cpeople
11、构造函数 endl;void print()coutname sex age endl;class CStudent:public CPeopleprotected:string number;string time;double score;public:CStudent(char*na,chars,intag,char*nu,char*ti,double sc):CPeople(na,s,ag),number(nu),time(ti),score(sc)cout 调 用 cstudent 的 构 造 函 数 endl;void print()CPeople:print();coutnumb
12、er time scoreendl;class CTeacher:public CPeopleprivate:string position;string section;int worktime;public:CTeacher(char *na,char s,int ag,char *pos,char *se,int wo):CPeople(na,s,ag),position(pos),section(se),worktime(wo)coutvv调用 cteacher 的构造函数vvendl;void print()CPeople:print();coutvvpositionvv vvsec
13、tionvv vvworktimevvendl;class CGraduate:public CStudentprotected:string about;string teacher;public:CGraduate(char *na,char s,int ag,char *nu,char *ti,double sc,char *ab,char *te):CStudent(na,s,ag,nu,ti,sc),about(ab),teacher(te)coutvv调用 CGraduate 的构造函数vvendl;void print()CStudent:print();coutvvaboutvv vvteachervvendl;class CGradOnWork:public CGraduate,public CTeacherpublic:CGradOnWork(char