C++实验报告十七Word格式文档下载.docx

上传人:b****5 文档编号:18364926 上传时间:2022-12-15 格式:DOCX 页数:7 大小:59.76KB
下载 相关 举报
C++实验报告十七Word格式文档下载.docx_第1页
第1页 / 共7页
C++实验报告十七Word格式文档下载.docx_第2页
第2页 / 共7页
C++实验报告十七Word格式文档下载.docx_第3页
第3页 / 共7页
C++实验报告十七Word格式文档下载.docx_第4页
第4页 / 共7页
C++实验报告十七Word格式文档下载.docx_第5页
第5页 / 共7页
点击查看更多>>
下载资源
资源描述

C++实验报告十七Word格式文档下载.docx

《C++实验报告十七Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《C++实验报告十七Word格式文档下载.docx(7页珍藏版)》请在冰豆网上搜索。

C++实验报告十七Word格式文档下载.docx

1、声明一个抽象类shape,派生出rectangle和circle类,二者都有geta()和getb()函数,可分别计算出对象的面积和周长。

2、设计一个类系统,其中有教师和学生,他们之前有相同的地方,也有自己的特征,利用继承机制定义这个类系统中各个类及类中必须的操作。

三、实验结果及其结果分析

#include<

iostream.h>

classShape//抽象类的定义

{

public:

virtualdoubleGeta()=0;

//纯虚函数

virtualdoubleGetb()=0;

};

classRectangle:

publicShape//矩形类,公有继承

public:

Rectangle(doubleaa,doublebb)//带参数的构造函数

{

a=aa;

b=bb;

cout<

<

"

长"

a<

宽"

b<

endl;

}

virtualdoubleGeta()

{returna*b;

}

virtualdoubleGetb()

{return2*(a+b);

private:

doublea;

doubleb;

classCircle:

publicShape//圆类,公有继承

Circle(doublerr)//带参数的构造函数

r=rr;

半径"

r<

{returnr*r*3.14;

virtualdoubleGetb()

{return2*r*3.14;

}

private:

doubler;

voidmain()

{

doublelength,width;

cout<

"

输入长和宽:

;

cin>

>

length>

width;

Rectanglerect(length,width);

面积是:

rect.Geta()<

endl<

周长是:

rect.Getb()<

doublerr;

输入半径:

rr;

Circlecir(rr);

cir.Geta()<

cir.Getb()<

2、#include<

iostream>

string>

usingnamespacestd;

classPerson{

Person(stringn,strings,inta)

{name=n;

sex=s;

age=a;

voidinput(){

cin>

姓名:

name;

性别:

sex;

年龄:

age;

voiddisplay()

name<

sex<

age<

protected:

stringname;

stringsex;

intage;

classTeacher:

virtualpublicPerson{

Teacher(stringn,strings,inta,stringt,stringl):

Person(n,s,a)

title=t;

lesson=l;

voidinput(){

Person:

:

input();

cin>

职称:

title;

担任课程:

lesson;

display();

title<

lesson<

stringtitle;

stringlesson;

classStudent:

virtualpublicPerson

{

Student(stringn,strings,inta,stringno,stringc):

Person(n,s,a){

num=no;

credit=c;

voidinput(){

学号:

num;

已修学分:

credit;

voiddisplay(){

num<

credit<

stringnum;

stringcredit;

classGraduate:

publicStudent{

Graduate(stringn,strings,inta,stringno,stringc,stringtn,stringth):

Person(n,s,a),Student(n,s,a,no,c)

tname=tn;

thesis=th;

voidinput()

导师姓名:

tname;

发表论文数目:

thesis;

Student:

tname<

thesis<

stringtname;

stringthesis;

intmain()

Teacherteac("

镇镇"

"

男"

30,"

博士"

面向对象程序设计"

);

Studentstud("

星星"

女"

19,"

100202019"

6"

Graduategrad("

杰杰"

25,"

100202021"

18"

源源"

20"

教师有关数据:

teac.display();

\n学生数据:

stud.display();

\n研究生数据:

grad.display();

return0;

四、实验收获与体会

通过这次实验,我们复习了跟类有关的程序,自己能够掌握知识,去编写一个需要的程序。

任何知识的学习,都要求我们能够融会贯通,学以致用。

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

当前位置:首页 > 高等教育 > 其它

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

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