面向对象程序设计实验报告 实验三 继承与多态性 s205100016 曾峻

上传人:笛音 文档编号:25872634 上传时间:2017-12-19 格式:DOC 页数:11 大小:199.50KB
返回 下载 相关 举报
面向对象程序设计实验报告 实验三 继承与多态性 s205100016 曾峻_第1页
第1页 / 共11页
面向对象程序设计实验报告 实验三 继承与多态性 s205100016 曾峻_第2页
第2页 / 共11页
面向对象程序设计实验报告 实验三 继承与多态性 s205100016 曾峻_第3页
第3页 / 共11页
面向对象程序设计实验报告 实验三 继承与多态性 s205100016 曾峻_第4页
第4页 / 共11页
面向对象程序设计实验报告 实验三 继承与多态性 s205100016 曾峻_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《面向对象程序设计实验报告 实验三 继承与多态性 s205100016 曾峻》由会员分享,可在线阅读,更多相关《面向对象程序设计实验报告 实验三 继承与多态性 s205100016 曾峻(11页珍藏版)》请在金锄头文库上搜索。

1、广州大学学生实验报告开课学院及实验室:计算机科学与工程实验 室 2014 年 11 月 6 日学院 化学化工学院 年级、专 业、班 化学 2 班 姓名 曾峻 学号 s205100016实验课程名称 面向对象的程序设计 成绩实验项目名称 实验三 继承与多态性 指导老师 熊伟一、 实验目的1、掌握继承、基类和派生类的概念。2、掌握初始化基类成员的方法。3、 掌握派生类对基类的继承。4、 学习虚函数和纯虚函数的定义与使用方式。5、 理解抽象类的概念,学习如何用指针指向其他的派生类,实现多态性。6、 掌握抽象类的定义与使用方式,并注意指针的用法。7、学习如何使用虚函数、纯虚函数、抽象类和实现类的多态性

2、。二、使用仪器、器材1、硬件环境:微型计算机,2、软件环境:Visual C+ 6.0;三、实验内容及步骤1、定义派生类的实例#include stdafx.h#include string.h#include iostream.hclass STUDENTprotected:char m_no12,m_name11;int m_sex,m_year,m_month,m_day;public:STUDENT() void set(char * no,char * name,int sex,int year,int month,int day) strcpy(m_no,no);strcpy(m_

3、name,name);m_sex=sex;m_year=year;m_month=month;m_day=day;void set_no(char * no) strcpy(m_no,no); void set_name(char * name) strcpy(m_name,name); void set_sex(int sex) m_sex=sex; void set_year(int year) m_year=year; void set_month(int month) m_month=month; void set_day(int day) m_day=day; char * get_

4、no() return m_no; char * get_name() return m_name; int get_sex() return m_sex; int get_year() return m_year; int get_month() return m_month; int get_day() return m_day; ;class CLASS: public STUDENTprotected:int m_c,m_english;public:CLASS()void set_c(int c) m_c=c; void set_english(int english) m_engl

5、ish=english; int get_c() return m_c; int get_english() return m_english; void list() coutstr;si.set_no(str);coutstr;si.set_name(str);coutx;si.set_sex(x);coutx;si.set_year(x);cinx;si.set_month(x);cinx;si.set_day(x);coutx;si.set_c(x);coutx;si.set_english(x);for (i=0;i class base public:virtual void fh

6、() cout In base class n ; ;class subclass: public base public:virtual void fn() cout In subclass n ; ;void test( base &b) b.fn(); void main( ) base be;subclass sc;cout Calling test(bc) n ;test(bc);cout Calling test(sc) n ;test(sc);5、用抽象类实现的菜单程序。# include class Menu public:virtual void action ( ) = 0

7、;;class item1: public Menupublic:virtual void action ()cout 新建文件 endl;;class item2: public Menupublic :virtual void action ( )cout 打开文件 endl;;class item3: public Menupublic:virtual void action ( )cout 保存文件 endl;;class item4: public Menupublic:virtual void action() cout关闭文件 endl;void main() int selec

8、t;Menu * Fptr4;FptrO = new item1;Fptr1 = new item2;Fptr2 = new item3;Fptr3 = new item4;Do cout 1 new file endl;cout 2 open file endl;cout 3 save file endl;cout 4 close file endl;cout O exit endl;cin select;if( select = 1 while( select! =0);for(int i=0;iclass people public:/添加程序private:;class student

9、: virtual public peoplepublic:/添加程序private:;class teacher:virtual public peoplepublic:/添加程序private:;class graduate: virtual public studentpublic:/添加程序private:;class TA:public people student, public teacherpublic:/添加程序private:;void main()/添加程序四、实验过程原始数据记录/*有一个图书馆类,内有数据成员“借书卡号”和“书名”;还有一个学生类,内有数据成员“姓名”

10、。试设计一个读者类,它是图书馆类和学生类的派生类,继承了它们的所有数据成员和成员函数,在此基础上要添加一个查找函数,可根据输入的读者姓名,显示出该读者的相关信息。程序设计:(1)定义一个图书馆类 Library,其主要功能是记录读者的借书卡号和书名。(2)定义一个学生类 Student,它的主要作用是记录读者的姓名。(3)定义一个通过读者类 Reader,它是 Library 和 Student 的派生类,它除了继承这两个类的数据成员,还定义自己的数据成员,通过该类可以了解到读者的上述所有信息,还记录了读者的借还书日期。*/#includeusing namespace std;class L

11、ibrarypublic:int cardno;char bookname30;Library(int c, char *b) strcpy_s(bookname, b); cardno = c; ;class Studentpublic:char readername30;Student(char *r) strcpy_s(readername, r); ;class Reader :public Library, public Studentpublic:char returndate30;Reader(char* rd) :Library(2, 中华经典诵读), Student(小明)

12、strcpy_s(returndate, rd); friend void print();friend Reader operatorusing namespace std;class containerpublic:virtual void surfacearea()const = 0;virtual void volume()const = 0;void getvalues(double r) radius = r; void getvalues(double r, double h) radius = r; height = h; double radius;double height

13、;class cube:public containerpublic:void surfacearea()constcout r1;cout h1;c.getvalues(r1);c.surfacearea();c.volume();s.getvalues(r1);s.surfacearea();s.volume(); cy.getvalues(r1,h1);cy.surfacearea();cy.volume();/*编写一个人员信息管理系统。这个系统的功能是:交互式的实现校园人员信息的录入与显示。分析:学校里,主要有四类人员:大学本科学生、教师、研究生和助教。大学本科生每周有固定的学时数。

14、教师除了固定的学时数外,还有每周的教学时数。研究生除了固定的学时数外,每周还可以自由做一定的研究。助教生除了上课外,还要做研究和一定的教学工作。人员的基本信息包括姓名、编号、性别、身份证号、总学时数以及每周固定学时数。各个人员之间的关系:people 类派生出 student 类和 teacher 类,student 类派生出 graduate 类, graduate 类和teacher 类派生出 TA 类。*/#includeusing namespace std;class peoplepublic:char name30;char no30;char sex30;char cardno3

15、0;int totalhours;int hourspw;/*people(char *n, char *nno, char *s, char *c, int t, int h)strcpy_s(name, n); strcpy_s(no, nno); strcpy_s(sex, s); strcpy_s(cardno, c); totalhours = t; hourspw = h;*/people();class student :virtual public peoplepublic:friend student operator(istream& in, student& s)cout s.name; in s.no; in s.sex; in s.cardno; in s.totalhours; in s.hourspw;return s;friend student operator(istream& in, teacher& t)cout t.name; in t.no; in t.sex; in t.cardno; i

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 其它文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号