面向对象程序设计C++.docx

上传人:b****1 文档编号:2430006 上传时间:2022-10-29 格式:DOCX 页数:31 大小:280.78KB
下载 相关 举报
面向对象程序设计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级计算机大类三班

学号:

*********

实验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*les

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

当前位置:首页 > 求职职场 > 面试

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

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