实验五:派生类和继承_第1页
实验五:派生类和继承_第2页
实验五:派生类和继承_第3页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验五:派生类和继承(二)福建农林大学实验报告实验5派生类和继承(二)、实验目的和要求去,进一步理解类二、实验内容和原理1、试写出所能想到的所有形状(包括二维的和三维 的),生成一个形状层次类结构。生成的层次结构以Shape作为基类,并由此派生出 TwoDimShape类和 ThreeDimShape类。它们的派生类是不同形状类,定 义层次结构中的每一个类,并用函数main ()进行测试。2、设计一个学生类CStudent。数据成员包括学生姓名,性别,年龄,学号,已修学分。成员函数包括输 入输出或修改学生姓名、学号,增加已修学分。设计 一个教师类CTeacher。数据成员包括教师姓名,性 别,

2、年龄,职称,担任课程。成员函数包括输入输出 或修改教师姓名、年龄,更改职称等。以CStude nt为基类派生子类CGraduateStudent。增加数据成员 导师姓名、研究生已发表论文的数目;成员函数为增 加已发表论文数目。最后,用函数main ()函数进行测试。(提示:可为CStudent类和CTeacher类创建一个公共基类)三、实验环境1. 硬件:PC机;Visual C+ 6.02. 软件:Windows操作系统、四、算法描述及实验步骤#i nclude<iostream.h>const double Pl=3.14;class shapepublic:shape();d

3、ouble area()c on stretur n 0.0; double bulk()co nstreturn 0.0; ;class TwoDimShape:public shape;class Circle:public TwoDimShapepublic:Circle(double myr)R=myr;double area()c on streturn PI*R*R; protected:double R;class ThreeDimShape:public shape;class sphere:public ThreeDimShapepublic:sphere(double my

4、w)R=myw;double bulk()co nstreturn 4/3*PI*R*R*R; protected:double R;int mai n()shape sha;double area;double bulk;Circle c(3.0);area=c.area();cout<<"Area of circle is"<<area<<e ndl;sphere sph(4.0);bulk=sph.bulk(); cout«"Bulk of sphere is"<<bulk<<e

5、 ndl; return 0;2.#in clude<iostream>lude<string>g n amespace std; s Pers onublic:Pers on( stri ng n, stri ng s,i nt a)n ame=n; sex=s;age=a;void in put()cin>>"姓名:">>name;cin>>"性别:">>sex;cin>>"年龄:">>age;void display()cout

6、71;"姓名:"<<name«endl; coutvv"'性另H:"<<sex<<endl; cout«"年龄:"<<age<<endl;ected:stri ng n ame;stri ng sex;int age;s Teacher:virtual public Personic:Teacher(stri ng n, stri ng s,i nt a,stri ng t,stri ng l) :Pers on(n ,s,a)title=t;vo

7、id in put()Pers on:i nput();cin>>"职称:">>title;cin>>"担任课程:">>lesson;void display。Pers on:display。; protcout«"职称:"vvtitlevvendl; coutvv"担任课程:"vvlesson<<endl;ected: string title; stri ng less on;;clas public:s Stude nt:virtual p

8、ublic Pers onStude nt(stri ng n, stri ng s,i nt a, stri ng no ,stri ng c) :Pers on(n ,s,a)num=no;redit=c;oid in put()Pers on:i nput();cin>>"学号:">>num; cin>>"已修学分:">>credit;void display()Pers on:display。;cout«"学号:"<vnum<<endl; cout&#

9、171;"已修学分:"vvcreditvvendl;protected:stri ng num;stri ng credit;s Graduate:public Stude nt;claspublic:Graduate(stri ng n, stri ng s,i nt a,stri ng no ,stri ng c,stri ng tn, stri ng th) :Perso n(n ,s,a),Stude nt( n, s,a, no,c) tn ame=t n; thesis=th;void in put()Pers on:i nput(); cin>>&

10、quot;导师姓名:">>tname; cin>>"发表论文数目:">>thesis;void display()Stude nt:display();coutvv"导师姓名:"<<tnamevvendl; cout«"发表论文数目:"<<thesis<<endl;private:stri ng tn ame;stri ng thesis;int main()Teacher teac("镇镇","男",3

11、0,"博士","面向对象程序设计");Student stud("星星","女",19,"100202019","6");Graduate grad(" 杰杰","男",25,"100202021","18"," 源源","20"); cout«"教师有关数据:"<<endl;teac.display();cout&

12、lt;<"n 学生数据:"<<endl;stud.display();cout<<"n 研究生数据:"<<endl; grad.display();return 0;五、调试过程PI=3.14后面忘记加“;”号了retur nO.O 应该是 return 0.0protected 后面加的是:而不是;2.error C25Q1'string' : missing storage-class or type specifiersoerror C25 01'name' : n)i5si

13、ng storage-class or tijpe 5pecifiers訂error C21Ji6syntax error : missing '4 before identifier 'sex4error C25 01'string- : nissing starage-class or type specifiers刃error C2501'sex' : missing storage-clasG or type gpecitiers0)error C229unexpected 'class Teacher ('0)error C23

14、34unpMpectt0k0n(g) preceding ':1; skipping apparent func打ef*r<jr C2i46syntam error : niisingbefore identlFier 1 title 1error C25B1'string': nissing storage-class or type specifiers?)errr C25 01'title' ; niiny tordge-cl55 or type specifiersa)error C21-46syntax error : missing &

15、#39;* before identifier 'lesson'町error C25 01'string- : missing storage-class or type specifiers0Jerror C25 01'lesson' :storage-class or type specifiers5)error C2«29unexpected 'class Student C'5)error C233J*unexpected takn(s) preceding ':1; skipping apparent func刃error C2146syntdx error : misirig - ;* before identifier 'num*error C25B1'string' : ni

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论