《面向对象程序设计(C++)实验报告.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计(C++)实验报告.docx(17页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、面向对象程序设计(C+)实验报告 面对对象程序设计 (C+) 试验报告 姓名: 学院:数学与计算机学院 班级:10级计算机大类班 学号: 试验5 单继承 一、试验目的 1驾驭派生的类别与方式; 2了解派生类中如何运用基类的成员、基类成员在派生类中的访问限制; 3. 驾驭继承中构造函数和析构函数的调用过程。 二、试验内容及步骤 1. 给出一个Document类,从Document派生出Book类,增加PageCount变量。在主函数中进行测试,创建Book类对象并进行初始化,输出书名和页数。 2. 设计一个单基继承的类层次程序,利用Person类派生出Student类,增加属性xh(学号),Pe
2、rson类中至少有姓名、年龄等数据成员,成员函数中构造函数对其初始化,析构函数释放相应存储单元,输出函数输出其数据成员的值,其它成员函数依据须要添加,在主函数中进行测试。 3. 设计一个人员类person和一个日期类date,由人员类派生出学生类student和老师类professor,学生类和老师类的数据成员birthday为日期类。在主函数中进行测试。 三、试验源程序和运行结果 试验(一)源程序: #include<iostream> #include<string> using namespace std; class Document public: Docum
3、ent(); Document(); Document(char*name); char *Name; void PrintNameOf(); ; Document:Document(char*name) Name=new charstrlen(name+1); strcpy(Name,name); Document:Document() delete Name; void Document:PrintNameOf() cout<<Name<<endl; class Book:public Document public: int PageCount; Book(cha
4、r *a,int b):Document(a) PageCount=b; ; void main() char BookName20; int n; cout<<“请输入书名:“<<endl; cin>>BookName; cout<<“请输入书的页数:“<<endl; cin>>n; Book b(BookName,n); cout<<“书名为:“<<b.Name<<endl; cout<<“页数为:“<<b.PageCount<<endl; 运行结
5、果: 试验(二)源程序: #include<iostream> #include<string> using namespace std; class person public: person() name=“张三“; age=0; person(string c,int a) name=c; age=a; person() void setname(string c) name=c; string getname() return name; void setage(int a) age=a; int getage() return age; private: st
6、ring name; int age; ; class student:public person public: student() xh=0; student(int d) xh=d; student(string c,int a,int d):person(c,a) xh=d; student() void setxh(int d) xh=d; int getxh() return xh; private: int xh; ; void main() string c; cout<<“请输入学生的姓名:n“; cin>>c; cout<<“请输入学生的
7、年龄:n“; int a; cin>>a; cout<<“请输入学生的学号:n“; int d; cin>>d; student n(c,a,d); cout<<“请输入学生的姓名为: “<<n.getname()<<endl; cout<<“请输入学生的年龄为: “<<n.getage()<<endl; cout<<“请输入学生的学号为: “<<n.getxh()<<endl; 运行结果: 试验(三)源程序: #include<iostream
8、> using namespace std; class person public: person() name=“张三“; age=0; person(string c,int a) name=c; age=a; person() void setname(string c) name=c; string getname() return name; void setage(int a) age=a; int getage() return age; private: string name; int age; ; class date public: date() year=201
9、1; month=12; day=17; date() date(int y,int m,int d) year=y; month=m; day=d; int getyear() return year; int getmonth() return month; int getday() return day; private: int year; int month; int day; ; class student:public person public: student() / birthday.date(); student(int y,int m,int d):birthday(y
10、,m,d) student() void getbirthday() cout<<“学生的生日为:n“; cout<<birthday.getyear()<<“年“<<birthday.getmonth()<<“月“<<birthday.getday()<<“日“<<endl; private: date birthday; ; class teacher:public person public: teacher() / birthday.date(); teacher(int y,int m,i
11、nt d):birthday(y,m,d) /birthday.date(y,m,d); teacher() void getbirthday() cout<<“老师的生日为:n“; cout<<birthday.getyear()<<“年“<<birthday.getmonth()<<“月“<<birthday.getday()<<“日“<<endl; private: date birthday; ; void main() cout<<“请输入学生的生日:“<<endl
12、; int y,m,d; cin>>y>>m>>d; student s(y,m,d); cout<<“请输入老师的生日:“<<endl; cin>>y>>m>>d; teacher t(y,m,d); s.getbirthday(); t.getbirthday(); 运行结果: 试验6 多继承 一、试验目的 1驾驭多基继承的运用,访问方法; 2理解类层次中访问规则; 3驾驭虚基类的定义及运用。二、试验内容及步骤 1. 定义一个学生类Student和老师类Teacher,学生类有姓名、学号、私有数
13、据成员,老师类有姓名、工作证号、职称、课程、周学时数。再定义一个助教类TA,继承学生类和老师类,该类可以运用学生类的全部数据成员,以及老师类的课程和周学时数的数据成员。要求:每个类供应自定义的构造函数和析构函数,并通过同名函数ShowInfo来显示全部数据成员的值。 2. 设计一个虚基类Person,包含姓名和年龄私有数据成员以及相关的成员函数;由它派生出领导类Leader,包含职务和部门私有数据成员以及相关的成员函数;再由Person派生出工程师类Engineer,包含职务和专业私有数据成员以及相关的成员函数;再由Leader和Engineer类派生出主任工程师类Chairman。并采纳相关
14、数据进行测试。 三、试验源程序和运行结果 试验(一)源程序: #include<iostream.h> #include<string.h> class Student protected: char s_name20; int id_s; public: Student(char *name,int id); void ShowInfo(); ; class Teacher protected: char t_name20; int id_t; char position30; char lesson30; int hour; public: Teacher(char
15、 *pos,int h); Teacher(char *name,int id,char *less,char *pos,int h); void ShowInfo(); ; class TA:public Student,public Teacher public: TA(char *name,char id,char *less,int h); void ShowInfo(); ; Student:Student(char *name,int id) strcpy(s_name,name); id_s=id; void Student:ShowInfo() cout<<“姓名:
16、“<<s_name<<t<<“学号:“<<id_s<<endl; Teacher:Teacher(char *less,int h) strcpy(lesson,less); hour=h; Teacher:Teacher(char *name,int id,char *less,char *pos,int h) strcpy(t_name,name); strcpy(lesson,less); strcpy(position,pos); id_t=id; hour=h; void Teacher:ShowInfo() cout<
17、;<“姓名:“<<t_name<<t<<“职工号:“<<id_t<<t<<“职称:“<<position<<t<<“课程:“<<lesson<<t<<“学时数:“<<hour<<endl; TA:TA(char *name,char id,char *less,int h):Student(name,id),Teacher(less,h) void TA:ShowInfo() Student:ShowInfo(); cou
18、t<<“课程:“<<lesson<<t<<“学时数:“<<hour<<endl; void main() TA ta(“刘九州“,14,“c+“,64); ta.ShowInfo(); 运行结果: 试验(二)源程序: #include<iostream.h> #include<string.h> class Person /虚基类person类 char name30; int age; public: Person(char *n,int a); void setname(char *n); vo
19、id setage(int a); char *getname(); int getage(); ; class Leader:virtual public Person /领导类 char job30; /职务 char dep30; /部门 public: Leader(char *jb,char *dp); void setjob(char *jb); void setdep(char *dp); char *getjob(); char *getdep(); ; class Engineer:virtual public Person /工程师类 char major30; /专业 c
20、har prof30; /职称 public: Engineer(char *maj,char *pf); void setmajor(char *maj); void setprof(char *pf); char *getmajor(); char *getprof(); ; class Chairman:public Leader,public Engineer /主任工程师类 public: Chairman(char *n,int a,char *jb,char *dp,char *maj,char *pf); void disp(); ; Person:Person(char *n
21、,int a) strcpy(name,n); age=a; void Person:setname(char *n) strcpy(name,n); void Person:setage(int a) age=a; char *Person:getname() return name; int Person:getage() return age; Leader:Leader(char *jb,char *dp):Person(“,30) strcpy(job,jb); strcpy(dep,dp); void Leader:setjob(char *jb) strcpy(job,jb);
22、void Leader:setdep(char *dp) strcpy(dep,dp); char *Leader:getjob() return job; char *Leader:getdep() return dep; Engineer:Engineer(char *maj,char *pf):Person(“,30) strcpy(major,maj); strcpy(prof,pf); void Engineer:setmajor(char *maj) strcpy(major,maj); void Engineer:setprof(char *pf) strcpy(prof,pf)
23、; char *Engineer:getmajor() return major; char *Engineer:getprof() return prof; Chairman:Chairman(char *n,int a,char *jb,char *dp,char *maj,char *pf):Person(n,a),Leader(jb,dp),Engineer(maj,pf) void Chairman:disp() cout<<“姓名:“<<getname()<<t<<“年龄:“<<getage()<<t<&
24、lt;“职务:“<<getjob()<<t<<“部门:“<<getdep()<<t<<“专业:“<<getmajor()<<t<<“职称:“<<getprof()<<endl; void main() Chairman c(“刘九州“,21,“厅长“,“财政厅“,“经济学“,“高级经济师“); c.disp(); 运行结果: 试验7 多态与虚函数 一、试验目的 1.理解多态的概念 2.驾驭如何用虚函数实现运行时多态 3.驾驭如何利用抽象类 二、试验内容及步骤 1.
25、 设计一个图形类(Shape),由它派生出三角形类(Triangle)、正方形类(Square)、圆形类(Circle),利用虚函数计算图形面积,并在主函数中进行测试。2. 定义一个老师类,由老师类派生出讲师、副教授、教授类。老师的工资分别由基本工资、课时费和津贴构成。假设讲师、副教授、教授的基本工资分别为800、900、1000元,课时费分别为每小时40、45、50元,津贴分别为1300、1800、2300。定义虚函数来计算老师的工资,并通过主函数来进行验证。三、试验源程序和运行结果 试验(一)源程序: #include<iostream> using namespace std
26、; class Shape public: virtual float area() return 0.0; ; class Triangle:public Shape public: Triangle() bc=1.0;h=1.0; Triangle(float bc,float h) this->bc=bc;this->h=h; bool setbc(float a) if(a>0)bc=a; float getbc() return bc; bool setg(float b) if(b>0)h=b; float getg() return h; float ar
27、ea() return bc*h/2; protected: float bc,h; ; class Square:public Shape public: Square() l=1.0; Square(float m) this->l=m; bool setbc(float c) if(c>0)l=c; float getbc() return l; float area() return l*l; protected: float l; ; class Circle:public Shape public: Circle() radius=1.0; Circle(float R
28、) this->radius=R; bool setRadius(float r) if(r>0)radius=r; float getRadius() return radius; float area() return 3.14159*radius*radius; protected: float radius; ; void displayShapeArea(Shape *p) cout<<“图形面积为:“<<p->area()<<endl; void main() Shape *p1,*p2,*p3; Triangle T(15.0
29、,10.0); Square S(10.0); Circle C(10.0); p1=T; p2=S; p3=C; displayShapeArea(p1); displayShapeArea(p2); displayShapeArea(p3); 运行结果: 试验(二)源程序: #include<iostream> using namespace std; class teacher public: virtual float wage() return 0.0; ; class lecturer:public teacher public: lecturer() WorkHour
30、s=1.0; lecturer(float WorkHours) this->WorkHours=WorkHours; bool setWorkHours(float h) if(h>0)WorkHours=h; float getWorkHours() return WorkHours; float wage() return (800+40*WorkHours+1300); protected: float WorkHours; ; class AssociateProfessor:public teacher public: AssociateProfessor() Work
31、Hours=1.0; AssociateProfessor(float WorkHours) this->WorkHours=WorkHours; bool setWorkHours(float h) if(h>0)WorkHours=h; float getWorkHours() return WorkHours; float wage() return (900+45*WorkHours+1800); protected: float WorkHours; ; class Professor:public teacher public: Professor() WorkHour
32、s=1.0; Professor(float WorkHours) this->WorkHours=WorkHours; bool setWorkHours(float h) if(h>0)WorkHours=h; float getWorkHours() return WorkHours; float wage() return (1000+50*WorkHours+2300); protected: float WorkHours; ; void displayWage(teacher *s) cout<<“工资为:“<<s->wage()<
33、<endl; void main() teacher *s1,*s2,*s3; lecturer L(30.5); AssociateProfessor A(20.6); Professor P(10.5); s1=L; s2=A; s3=P; displayWage(s1); displayWage(s2); displayWage(s3); 运行结果: 试验8 运算符重载 一、试验目的 驾驭C+中运算符重载的机制和运算符重载的方式; 二、试验内容及步骤 1. 编写一个简洁复数类Scomplex,要求用友元函数重载“+”、“-”运算符,用成员函数重载“=”运算符,使之能够实现整数或浮点
34、数和复数的加法和减法,并且进行测试。 2. 空间一点p的坐标为(x,y,z),其中x,y,z为整数。编写点类Point3D,定义空间两点之间的加”+”,减”-”运算为相应三个坐标值分别进行加、减运算,要求实现空间两点之间的加”+”减”-”赋值”=”运算,空间两点间的比较”= =”运算。要求编写Point3D类的声明定义和测试程序。 3. 设计一个时间类Time,包括时、分、秒等私有数据成员。重载“+”和“-”运算符以实现时间的加法和减法运算,并进行测试。 三、试验源程序和运行结果 试验(一)源程序: #include<iostream.h> class Scomplex priva
35、te: double real,imag; public: Scomplex() real=0; /实部 imag=0; /虚部 Scomplex(double x,double y) real=x; imag=y; Scomplex operator =(Scomplex s); double getreal() return real; double getimag() return imag; friend Scomplex operator+(int i,Scomplex s); friend Scomplex operator+(double d,Scomplex s); frien
36、d Scomplex operator-(int i,Scomplex s); friend Scomplex operator-(double d,Scomplex s); ; Scomplex Scomplex:operator =(Scomplex s) if(this=s) return *this; real=s.real; imag=s.imag; return *this; Scomplex operator+(int i,Scomplex s) Scomplex t; t.real=i+s.real; t.imag=s.imag; return t; Scomplex oper
37、ator+(double d,Scomplex s) Scomplex t; t.real=d+s.real; t.imag=s.imag; return t; Scomplex operator-(int i,Scomplex s) Scomplex t; t.real=i-s.real; t.imag=s.imag; return t; Scomplex operator-(double d,Scomplex s) Scomplex t; t.real=d-s.real; t.imag=s.imag; return t; void main() Scomplex s1(3.4,5.2),s
38、2; s2=1+s1; cout<<“复数s2是:(“<<s2.getreal()<<,<<s2.getimag()<<)<<endl; s2=6.2+s1; cout<<“复数s2是:(“<<s2.getreal()<<,<<s2.getimag()<<)<<endl; s2=5-s1; cout<<“复数s2是:(“<<s2.getreal()<<,<<s2.getimag()<<)<
39、;<endl; s2=3.2-s1; cout<<“复数s2是:(“<<s2.getreal()<<,<<s2.getimag()<<)<<endl; 运行结果: 试验(二)源程序: #include<iostream.h> class Point3D public: Point3D() x=1; y=1; z=1; Point3D(int a,int b,int c) x=a; y=b; z=c; int getx() return x; int gety() return y; int getz()
40、return z; Point3D operator =(Point3D p); Point3D operator +(Point3D p); Point3D operator -(Point3D p); bool operator =(Point3D p); private: int x,y,z; ; Point3D Point3D:operator =(Point3D p) if(this=p) return *this; x=p.x; y=p.y; z=p.z; return *this; Point3D Point3D:operator +(Point3D p) Point3D t;
41、t.x=x+p.x; t.y=y+p.y; t.z=z+p.z; return t; Point3D Point3D:operator -(Point3D p) Point3D t; t.x=x-p.x; t.y=y-p.y; t.z=z-p.z; return t; bool Point3D:operator =(Point3D p) if(x=p.xy=p.yz=p.z) return true; else return false; void main() Point3D p1(1,2,3),p2(1,2,3),p3,p4; p3=p1+p2; cout<<“两点相加后为:
42、(“<<p3.getx()<<“,“<<p3.gety()<<“,“<<p3.getz()<<“)“<<endl; p4=p2-p1; cout<<“两点相减后为: (“<<p4.getx()<<“,“<<p4.gety()<<“,“<<p4.getz()<<“)“<<endl; if(p1=p2) cout<<“p1=p2“<<endl; else cout<<“p1!=p2“&
43、lt;<endl; 运行结果: 试验(三)源程序: #include<iostream.h> class Time public: Time() hour=0; minute=0; second=0; Time(int h,int m,int s) hour=h; minute=m; second=s; void setHour(int h) hour=h; void setMinute(int m) minute=m; void setSecond(int s) second=s; int getHour() return hour; int getMinute() ret
44、urn minute; int getSecond() return second; void displayTime() cout<<hour<<“:“<<minute<<“:“<<second<<endl; Time operator + (Time); Time operator - (Time); private: int hour; int minute; int second; ; Time Time:operator+(Time t) int carry,hh,mm,ss; ss=second+t.getSecond(); if(ss>60) ss-=60; carry=1; else carry=0; mm=minute+t.getMinute()+carry; if(mm>60) mm-=60; carry=1; else carry=0; hh=hour+t.getHour()+carry; if(hh>24) hh-=24; Time tt(hh,mm,ss); return(tt); Time Time:operator-(Time t) int borrow,hh,mm,ss; ss=second-t.getSecond(); if(ss&l