第十三周 课后实践:阅读程序1
2015-06-05 13:24
267 查看
(1)问题及代码:
运行结果:
(2)问题及代码:
运行结果:
(3)问题及代码:
运行结果:
#include<iostream> using namespace std; class A { int a; public: A():a(5){} virtual void print()const { cout<<a;} }; class B: public A { char b; public: B() { b='E'; } void print() const { cout<<b; } }; void show(A &x) { x.print(); } int main() { A d1,*p; B d2; p=&d2; d1.print(); d2.print(); p->print(); show(d1); show(d2); return 0; }
运行结果:
(2)问题及代码:
#include <iostream> using namespace std; class BASE { private: char c; public: BASE(char n):c(n) {} virtual ~BASE() { cout<<c; } }; class DERIVED:public BASE { private: char c; public: DERIVED(char n):BASE(n+1),c(n) {} ~DERIVED(){ cout<<c; } }; int main(){ DERIVED d('X'); return 0; }
运行结果:
(3)问题及代码:
#include <iostream> using namespace std; class Base { public: virtual void Who() =0; }; class FirstDerived:public Base { public: void Who() { cout<<"F"; } }; class SecondDerived:public Base { public: void Who() { cout<<"S"; } }; int main() { FirstDerived first_obj; SecondDerived second_obj; Base &bref=first_obj; bref.Who(); bref=second_obj; bref.Who(); Base *bp; bp=&first_obj; bp->Who(); bp=&second_obj; bp->Who(); return 0; }
运行结果:
相关文章推荐
- oracle表空间和用户的删除
- jquery 使用ajax请求数据显示到页面表格中
- 随机数 java.util.Random与java.lang.Math.Random()-Java
- [LeetCode] Two Sum III - Data Structure Design
- oracle创建表空间和用户,并且设置权限
- 使用API创建AR 贷项通知单
- 求匹配的括号的最大长度
- 缓冲区溢出攻击
- 二进制中1的个数
- SGU127 Telephone directory
- windows下MongoDB的安装及配置
- Unity3d读取CSV中的文本到Rich Text中换行符不识别的问题
- epel yum源。
- GitHub如何征服了Google、微软及一切
- 倒序输出
- uploadify 超出最大请求长度
- Mac OS X 键盘快捷键
- 黑马程序员—java语法
- java注释生成文档 乱码,java.lang.IllegalArgumentException
- 输入一个字符串,删除其中所有的数字,所有大写字母改成小写,其他不变,并输出