面向对象程序设计(C)实验报告.doc
面向对象程序设计(C+)实验报告姓名:刘九州 学院:数学与计算机学院 班级:10级计算机大类三班 学号:100511314实验5 单继承一、实验目的1掌握派生的类别与方式; 2了解派生类中如何使用基类的成员、基类成员在派生类中的访问控制; 3. 掌握继承中构造函数和析构函数的调用过程。二、实验内容及步骤1. 给出一个Document类,从Document派生出Book类,增加PageCount变量。在主函数中进行测试,创建Book类对象并进行初始化,输出书名和页数。 2. 设计一个单基继承的类层次程序,利用Person类派生出Student类,增加属性xh(学号),Person类中至少有姓名、年龄等数据成员,成员函数中构造函数对其初始化,析构函数释放相应存储单元,输出函数输出其数据成员的值,其它成员函数根据需要添加,在主函数中进行测试。3. 设计一个人员类person和一个日期类date,由人员类派生出学生类student和教师类professor,学生类和教师类的数据成员birthday为日期类。在主函数中进行测试。三、实验源程序和运行结果实验(一)源程序:#include<iostream>#include<string>using namespace std;class Documentpublic:Document();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 Documentpublic:int PageCount;Book(char *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;运行结果:实验(二)源程序:#include<iostream>#include<string>using namespace std;class personpublic: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 student:public personpublic: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<<"请输入学生的年龄: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>using namespace std;class personpublic: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 datepublic:date()year=2011;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 personpublic:student()/birthday.date();student(int y,int m,int d):birthday(y,m,d)student()void getbirthday()cout<<"学生的生日为:n"cout<<birthday.getyear()<<"年"<<birthday.getmonth()<<"月"<<birthday.getday()<<"日"<<endl;private:date birthday;class teacher:public personpublic:teacher()/birthday.date();teacher(int y,int m,int 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;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,学生类有姓名、学号、私有数据成员,教师类有姓名、工作证号、职称、课程、周学时数。再定义一个助教类TA,继承学生类和教师类,该类可以使用学生类的全部数据成员,以及教师类的课程和周学时数的数据成员。要求:每个类提供自定义的构造函数和析构函数,并通过同名函数ShowInfo来显示全部数据成员的值。2. 设计一个虚基类Person,包含姓名和年龄私有数据成员以及相关的成员函数;由它派生出领导类Leader,包含职务和部门私有数据成员以及相关的成员函数;再由Person派生出工程师类Engineer,包含职务和专业私有数据成员以及相关的成员函数;再由Leader和Engineer类派生出主任工程师类Chairman。并采用相关数据进行测试。三、实验源程序和运行结果实验(一)源程序:#include<iostream.h>#include<string.h>class Studentprotected:char s_name20;int id_s;public:Student(char *name,int id);void ShowInfo();class Teacherprotected:char t_name20;int id_t;char position30;char lesson30;int hour;public:Teacher(char *pos,int h);Teacher(char *name,int id,char *less,char *pos,int h);void ShowInfo();class TA:public Student,public Teacherpublic: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<<"姓名:"<<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<<"姓名:"<<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();cout<<"课程:"<<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);void 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; /专业char 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,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);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);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'<<"职务:"<<getjob()<<'t'<<"部门:"<<getdep()<<'t'<<"专业:"<<getmajor()<<'t'<<"职称:"<<getprof()<<endl;void main()Chairman c("刘九州",21,"厅长","财政厅","经济学","高级经济师");c.disp();运行结果: 实验7 多态与虚函数一、实验目的二、实验内容及步骤1. 设计一个图形类(Shape),由它派生出三角形类(Triangle)、正方形类(Square)、圆形类(Circle),利用虚函数计算图形面积,并在主函数中进行测试。2. 定义一个教师类,由教师类派生出讲师、副教授、教授类。教师的工资分别由基本工资、课时费和津贴构成。假设讲师、副教授、教授的基本工资分别为800、900、1000元,课时费分别为每小时40、45、50元,津贴分别为1300、1800、2300。定义虚函数来计算教师的工资,并通过主函数来进行验证。三、实验源程序和运行结果实验(一)源程序:#include<iostream>using namespace std;class Shapepublic:virtual float area()return 0.0;class Triangle:public Shapepublic: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 area()return bc*h/2;protected:float bc,h;class Square:public Shapepublic: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 Shapepublic:Circle()radius=1.0;Circle(float R) 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,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 teacherpublic:virtual float wage()return 0.0;class lecturer:public teacherpublic:lecturer()WorkHours=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 teacherpublic:AssociateProfessor()WorkHours=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 teacherpublic:Professor()WorkHours=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()<<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,要求用友元函数重载“+”、“-”运算符,用成员函数重载“=”运算符,使之能够实现整数或浮点数和复数的加法和减法,并且进行测试。 2. 空间一点p的坐标为(x,y,z),其中x,y,z为整数。编写点类Point3D,定义空间两点之间的加”+”,减”-”运算为相应三个坐标值分别进行加、减运算,要求实现空间两点之间的加”+”减”-”赋值”=”运算,空间两点间的比较”= =”运算。要求编写Point3D类的声明定义和测试程序。3. 设计一个时间类Time,包括时、分、秒等私有数据成员。重载“+”和“-”运算符以实现时间的加法和减法运算,并进行测试。三、实验源程序和运行结果 实验(一)源程序:#include<iostream.h>class Scomplex private: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); friend 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 operator+(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),s2;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()<<')'<<endl;s2=3.2-s1;cout<<"复数s2是:("<<s2.getreal()<<','<<s2.getimag()<<')'<<endl;运行结果:实验(二)源程序:#include<iostream.h>class Point3Dpublic: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()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;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.x&&y=p.y&&z=p.z)return true;elsereturn false;void main()Point3D p1(1,2,3),p2(1,2,3),p3,p4;p3=p1+p2;cout<<"两点相加后为:("<<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;elsecout<<"p1!=p2"<<endl;运行结果:实验(三)源程序:#include<iostream.h>class Timepublic: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()return 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<0)ss+=60;borrow=1;else borrow=0;mm=minute-t.getMinute()-borrow;if(mm<0)mm+=60;borrow=1;else borrow=0;hh=hour-t.getHour()-borrow;if(hh<0) hh+=24;Time tt(hh,mm,ss);return(tt);void main()Time t1(13,2,40