课时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:多态的更多相关文章

  1. 网易云课堂_C++开发入门到精通_章节7:模板

    课时35类模板 类模板 创建类模板的实例 class Name<类型参数表>object; 类模板与模板类的区别 类模板是模板的定义,不是一个实实在在的类,定义中用到通用类型参数 模板类是 ...

  2. 网易云课堂_C++开发入门到精通_章节8:设计模式

    课时44设计模式简介 设计模式简介 面向对象设计的第一个原则:针对接口编程,而不是针对实现编程 接口->指针 实现->实例 若已存在一个类Class A,现在希望复用Class A,则有以 ...

  3. 网易云课堂_C++开发入门到精通_章节4:运算符重载

    课时23运算符重载 运算符重载 重载赋值运算符 Person& Person::operator=(const Person& other) { //检查自赋值 if (this == ...

  4. 网易云课堂_C++开发入门到精通_章节3: 类、对象和封装

    课时12构造函数与析构函数-2 构造函数 构造函数可以有多个 构造函数可以重载 构造函数用于隐式类型转换 class Student { public: explicit Student(int ss ...

  5. 网易云课堂_C++开发入门到精通_章节2:引用和函数的高级用法

    课时6函数重载 函数重载 在C语言头文件中的extern "C" //常见的C语言头文件格式 #ifndef _FUNC_ #define _FUNC_ #ifdef __cplu ...

  6. 网易云课堂_C++程序设计入门(上)_第6单元:丹枫虽老犹多态–继承与多态_第6单元作业【2】- 在线编程(难度:中)

    第6单元作业[2]- 在线编程(难度:中) 查看帮助 返回   温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提交答案,系统 ...

  7. 网易云课堂_C++程序设计入门(上)_第1单元:C++概览_第1单元作业 - 写代码 - 互评 (难度:易)

    第1单元作业 - 写代码 - 互评 (难度:易) 查看帮助 返回   提交作业(截止时间已过) 完成并提交作业     作业批改 互评训练   互评作业   自评作业     成绩公布 查看成绩 温 ...

  8. 网易云课堂_C++程序设计入门(上)_第5单元:万类霜天竞自由 – 对象和类的更多内容_第5单元作业【4】 - 在线编程(难度:难)

    第5单元作业[4] - 在线编程(难度:难) 查看帮助 返回   温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提交答案,系 ...

  9. 网易云课堂_C++程序设计入门(上)_第4单元:物以类聚 – 对象和类_第4单元作业【3】- 在线编程(难度:难)

    1 在本单元作业[1]和作业[2]的基础上,创建一个MyRectangle类,并在main函数中创建类的实例.(10分) 题目难度: 难 题目内容: Screen类: 与作业[2]要求完全相同. 如果 ...

随机推荐

  1. View中取设置了Tag的UILabel

    UILabel *badge = (UILabel *)[self.view viewWithTag:];

  2. JS 数组扩展函数--求起始项到终止项和

    Array.prototype.sum= function(l,r){ l=l==undefined ? 0 : l; r=r==undefined ? this.length - 1 : r; va ...

  3. [Immutable,js] Immutable.Record() as data models

    The Immutable.js Record() allows you to model your immutable data much like you would model data wit ...

  4. [Immutable.js] Converting Immutable.js Structures to Javascript and other Immutable Types

    Immutable.js provides several conversion methods to migrate one structure to another. Each Immutable ...

  5. ThinkPHP整合百度Ueditor图文教程

    ThinkPHP整合百度Ueditor图文教程 ThinkPHP整合百度Ueditor,基于黄永成老师的视频说明的申明:最好大家都能写绝对路径的都写好绝对路径比如:window.UEDITOR_HOM ...

  6. Pull生成&解析

    开篇注意,由于解析有可能有大文件非常耗时,建议另开一个线程解析也可以不开具体视情况而定     Pull生成 1.通过xml获得序列化的实例 XmlSerializer nxs = Xml.newSe ...

  7. iOS 面试题 1

    1.    简述OC中内存管理机制.与retain配对使用的方法是dealloc还是release,为什么?需要与alloc配对使用的方法是dealloc还是release,为什么?readwrite ...

  8. 1.iOS第一个简单APP

    大纲: iOS系统发展 UI和OC 简单的APP程序 程序的生命周期   1.iOS的系统发展 从1983年OC程序开始发展到2015年,30多年的时间,但这依然不是一个十分完善的语言,可以说现在都没 ...

  9. C++服务器设计(二):应用层I/O缓冲

    数据完整性讨论 我们已经选择了I/O复用模型作为系统底层I/O模型.但是我们并没有具体解决读写问题,即在我们的Reactor模式中,我们怎么进行读写操作,才能保证对于每个连接的发送或接收的数据是完整的 ...

  10. poj1111 DFS

    J - 搜索 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:10000KB     64bit I ...