面向对象程序设计C++实验报告.docx

上传人:b****5 文档编号:7671824 上传时间:2023-01-25 格式:DOCX 页数:31 大小:281.11KB
下载 相关 举报
面向对象程序设计C++实验报告.docx_第1页
第1页 / 共31页
面向对象程序设计C++实验报告.docx_第2页
第2页 / 共31页
面向对象程序设计C++实验报告.docx_第3页
第3页 / 共31页
面向对象程序设计C++实验报告.docx_第4页
第4页 / 共31页
面向对象程序设计C++实验报告.docx_第5页
第5页 / 共31页
点击查看更多>>
下载资源
资源描述

面向对象程序设计C++实验报告.docx

《面向对象程序设计C++实验报告.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计C++实验报告.docx(31页珍藏版)》请在冰豆网上搜索。

面向对象程序设计C++实验报告.docx

面向对象程序设计C++实验报告

面向对象程序设计

(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

#include

usingnamespacestd;

classDocument

{

public:

Document(){};

~Document();

Document(char*name);

char*Name;

voidPrintNameOf();

};

Document:

:

Document(char*name)

{

Name=newchar[strlen(name+1)];

strcpy(Name,name);

}

Document:

:

~Document(){

delete[]Name;

}

voidDocument:

:

PrintNameOf()

{

cout<

}

classBook:

publicDocument

{

public:

intPageCount;

Book(char*a,intb):

Document(a)

{

PageCount=b;

}

};

voidmain()

{

charBookName[20];

intn;

cout<<"请输入书名:

"<

cin>>BookName;

cout<<"请输入书的页数:

"<

cin>>n;

Bookb(BookName,n);

cout<<"书名为:

"<

cout<<"页数为:

"<

}

运行结果:

实验

(二)源程序:

#include

#include

usingnamespacestd;

classperson{

public:

person(){

name="张三";

age=0;

}

person(stringc,inta){

name=c;

age=a;

}

~person(){}

voidsetname(stringc){

name=c;

}

stringgetname(){

returnname;

}

voidsetage(inta){

age=a;

}

intgetage(){

returnage;

}

private:

stringname;

intage;

};

classstudent:

publicperson

{

public:

student(){

xh=0;

}

student(intd){

xh=d;

}

student(stringc,inta,intd):

person(c,a){

xh=d;

}

~student(){}

voidsetxh(intd){

xh=d;

}

intgetxh(){

returnxh;

}

private:

intxh;

};

voidmain(){

stringc;

cout<<"请输入学生的姓名:

\n";

cin>>c;

cout<<"请输入学生的年龄:

\n";

inta;

cin>>a;

cout<<"请输入学生的学号:

\n";

intd;

cin>>d;

studentn(c,a,d);

cout<<"请输入学生的姓名为:

"<

cout<<"请输入学生的年龄为:

"<

cout<<"请输入学生的学号为:

"<

}

运行结果:

实验(三)源程序:

#include

usingnamespacestd;

classperson{

public:

person(){

name="张三";

age=0;

}

person(stringc,inta){

name=c;

age=a;

}

~person(){}

voidsetname(stringc){

name=c;

}

stringgetname(){

returnname;

}

voidsetage(inta){

age=a;

}

intgetage(){

returnage;

}

private:

stringname;

intage;

};

classdate{

public:

date(){

year=2011;

month=12;

day=17;

}

~date(){}

date(inty,intm,intd){

year=y;

month=m;

day=d;

}

intgetyear(){

returnyear;

}

intgetmonth(){

returnmonth;

}

intgetday(){

returnday;

}

private:

intyear;

intmonth;

intday;

};

classstudent:

publicperson{

public:

student(){

//birthday.date();

}

student(inty,intm,intd):

birthday(y,m,d)

{

}

~student(){}

voidgetbirthday(){

cout<<"学生的生日为:

\n";

cout<

}

private:

datebirthday;

};

classteacher:

publicperson{

public:

teacher(){

//birthday.date();

}

teacher(inty,intm,intd):

birthday(y,m,d)

{

//birthday.date(y,m,d);

}

~teacher(){}

voidgetbirthday(){

cout<<"老师的生日为:

\n";

cout<

}

private:

datebirthday;

};

voidmain(){

cout<<"请输入学生的生日:

"<

inty,m,d;

cin>>y>>m>>d;

students(y,m,d);

cout<<"请输入老师的生日:

"<

cin>>y>>m>>d;

teachert(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

#include

classStudent{

protected:

chars_name[20];

intid_s;

public:

Student(char*name,intid);

voidShowInfo();

};

classTeacher{

protected:

chart_name[20];

intid_t;

charposition[30];

charlesson[30];

inthour;

public:

Teacher(char*pos,inth);

Teacher(char*name,intid,char*less,char*pos,inth);

voidShowInfo();

};

classTA:

publicStudent,publicTeacher{

public:

TA(char*name,charid,char*less,inth);

voidShowInfo();

};

Student:

:

Student(char*name,intid){

strcpy(s_name,name);

id_s=id;

}

voidStudent:

:

ShowInfo(){

cout<<"姓名:

"<

"<

}

Teacher:

:

Teacher(char*less,inth){

strcpy(lesson,less);

hour=h;

}

Teacher:

:

Teacher(char*name,intid,char*less,char*pos,inth){

strcpy(t_name,name);

strcpy(lesson,less);

strcpy(position,pos);

id_t=id;

hour=h;

}

voidTeacher:

:

ShowInfo(){

cout<<"姓名:

"<

"<

"<

"<

"<

}

TA:

:

TA(char*name,charid,char*less,inth):

Student(name,id),Teacher(less,h){}

voidTA:

:

ShowInfo(){

Student:

:

ShowInfo();

cout<<"课程:

"<

"<

}

voidmain(){

TAta("刘九州",14,"c++",64);

ta.ShowInfo();

}

运行结果:

实验

(二)源程序:

#include

#include

classPerson{//虚基类person类

charname[30];

intage;

public:

Person(char*n,inta);

voidsetname(char*n);

voidsetage(inta);

char*getname();

intgetage();

};

classLeader:

virtualpublicPerson{//领导类

charjob[30];//职务

chardep[30];//部门

public:

Leader(char*jb,char*dp);

voidsetjob(char*jb);

voidsetdep(char*dp);

char*getjob();

char*getdep();

};

classEngineer:

virtualpublicPerson{//工程师类

charmajor[30];//专业

charprof[30];//职称

public:

Engineer(char*maj,char*pf);

voidsetmajor(char*maj);

voidsetprof(char*pf);

char*getmajor();

char*getprof();

};

classChairman:

publicLeader,publicEngineer{//主任工程师类

public:

Chairman(char*n,inta,char*jb,char*dp,char*maj,char*pf);

voiddisp();

};

Person:

:

Person(char*n,inta){

strcpy(name,n);

age=a;

}

voidPerson:

:

setname(char*n){

strcpy(name,n);

}

voidPerson:

:

setage(inta){

age=a;

}

char*Person:

:

getname(){

returnname;

}

intPerson:

:

getage(){

returnage;

}

Leader:

:

Leader(char*jb,char*dp):

Person("",30){

strcpy(job,jb);

strcpy(dep,dp);

}

voidLeader:

:

setjob(char*jb){

strcpy(job,jb);

}

voidLeader:

:

setdep(char*dp){

strcpy(dep,dp);

}

char*Leader:

:

getjob(){

returnjob;

}

char*Leader:

:

getdep(){

returndep;

}

Engineer:

:

Engineer(char*maj,char*pf):

Person("",30){

strcpy(major,maj);

strcpy(prof,pf);

}

voidEngineer:

:

setmajor(char*maj){

strcpy(major,maj);

}

voidEngineer:

:

setprof(char*pf){

strcpy(prof,pf);

}

char*Engineer:

:

getmajor(){

returnmajor;

}

char*Engineer:

:

getprof(){

returnprof;

}

Chairman:

:

Chairman(char*n,inta,char*jb,char*dp,char*maj,char*pf):

Person(n,a),Leader(jb,dp),Engineer(maj,pf){}

voidChairman:

:

disp(){

cout<<"姓名:

"<

"<

"<

"<

"<

"<

}

voidmain(){

Chairmanc("刘九州",21,"厅长","财政厅","经济学","高级经济师");

c.disp();

}

运行结果:

 

实验7多态与虚函数

一、实验目的

 

二、实验内容及步骤

1.设计一个图形类(Shape),由它派生出三角形类(Triangle)、正方形类(Square)、圆形类(Circle),利用虚函数计算图形面积,并在主函数中进行测试。

2.定义一个教师类,由教师类派生出讲师、副教授、教授类。

教师的工资分别由基本工资、课时费和津贴构成。

假设讲师、副教授、教授的基本工资分别为800、900、1000元,课时费分别为每小时40、45、50元,津贴分别为1300、1800、2300。

定义虚函数来计算教师的工资,并通过主函数来进行验证。

三、实验源程序和运行结果

实验

(一)源程序:

#include

usingnamespacestd;

classShape

{

public:

virtualfloatarea()

{return0.0;}

};

classTriangle:

publicShape

{

public:

Triangle()

{bc=1.0;h=1.0;}

Triangle(floatbc,floath)

{

this->bc=bc;this->h=h;}

boolsetbc(floata)

{if(a>0)bc=a;}

floatgetbc()

{returnbc;}

boolsetg(floatb)

{if(b>0)h=b;}

floatgetg()

{returnh;}

floatarea()

{returnbc*h/2;}

protected:

floatbc,h;

};

classSquare:

publicShape

{

public:

Square()

{l=1.0;}

Square(floatm)

{this->l=m;}

boolsetbc(floatc)

{if(c>0)l=c;}

floatgetbc()

{returnl;}

floatarea()

{returnl*l;}

protected:

floatl;

};

classCircle:

publicShape

{

public:

Circle()

{radius=1.0;}

Circle(floatR)

{this->radius=R;}

boolsetRadius(floatr)

{if(r>0)radius=r;}

floatgetRadius()

{returnradius;}

floatarea()

{return3.14159*radius*radius;}

protected:

floatradius;

};

voiddisplayShapeArea(Shape*p)

{

cout<<"图形面积为:

"<area()<

}

voidmain()

{

Shape*p1,*p2,*p3;

TriangleT(15.0,10.0);

SquareS(10.0);

CircleC(10.0);

p1=&T;

p2=&S;

p3=&C;

displayShapeArea(p1);

displayShapeArea(p2);

displayShapeArea(p3);

}

运行结果:

实验

(二)源程序:

#include

usingnamespacestd;

classteacher

{

public:

virtualfloatwage()

{return0.0;}

};

classlecturer:

publicteacher

{

public:

lecturer()

{WorkHours=1.0;}

lecturer(floatWorkHours)

{

this->WorkHours=WorkHours;

}

boolsetWorkHours(floath)

{

if(h>0)WorkHours=h;}

floatgetWorkHours()

{returnWorkHours;}

floatwage()

{

return(800+40*WorkHours+1300);

}

protected:

floatWorkHours;

};

classAssociateProfessor:

publicteacher

{

public:

AssociateProfessor()

{WorkHours=1.0;}

AssociateProfessor(floatWorkHours)

{

this->WorkHours=WorkHours;

}

boolsetWorkHours(floath)

{

if(h>0)WorkHours=h;}

floatgetWorkHours()

{returnWorkHours;}

floatwage()

{

return(900+45*WorkHours+1800);

}

protected:

floatWorkHours;

};

classProfessor:

publicteacher

{

public:

Professor()

{WorkHours=1.0;}

Professor(floatWorkHours)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 预防医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1