Polymorphism in C++ https://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm

https://github.com/mongodb/mongo/blob/410656e971aff8f491a87337a17d04bd866389ba/src/mongo/base/initializer.cpp

/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/ #include "mongo/platform/basic.h" #include "mongo/base/initializer.h" #include <iostream> #include "mongo/base/deinitializer_context.h"
#include "mongo/base/global_initializer.h"
#include "mongo/base/initializer_context.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/quick_exit.h" namespace mongo { Initializer::Initializer() {}
Initializer::~Initializer() {} Status Initializer::executeInitializers(const InitializerContext::ArgumentVector& args,
const InitializerContext::EnvironmentMap& env) {
std::vector<std::string> sortedNodes;
Status status = _graph.topSort(&sortedNodes);
if (Status::OK() != status)
return status; InitializerContext context(args, env); for (size_t i = 0; i < sortedNodes.size(); ++i) {
InitializerDependencyNode* node = _graph.getInitializerNode(sortedNodes[i]); // If already initialized then this node is a legacy initializer without re-initialization
// support.
if (node->isInitialized())
continue; auto const& fn = node->getInitializerFunction();
if (!fn) {
return Status(ErrorCodes::InternalError,
"topSort returned a node that has no associated function: \"" +
sortedNodes[i] + '"');
}
try {
status = fn(&context);
} catch (const DBException& xcp) {
return xcp.toStatus();
} if (Status::OK() != status)
return status; node->setInitialized(true);
}
return Status::OK();
} Status Initializer::executeDeinitializers() {
std::vector<std::string> sortedNodes;
Status status = _graph.topSort(&sortedNodes);
if (Status::OK() != status)
return status; DeinitializerContext context{}; // Execute deinitialization in reverse order from initialization.
for (auto it = sortedNodes.rbegin(), end = sortedNodes.rend(); it != end; ++it) {
InitializerDependencyNode* node = _graph.getInitializerNode(*it);
auto const& fn = node->getDeinitializerFunction();
if (fn) {
try {
status = fn(&context);
} catch (const DBException& xcp) {
return xcp.toStatus();
} if (Status::OK() != status)
return status; node->setInitialized(false);
}
}
return Status::OK();
} Status runGlobalInitializers(const InitializerContext::ArgumentVector& args,
const InitializerContext::EnvironmentMap& env) {
return getGlobalInitializer().executeInitializers(args, env);
} Status runGlobalInitializers(int argc, const char* const* argv, const char* const* envp) {
InitializerContext::ArgumentVector args(argc);
std::copy(argv, argv + argc, args.begin()); InitializerContext::EnvironmentMap env; if (envp) {
for (; *envp; ++envp) {
const char* firstEqualSign = strchr(*envp, '=');
if (!firstEqualSign) {
return Status(ErrorCodes::BadValue, "malformed environment block");
}
env[std::string(*envp, firstEqualSign)] = std::string(firstEqualSign + 1);
}
} return runGlobalInitializers(args, env);
} Status runGlobalDeinitializers() {
return getGlobalInitializer().executeDeinitializers();
} void runGlobalInitializersOrDie(int argc, const char* const* argv, const char* const* envp) {
Status status = runGlobalInitializers(argc, argv, envp);
if (!status.isOK()) {
std::cerr << "Failed global initialization: " << status << std::endl;
quickExit(1);
}
} } // namespace mongo

  

#include <iostream>
using namespace std; class Shape {
protected:
int width, height; public:
Shape( int a = 0, int b = 0){
width = a;
height = b;
}
int area() {
cout << "Parent class area :" <<endl;
return 0;
}
};
class Rectangle: public Shape {
public:
Rectangle( int a = 0, int b = 0):Shape(a, b) { } int area () {
cout << "Rectangle class area :" <<endl;
return (width * height);
}
}; class Triangle: public Shape {
public:
Triangle( int a = 0, int b = 0):Shape(a, b) { } int area () {
cout << "Triangle class area :" <<endl;
return (width * height / 2);
}
}; // Main function for the program
int main() {
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5); // store the address of Rectangle
shape = &rec; // call rectangle area.
shape->area(); // store the address of Triangle
shape = &tri; // call triangle area.
shape->area(); return 0;
}

  Polymorphism in C++ https://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm

C++ 多态 | 菜鸟教程 http://www.runoob.com/cplusplus/cpp-polymorphism.html

