1 Star 0 Fork 2

codes_test/cpp-Interview2

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
virtual.cpp 1002 Bytes
一键复制 编辑 原始数据 按行查看 历史
ericps 提交于 2022-03-13 17:51 +08:00 . remove and rename
#include <iostream>
#include<iostream>
using namespace std;
class Employee
{
public:
virtual void raiseSalary()
{
cout<<0<<endl;
}
virtual void promote()
{ /* common promote code */ }
};
class Manager: public Employee
{
void raiseSalary() override
{
cout<<100<<endl;
}
void promote() override
{ /* Manager specific promote */ }
};
class Engineer: public Employee
{
void raiseSalary() override
{
cout<<200<<endl;
}
void promote() override
{ /* Manager specific promote */ }
};
void globalRaiseSalary(Employee *emp[], int n)
{
for (int i = 0; i < n; i++)
emp[i]->raiseSalary();
// according to the actual object, not according to the type of pointer
}
int main()
{
// 虚函数的调用取决于指向或者引用的对象的类型,而不是指针或者引用自身的类型
Employee *emp[] = {new Manager(), new Engineer};
globalRaiseSalary(emp,2); // 100 200
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/codes_test/cpp-interview2.git
git@gitee.com:codes_test/cpp-interview2.git
codes_test
cpp-interview2
cpp-Interview2
main

搜索帮助