欢迎来到天天文库
浏览记录
ID:36438173
大小:61.50 KB
页数:6页
时间:2019-05-10
《C实验报告-实验5多态性与虚函数》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、实验5多态性与虚函数一、实验目的和要求了解静态联编和动态联编的概念。掌握动态联编的条件。二、实验内容和原理事先编写好程序,上机调试和运行程序,分析结果。(1)实验指导书P961~4任选一题。(2)实验指导书P1005~6任选一题。三、实验环境联想计算机,WindowsXP操作系统,VisualC++6.0四、算法描述及实验步骤(1)编写源程序。(2)检查程序有无错误(包括语法错误和逻辑错误),有则改之。(3)编译和连接,仔细分析编译信息,如有错误应找出原因并改正之。(4)运行程序,分析结果。(5)将调试好的程序保存在自己的用户目录中,文件名自定。五、调试过程12六、实验结
2、果15七、总结动态联编需要满足3个条件,首先类之间满足类型兼容规则;第二是要声明虚函数;第三是要由成员函数来调用或者是通过基类指针、引用来访问虚函数。附录:1.{public:virtualvoidf(floatx){cout<<"Base::f(float)"<3、{cout<<"Derived::h(float)"<f(3.14f);pb->f(3.14f);pb->g(3.14f);pb->h(3.14f);pb->h(3.14f);return0;}2#includeusingnamespacestd;#includeclassTeacher{public:Teacher(stringn,inth);virtualdoublesalary()=0;virtualvoidpr4、int()=0;protected:stringname;intlessons;};Teacher::Teacher(stringn,inth){name=n;lessons=h;}classProfessor:publicTeacher{public:Professor(stringn,inth):Teacher(n,h){}doublesalary(){return5000+50*lessons;}voidprint();};voidProfessor::print(){cout<5、;cout<<"tearned¥"<usingnamespacestd;#includeclassTeacher{publ6、ic:Teacher(stringn,inth);virtualdoublesalary()=0;virtualvoidprint()=0;protected:stringname;intlessons;};Teacher::Teacher(stringn,inth){name=n;lessons=h;}classProfessor:publicTeacher{public:Professor(stringn,inth):Teacher(n,h){}doublesalary(){return5000+50*lessons;}voidprint();};voidProfes7、sor::print(){cout<
3、{cout<<"Derived::h(float)"<f(3.14f);pb->f(3.14f);pb->g(3.14f);pb->h(3.14f);pb->h(3.14f);return0;}2#includeusingnamespacestd;#includeclassTeacher{public:Teacher(stringn,inth);virtualdoublesalary()=0;virtualvoidpr
4、int()=0;protected:stringname;intlessons;};Teacher::Teacher(stringn,inth){name=n;lessons=h;}classProfessor:publicTeacher{public:Professor(stringn,inth):Teacher(n,h){}doublesalary(){return5000+50*lessons;}voidprint();};voidProfessor::print(){cout<5、;cout<<"tearned¥"<usingnamespacestd;#includeclassTeacher{publ6、ic:Teacher(stringn,inth);virtualdoublesalary()=0;virtualvoidprint()=0;protected:stringname;intlessons;};Teacher::Teacher(stringn,inth){name=n;lessons=h;}classProfessor:publicTeacher{public:Professor(stringn,inth):Teacher(n,h){}doublesalary(){return5000+50*lessons;}voidprint();};voidProfes7、sor::print(){cout<
5、;cout<<"tearned¥"<usingnamespacestd;#includeclassTeacher{publ
6、ic:Teacher(stringn,inth);virtualdoublesalary()=0;virtualvoidprint()=0;protected:stringname;intlessons;};Teacher::Teacher(stringn,inth){name=n;lessons=h;}classProfessor:publicTeacher{public:Professor(stringn,inth):Teacher(n,h){}doublesalary(){return5000+50*lessons;}voidprint();};voidProfes
7、sor::print(){cout<
此文档下载收益归作者所有