C++ polymorphism Virtual Function 多态 虚函数的更多相关文章

  1. Virtual Function(虚函数)in c++

    Virtual Function(虚函数)in c++ 用法: virtual void log() { std::cout << "hello world!" < ...

  2. 应该用bind+function取代虚函数吗?

    用bind+function取代虚函数在好几年前就有人提出了,曾引起广泛的讨论,有支持的有反对的,可能赞成的人占大多数.这个话题挺有趣,本来是作为技术沙龙的开放性话题来讨论的,由于时间关系并没有讨论. ...

  3. C++继承-重载-多态-虚函数

    C++ 继承 基类 & 派生类 一个类可以派生自多个类,这意味着,它可以从多个基类继承数据和函数.定义一个派生类,我们使用一个类派生列表来指定基类.类派生列表以一个或多个基类命名,形式如下: ...

  4. OOP 多态/虚函数

    // main.cpp // OOP // 虚函数允许继承层次结构中绝大多数特定版本的成员函数被选择执行,虚函数使多态成为可能. // Created by mac on 2019/4/8. // C ...

  5. C++ (P199—P211)多态 虚函数 抽象类

    在介绍多态之前,先回忆:赋值兼容原则.虚基类.二义性.派生类如何给基类赋值等知识. 在赋值兼容原则中:父类对象的指针赋给基类的指针或者父类的对象赋给基类的引用,可以通过强转基类的指针或者引用变为父类的 ...

  6. 看懂下面C++代码才说你理解了C++多态虚函数!

    #include <iostream> using namespace std ; class Father { private :  virtual void Say()  //只有添加 ...

  7. 多态&虚函数

     (1).对象类型:           a.静态类型:对象声明时的类型,编译的时候确定           b.动态类型:对象的类型是运行时才能确定的 class A {}; class B:pub ...

  8. C++虚函数virtual,纯虚函数pure virtual和Java抽象函数abstract,接口interface与抽象类abstract class的比较

    由于C++和Java都是面向对象的编程语言,它们的多态性就分别靠虚函数和抽象函数来实现. C++的虚函数可以在子类中重写,调用是根据实际的对象来判别的,而不是通过指针类型(普通函数的调用是根据当前指针 ...

  9. c++学习之多态(虚函数和纯虚函数)

    c++是面向对象语言,面向对象有个重要特点,就是继承和多态.继承之前学过了,就是一种重用类的设计方式.原有的类叫父类,或者基类,继承父类的类叫子类.在设计模式中,我们总是要避免继承,推荐用组合.因为继 ...

随机推荐

  1. Spring4 Quartz2 动态任务,Spring4整合quartz2.2.3简单动态任务

     Spring4 Quartz2 动态任务 Spring4整合quartz2.2.3简单动态任务, Quartz2.2.3简单动态定时任务二, SimpleScheduleBuilder简单定时任务 ...

  2. python cookie

    http://www.jayconrod.com/posts/17/how-to-use-http-cookies-in-python

  3. Ansible Playbook 简介

    我们去远程执行命令时要使用 command 模块,拷贝文件时要使用 copy 模块,如果我们要操作的东西很多,那就要执行很多条不同模块的命令Playbook 是一个 yaml 配置文件,我们可以把不同 ...

  4. Spring Web 应用的最大败笔

    开发人员在使用Spring应用是非常擅长谈论依赖注入的好处.不幸的是,他们不是那么真的利用它的好处,如单一职责原则,分离关注原则.如果我们一起来看看大部分Spring的Web应用程序,常见的错误的设计 ...

  5. es5.0 安装head插件

    es5.0的安装和之前的版本有些区别,我的电脑用plugin install 没成功, 查了一下资料,说是可以用grunt进行安装,启动; 1,先安装grunt: grunt是一个很方便的构建工具,可 ...

  6. Python进阶 学习笔记(一)

    (笔记范围:第一章 课程介绍:第二章 函数式编程:第三章 模块) Python支持的函数式编程 不是纯函数式编程:允许有变量 支持高阶函数:函数也可以作为变量传入 支持闭包:有了闭包就能返回函数 有限 ...

  7. python下安装Scikit-learn

    安装SK-Learn需要依赖的Python安装包有: Python (>= 2.6), NumPy (>= 1.3), SciPy (>= 0.7), 下载python的各种包的地址 ...

  8. Dropbox 在 Ubuntu 上需要认证授权的问题

    在 Ubuntu 上,通过 Ubuntu软件中心 搜索下载安装了 Dropbox . 运行时,弹出如下图的提示. 输入密码,点击 授权 后,没有出现 Dropbox 的运行界面. 再次点击运行,仍会出 ...

  9. JavaScript 正则表达式 通俗解释 快速记忆

    1.正则表达式中最重要的三个符号: 1.1 B 在正则表达式中B有3种类型的括号: 1.1.1 方括号 “[“. 方括号"["内是需要匹配的字符.中括号括住的内容只匹配一个单一的字 ...

  10. 常见的mysql 进程state<转自网络>

    Analyzing 线程是对MyISAM 表的统计信息做分析(例如, ANALYZE TABLE ). checking permissions 线程是检查服务器是否具有所需的权限来执行该语句. Ch ...