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

下载本文档

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

文档简介

1、福建农林大学实验报告实验5 派生类和继承(二)一、实验目的和要求(1)掌握派生类的声明与定义方法,进一步理解类的继承的概念,能够定义和使用类的继承关系。(2)熟悉公有派生和私有派生的访问特性。(3)了解虚基类在解决二义性问题中的作用。二、实验内容和原理1、试写出所能想到的所有形状(包括二维的和三维的),生成一个形状层次类结构。生成的层次结构以Shape作为基类,并由此派生出TwoDimShape类和ThreeDimShape类。它们的派生类是不同形状类,定义层次结构中的每一个类,并用函数main()进行测试。2、设计一个学生类CStudent。数据成员包括学生姓名,性别,年龄,学号,已修学分。

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

3、>const double PI=3.14;class shape public:shape(); double area()constreturn 0.0; double bulk()constreturn 0.0;class TwoDimShape:public shape;class Circle:public TwoDimShapepublic:Circle(double myr)R=myr;double area()constreturn PI*R*R; protected: double R;class ThreeDimShape:public shape;class sph

4、ere:public ThreeDimShapepublic:sphere(double myw)R=myw;double bulk()constreturn 4/3*PI*R*R*R; protected: double R;int main()shape sha;double area;double bulk;Circle c(3.0);area=c.area();cout<<"Area of circle is"<<area<<endl; sphere sph(4.0);bulk=sph.bulk();cout<<&qu

5、ot;Bulk of sphere is"<<bulk<<endl;return 0;2.#include<iostream>#include<string>using namespace std;class Person public: Person(string n,string s,int a) name=n;sex=s;age=a; void input() cin>>"姓名:">>name;cin>>"性别:">>sex;cin>&g

6、t;"年龄:">>age;void display() cout<<"姓名:"<<name<<endl; cout<<"性别:"<<sex<<endl; cout<<"年龄:"<<age<<endl;protected:string name; string sex; int age; ; class Teacher:virtual public Person public:Teacher(str

7、ing n,string s,int a,string t,string l):Person(n,s,a) title=t;lesson=l; void input() Person:input();cin>>"职称:">>title;cin>>"担任课程:">>lesson;void display() Person:display();cout<<"职称:"<<title<<endl;cout<<"担任课程:"<

8、;<lesson<<endl;protected:string title; string lesson; ;class Student:virtual public Person public:Student(string n,string s,int a, string no,string c):Person(n,s,a) num=no; credit=c; void input() Person:input();cin>>"学号:">>num;cin>>"已修学分:">>credit

9、;void display() Person:display();cout<<"学号:"<<num<<endl;cout<<"已修学分:"<<credit<<endl;protected:string num; string credit; ;class Graduate:public Student public:Graduate(string n,string s,int a,string no,string c,string tn,string th):Person(n,s,a

10、),Student(n,s,a,no,c)tname=tn; thesis=th;void input() Person:input();cin>>"导师姓名:">>tname;cin>>"发表论文数目:">>thesis;void display() Student:display();cout<<"导师姓名:"<<tname<<endl;cout<<"发表论文数目:"<<thesis<<end

11、l;private:string tname;string thesis;int main() Teacher teac("镇镇","男",30,"博士","面向对象程序设计");Student stud("星星","女",19,"100202019","6");Graduate grad("杰杰","男",25,"100202021","18","源源","20");cout<<"教师有关数据:"<<endl;teac.display();cout<<"n学生数据:"<<endl; stud.display();cout<<"n研究生数据:"<&

温馨提示

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

评论

0/150

提交评论