用c++语言声明包含5个元素的对象数组,每个元素都是Employee类型的对象,并生成完整程序
时间: 2024-02-25 20:55:31 浏览: 55
以下是一个C++程序,它声明了包含5个元素的Employee类型数组,并将它们初始化为具有不同值的Employee对象:
```c++
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
string name;
int age;
double salary;
public:
Employee() {
name = "";
age = 0;
salary = 0.0;
}
Employee(string n, int a, double s) {
name = n;
age = a;
salary = s;
}
void print() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Salary: " << salary << endl;
}
};
int main() {
Employee employees[5] = {
Employee("Alice", 25, 50000.0),
Employee("Bob", 30, 60000.0),
Employee("Charlie", 35, 70000.0),
Employee("David", 40, 80000.0),
Employee("Emma", 45, 90000.0)
};
for (int i = 0; i < 5; i++) {
cout << "Employee " << i+1 << ":" << endl;
employees[i].print();
cout << endl;
}
return 0;
}
```
该程序定义了一个Employee类,它具有名称、年龄和工资属性,并在构造函数中初始化这些属性。在主函数中,它声明了一个包含5个元素的Employee数组,并将其初始化为具有不同值的Employee对象。然后,它使用循环结构打印每个Employee对象的属性。
阅读全文