网易云课堂_C++开发入门到精通_章节6:多态
课时33示例--为多态基类声明虚析构函数
微软校园招聘笔试题
#include <iostream> class Base
{
public:
char Value()
{
return 'A';
} virtual char VirtualValue()
{
return 'X';
}
}; class Derived :public Base
{
public:
char Value()
{
return 'U';
}
}; class VirtualDerived :virtual public Base
{
public:
char Value()
{
return 'Z';
} char VirtualValue()
{
return 'V';
}
}; int main()
{
Base *p1 = new Derived();
Base *p2 = new VirtualDerived(); std::cout << p1->Value() << " " <<
p1->VirtualValue() << " " <<
p2->Value() << " " <<
p2->VirtualValue() << std::endl; system("pause"); return 0;
}
A X A V
请按任意键继续. . .
课时34案例讲解--简单工厂
案例讲解
简单工厂
案例介绍
模拟种植园管理程序
种植园里种苹果、葡萄等水果
案例设计
使用简单工厂模式实现开闭原则
#include <iostream> enum
{
APPLE = 0,
GRAPE = 1
}; class Fruit
{
public:
virtual ~Fruit() = 0; virtual void plant() = 0;
virtual void grow() = 0;
virtual void harvest() = 0;
}; class Apple :public Fruit
{
public:
Apple();
~Apple(); virtual void plant();
virtual void grow();
virtual void harvest();
}; class Grape :public Fruit
{
public:
Grape();
~Grape(); virtual void plant();
virtual void grow();
virtual void harvest();
}; class Gardener
{
public:
Gardener();
~Gardener(); Fruit* getFruit(int type); private:
Apple* apple;
Grape* grape;
}; Fruit::~Fruit()
{ } Apple::Apple()
{
std::cout << "apple constructing" << std::endl;
} Apple::~Apple()
{
std::cout << "apple destructing" << std::endl;
} void Apple::plant()
{
std::cout << "apple plant" << std::endl;
} void Apple::grow()
{
std::cout << "apple grow" << std::endl;
} void Apple::harvest()
{
std::cout << "apple harvest" << std::endl;
} Grape::Grape()
{
std::cout << "grape constructing" << std::endl;
} Grape::~Grape()
{
std::cout << "grape destructing" << std::endl;
} void Grape::plant()
{
std::cout << "grape plant" << std::endl;
} void Grape::grow()
{
std::cout << "grape grow" << std::endl;
} void Grape::harvest()
{
std::cout << "grape harvest" << std::endl;
} Gardener::Gardener()
{
apple = nullptr;
grape = nullptr;
} Gardener::~Gardener()
{ } Fruit* Gardener::getFruit(int type)
{
Fruit *fruit = nullptr; if (APPLE == type)
{
if (nullptr == apple)
{
apple = new Apple();
} fruit = apple;
}
else if (GRAPE == type)
{
if (nullptr == grape)
{
grape = new Grape();
} fruit = grape;
}
else
{ } return fruit;
} int main()
{
Gardener tom; Fruit* fruit = tom.getFruit(APPLE); fruit->plant();
fruit->grow();
fruit->harvest(); system("pause"); return 0;
}
网易云课堂_C++开发入门到精通_章节6:多态的更多相关文章
- 网易云课堂_C++开发入门到精通_章节7:模板
课时35类模板 类模板 创建类模板的实例 class Name<类型参数表>object; 类模板与模板类的区别 类模板是模板的定义,不是一个实实在在的类,定义中用到通用类型参数 模板类是 ...
- 网易云课堂_C++开发入门到精通_章节8:设计模式
课时44设计模式简介 设计模式简介 面向对象设计的第一个原则:针对接口编程,而不是针对实现编程 接口->指针 实现->实例 若已存在一个类Class A,现在希望复用Class A,则有以 ...
- 网易云课堂_C++开发入门到精通_章节4:运算符重载
课时23运算符重载 运算符重载 重载赋值运算符 Person& Person::operator=(const Person& other) { //检查自赋值 if (this == ...
- 网易云课堂_C++开发入门到精通_章节3: 类、对象和封装
课时12构造函数与析构函数-2 构造函数 构造函数可以有多个 构造函数可以重载 构造函数用于隐式类型转换 class Student { public: explicit Student(int ss ...
- 网易云课堂_C++开发入门到精通_章节2:引用和函数的高级用法
课时6函数重载 函数重载 在C语言头文件中的extern "C" //常见的C语言头文件格式 #ifndef _FUNC_ #define _FUNC_ #ifdef __cplu ...
- 网易云课堂_C++程序设计入门(上)_第6单元:丹枫虽老犹多态–继承与多态_第6单元作业【2】- 在线编程(难度:中)
第6单元作业[2]- 在线编程(难度:中) 查看帮助 返回 温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提交答案,系统 ...
- 网易云课堂_C++程序设计入门(上)_第1单元:C++概览_第1单元作业 - 写代码 - 互评 (难度:易)
第1单元作业 - 写代码 - 互评 (难度:易) 查看帮助 返回 提交作业(截止时间已过) 完成并提交作业 作业批改 互评训练 互评作业 自评作业 成绩公布 查看成绩 温 ...
- 网易云课堂_C++程序设计入门(上)_第5单元:万类霜天竞自由 – 对象和类的更多内容_第5单元作业【4】 - 在线编程(难度:难)
第5单元作业[4] - 在线编程(难度:难) 查看帮助 返回 温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提交答案,系 ...
- 网易云课堂_C++程序设计入门(上)_第4单元:物以类聚 – 对象和类_第4单元作业【3】- 在线编程(难度:难)
1 在本单元作业[1]和作业[2]的基础上,创建一个MyRectangle类,并在main函数中创建类的实例.(10分) 题目难度: 难 题目内容: Screen类: 与作业[2]要求完全相同. 如果 ...
随机推荐
- 一个简陋的 CSS 样式
有些网友对 Smart Framewok 中的 Sample 示例的样式比较感兴趣.由于本人对前端不太精通,但为了满足网友们的需求,只好献丑了. 以下这个简陋的 CSS 样式: ? 1 2 3 4 5 ...
- python - 文件
''' 模式 描述 r 以读方式打开文件,可读取文件信息. w 以写方式打开文件,可向文件写入信息.如文件存在,则清空该文件,再写入新内容 a 以追加模式打开文件(即一打开文件,文件指针自动移到文件末 ...
- 分析NTFS文件系统得到特定文件的内容
找某一个文件的内容(如要读取文件D:\dir\dir2\text.txt,详细过程例如以下: (1)读取分区表/分区链表信息,找到磁盘F的起始扇区. (2)读取D盘的第一个扇区(分区的BOOTSETO ...
- 利用boost获取时间并格式化
利用boost来获取当前时间又方便快捷,还不用考虑跨平台的问题. 1. 输出YYYYMMDD #include <boost/date_time/gregorian/gregorian.hpp& ...
- 面试前的准备---C#知识点回顾----04
播下的种子,慢慢开始发芽收获了,陆陆续续offer就来了,该轮到我挑的时候了 今天面试的一家公司,技术问的相对宽广和细致,程度令人发指 1.谈谈ViewState 这个问题,回答的好,工资翻一级 基本 ...
- linux基础内容学习一:linux下的分区及安装
linux看系统版本信息 uname -a 如果显示为i386,i686则为32位系统,如果为x86_64则为64位 一块硬盘最多可以有四个主分区其中一个主分区可以用一个扩展分区替换,在这个扩展分区中 ...
- 百度地图API用法(传地址)
网上找了很多都是没用的,非动态用法,最后在官网论坛才问到的 现在来一步一步教大家用 1 这是地址: http://developer.baidu.com/map/ 选择web开发 先获取密钥 ...
- Oracle更改字符集
更改oracle的字符集: sqlplus / as sysdba SQL> shutdown immediate; Database closed. Database dismounted. ...
- respondsToSelector的相关使用
-(BOOL) isKindOfClass: classObj 用来判断是否是某个类或其子类的实例 -(BOOL) isMemberOfClass: classObj 用来判断是否是某个类的实例 -( ...
- C# using垃圾回收详解
简介 定义一个范围,将在此范围之外释放一个或多个对象. 语法 using (Font font1 = new Font("Arial", 10.0f)) { } C# 语言参考 主 ...