C++面向对象大题.doc_第1页
C++面向对象大题.doc_第2页
C++面向对象大题.doc_第3页
C++面向对象大题.doc_第4页
C++面向对象大题.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1. 编写一个函数,用来实现对3个数按由小到大的顺序排序,并在主函数中调用此函数。要求函数的形参用以下两种形式实现:(1)使用指针形参(2)使用引用形参(1)#include void swap(int* ,int*,int*);void main()int a,b,c;cout请输入三个正整数abc;swap(&a,&b,&c);couta=a,b=b,c=c*y) if(*x*z) if(*y*z) t=*x; t=*x; t=*y;*x=*y; *x=*z; *y=*z;*y=t; *z=t; *z=t; (2)#include void main()int a,b,c;cout请输入三个正整数abc; swap(a,b,c);couta=a,b=b,c=y) 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);double swap(double,double,double);void main()coutswap(5,75)endl;coutswap(5.2,6.6,2.3)endl;int swap(int x,int y,int z) double swap(double m,double n,double s) if(xy) if(mn) x=y; m=n; if(xz) if(ms) x=z; m=s; return x; return m; 3. 使用函数模板求不同类型的三个数的最大值(含缺省参数的函数); #includeusing namespace std;templateT max(T a,T b,T c)if(ab)a=b;if(ac)a=c;cout最大值为aendl;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 main()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,对student类进行测试。义#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();Student 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:public Graph ( 继承)public:Rectangle(int hei,int wid):Graph(hei,wid)int area()return height*width;void print()coutheight*widthendl;class Triangle:public Graphprivate:intthethird;double s;public:Triangle(int hei,int wid,int the):Graph(hei,wid),thethird(the)double area()s=(height+width+thethird)/2;return 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, 添加属性:职务、部门、工作时间;由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)cout调用cpeople构造函数endl;void print()coutname sex age endl;class CStudent:public CPeopleprotected:string number;string time;double score;public:CStudent(char *na,char s,int ag,char *nu,char *ti,double sc):CPeople(na,s,ag),number(nu),time(ti),score(sc)cout调用cstudent的构造函数endl;void print()CPeople:print();coutnumber 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)cout调用cteacher的构造函数endl;void print()CPeople:print();coutposition section worktimeendl;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)cout调用CGraduate的构造函数endl;void print()CStudent:print();coutabout teacherendl;class CGradOnWork:public CGraduate,public CTeacherpublic:CGradOnWork(char*na,char s,int ag,char *nu,char *ti,double sc,char *ab,char *te,char *pos,char*se,int wo): CGraduate(na,s,ag,nu,ti,sc,ab,te),CTeacher(na,s,ag,pos,se,wo) cout调用cgadonwork的构造函数endl; void print()CGraduate:print();CTeacher:print();void main()CPeople p(tom,m,5);p.print();CStudent s(mike,m,20,0905020101,2009-09-05,534);s.print();CTeacher t(hua,m,45,daoshi,jisuanji,20);t.print();CGraduate g(zhang,m,25,0905020111,2006-09-05,580,shengwugongcheng,wang);g.print();CGradOnWork gw(qian,w,30,0305020113,2003-09-05,560,huaxue,li,doctor,huayao,5);gw.print();8. 编写一个程序,输入N个学生数据,包括学号、姓名、成绩,要求输出这些学生数据并计算平均分。提示:设计一个学生类Stud,除了包括no(学号)、name(姓名)和deg(成绩)数据成员外,有两个静态变量sum和num,分别存放总分和人数,另有两个普通成员函数setdata()和disp(),分别用于给数据成员赋值和输出数据成员的值,另有一个静态成员函数avg(),它用于计算平均分。在main()函数中定义了一个对象数组用于存储输入的学生数据。*#include #include #include using namespace std;class Studprivate: string no; string name; double score; static double sum; static int num;public:Stud()no= ;name= ;score=0;sum=0;num=0;void setdate() string n; string na; double sc; cinnnasc; no=n; name=na; score=sc; void disp() coutnotnametscoreendl; sum+=score; num+;static void avg() cout总分:sumt总人数:numn 平均分:sum/numendl;void main() size_t size; cout 请输入学生人数:size; Stud *s=new Studsize; assert(s!=NULL); for(int i=1;i=size;i+) si.setdate(); ; for(i=1;i=size;i+) si.disp(); ; si.avg();9. 设计一个学生类student,包括姓名和三门课程成绩,利用重载运算符”+“将所有学生的成绩相加放在一个对象中,再对该对象求各门课程的平均分。#include #include class student char name10; int deg1,deg2,deg3; public: student() student(char na,int d1,int d2,int d3) strcpy(name,na); deg1=d1;deg2=d2;deg3=d3; friend student operator+(student s1,student s2) static student st; st.deg1=s1.deg1+s2.deg1; st.deg2=s1.deg2+s2.deg2; st.deg3=s1.deg3+s2.deg3; return st; void disp() coutsetw(10)namesetw(5)deg1setw(5)deg2setw(5)deg3endl; friend void avg(student &s,int n) coutsetw(10)平均分 setw(5)s.deg1/nsetw(5)s.deg2/nsetw(5)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论