 Define abstract class Employee, it contains:  Data member: string id, string name, int age, double salary  Constructor  pay(): pure virtual function for calculating the employee’s salary  Derived class Manager, it contains:  Derived from class Employee.  Data members: double fixedSalary  Constructor  Implement pay() function.  Tips: The salary of managers = fixedSalary.  Derived class Saleman, it contains:  Derived from class Employee.  Data member: double salesVolume  Implement pay() function.  Tips: Saleman salary = 0.04* salesVolume.  Declare the base class pointer p and derived class object “Manager A("1001","Zhang San", 25, 5000)” and “Saleman B("1002", "Li Si", 28, 100000)” in the main function, and call the member functions pay() of different objects through the base class pointer.

时间: 2024-02-21 20:56:33 浏览: 21
Here is the implementation of the required classes and their functions in C++: ```c++ #include<iostream> #include<string> using namespace std; // Abstract Employee class class Employee { protected: string id; string name; int age; double salary; public: Employee(string id, string name, int age, double salary) { this->id = id; this->name = name; this->age = age; this->salary = salary; } virtual void pay() = 0; // Pure virtual function }; // Manager class derived from Employee class Manager : public Employee { private: double fixedSalary; public: Manager(string id, string name, int age, double fixedSalary) : Employee(id, name, age, 0) { this->fixedSalary = fixedSalary; } void pay() { salary = fixedSalary; cout << "Manager " << name << " received salary: " << salary << endl; } }; // Salesman class derived from Employee class Salesman : public Employee { private: double salesVolume; public: Salesman(string id, string name, int age, double salesVolume) : Employee(id, name, age, 0) { this->salesVolume = salesVolume; } void pay() { salary = 0.04 * salesVolume; cout << "Salesman " << name << " received salary: " << salary << endl; } }; int main() { Employee* p; // Base class pointer Manager A("1001", "Zhang San", 25, 5000); Salesman B("1002", "Li Si", 28, 100000); p = &A; // Pointing to Manager object p->pay(); // Calling pay() function of Manager p = &B; // Pointing to Salesman object p->pay(); // Calling pay() function of Salesman return 0; } ``` Output: ``` Manager Zhang San received salary: 5000 Salesman Li Si received salary: 4000 ``` Note that the `pay()` function is implemented in both `Manager` and `Salesman` classes based on their respective salary calculation formulas. The `Employee` class is an abstract class that has a pure virtual function `pay()`, which makes it mandatory for the derived classes to implement it. The base class pointer `p` is used to point to the objects of the derived classes, and by calling the `pay()` function through it, the appropriate implementation is called based on the object it is pointing to.

相关推荐

最新推荐

recommend-type

Python中对错误NameError: name ‘xxx’ is not defined进行总结

错误NameError: name ‘xxx’ is not defined总结情况一:要加双引号(” “)或者(’ ‘)而没加情况二:字符缩进格式的问题情况三:`if __name__==’__main__’ :` 没有和`class类`进行对齐情况四:NameError: ...
recommend-type

require.js中的define函数详解

主要给大家介绍了关于require.js中define函数的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用require.js中的define函数具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
recommend-type

C# #define条件编译详解

主要介绍了C# #define条件编译,告诉大家#define是用来做什么?如何使用#define,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

详解C语言中的#define宏定义命令用法

有的时候为了程序的通用性,可以使用#define预处理宏定义命令,它的具体作用就是方便程序段的定义和修改,下面就来详解C语言中的#define宏定义命令用法.
recommend-type

浅谈#ifndef,#define,#endif的作用和用法

下面小编就为大家带来一篇浅谈#ifndef,#define,#endif的作用和用法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SQL怎么实现 数据透视表

SQL可以通过使用聚合函数和GROUP BY子句来实现数据透视表。 例如,假设有一个销售记录表,其中包含产品名称、销售日期、销售数量和销售额等信息。要创建一个按照产品名称、销售日期和销售额进行汇总的数据透视表,可以使用以下SQL语句: ``` SELECT ProductName, SaleDate, SUM(SaleQuantity) AS TotalQuantity, SUM(SaleAmount) AS TotalAmount FROM Sales GROUP BY ProductName, SaleDate; ``` 该语句将Sales表按照ProductName和SaleDat
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。