c++程序片段题定义一个员工类、经理类、销售员类和销售经理类
时间: 2023-09-07 14:11:04 浏览: 119
好的,下面是C语言程序片段,定义了员工类、经理类、销售员类和销售经理类:
```c
#include <stdio.h>
// 定义员工类
struct Employee {
char name[20];
int age;
double salary;
};
// 定义经理类
struct Manager {
char name[20];
int age;
double salary;
char department[20];
};
// 定义销售员类
struct Salesman {
char name[20];
int age;
double salary;
double commission;
};
// 定义销售经理类
struct SalesManager {
char name[20];
int age;
double salary;
double commission;
char department[20];
};
int main() {
// 在此处进行你想要执行的操作
return 0;
}
```
相关问题
重庆邮电大学c++面向对象程序设计C++实验四 程序片段编程题 1. 定义一个员工类、经理类、销售员类和销售经理类
好的,以下是我提供的代码:
```c++
#include <iostream>
#include <string>
using namespace std;
class Employee {
protected:
string name;
int age;
public:
Employee(string name, int age) {
this->name = name;
this->age = age;
}
virtual void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
class Manager : public Employee {
private:
int department;
public:
Manager(string name, int age, int department) : Employee(name, age) {
this->department = department;
}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Department: " << department << endl;
}
};
class Salesman : public Employee {
private:
int sales;
public:
Salesman(string name, int age, int sales) : Employee(name, age) {
this->sales = sales;
}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Sales: " << sales << endl;
}
};
class SalesManager : public Manager, public Salesman {
public:
SalesManager(string name, int age, int department, int sales) : Manager(name, age, department), Salesman(name, age, sales) {}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Department: " << department << endl;
cout << "Sales: " << sales << endl;
}
};
int main() {
Employee* e = new Employee("Tom", 25);
e->display();
cout << endl;
Manager* m = new Manager("Jerry", 30, 101);
m->display();
cout << endl;
Salesman* s = new Salesman("Lucy", 28, 100000);
s->display();
cout << endl;
SalesManager* sm = new SalesManager("Bob", 35, 102, 200000);
sm->display();
cout << endl;
return 0;
}
```
解释一下:我们定义了一个 `Employee` 类,表示员工,包含姓名和年龄两个属性,以及一个 `display()` 方法用于显示员工信息。然后派生出 `Manager` 类和 `Salesman` 类,分别表示经理和销售员,这两个类在 `Employee` 类的基础上增加了一些属性,分别为部门和销售额,同时也重载了 `display()` 方法。最后,我们再派生出 `SalesManager` 类,表示销售经理,这个类同时继承了 `Manager` 和 `Salesman` 两个类,因此可以直接使用它们的属性和方法。在 `main()` 函数中,我们分别创建了一个 `Employee` 对象、一个 `Manager` 对象、一个 `Salesman` 对象和一个 `SalesManager` 对象,然后都调用了它们的 `display()` 方法来展示各自的信息。
阅读全文