【题目】 定义一个Employee类,其中包括表示姓名、街道地址、城市和邮编等属性,包括setName()和display()函数。display()使用cout语句显示姓名、街道地址、城市和邮编等属性,函数setName()改变对象的姓名属性,实现并测试这个类。
题目描述已经
<使用平台:Microsoft visual studio>
代码如下:
#include<iostream>
using namespace std;
#include<string>
class Employee
{
public:
Employee(string City, int Post, string Name, int Street, int Tel)
:name(Name),street(Street),tel(Tel),city(City),post(Post){count++;}
Employee() :name("NULL"), street(), tel(), post(), city("NULL") {};
Employee(const Employee& e) //拷贝来自相同城市的雇主信息
{
string eName;
int eStreet=0, eTel=0;
//string* ename = new string(eName);
//int* estreet = new int(eStreet);
//int* etel = new int(eTel);
city = e.city;
post = e.post;
cout << endl << "Now you're copying the information from the same city --" << e.city << endl;
cout << "[warning]you should set your name|street|telephone again,please enter:" << endl;
cout << "Name:";
cin >> eName;
cout << "Street:";
cin >> eStreet;
cout << "Tel:";
cin >> eTel;
name = eName;
tel = eTel;
street = eStreet;
count++;
}
void setName(string Name) {
name = Name;
cout << endl << "...... Now you are changing its name" << endl;
}
void display() {
cout << endl;
cout << "Name: " << name << endl;
cout << "Street: " << street << endl;
cout << "Tel: " << tel << endl;
cout << "City: " << city << endl;
cout << "Postcode: " << post << endl;
}
~Employee() { count--; }
static void showcount() {
cout << endl << "Now the NUMBERS of employees are " << count << endl;
}
private:
string name, city;
int street, tel, post;
static int count;
};
int Employee::count = 0;
int main()
{
Employee e_inChongqing("重庆", 400000, "A", 001, 12356);
Employee e_inXian("西安", 710000, "B", 002, 12457);
Employee e_inZhengzhou("郑州", 450000, "C", 003, 16728);
Employee e_null;
Employee e_new(e_inZhengzhou); //拷贝来自郑州的雇主的城市和邮编信息
e_new.setName("D");
Employee e_e[]= { Employee(e_inChongqing),Employee(e_inXian) }; //设置一个对象数组,调用拷贝构造函数来拷贝来自相同城市的雇主信息
string num = "E";
for (int i = 0; i < 2; i++)
{
e_e[i].setName(num); //给雇主setname,编号
num += '1';
}
//display阶段
cout << endl << "——WELCOME TO OUR EMPLOYEE DATA BASES——" << endl << " (loarding......)" << endl;
e_null.display();
e_inChongqing.display();
e_inXian.display();
e_inZhengzhou.display();
e_new.display();
for (int i = 0; i < 2; i++)
{
e_e[i].display();
}
Employee::showcount();
return 0;
}
main函数中创建对象数组时,error出现在68行
严重性 代码 说明 项目 文件
错误(活动) E0334 类 "Employee" 没有适当的复制构造函数 6_24 D:\c&c++相关资料\c++\c++实验报告\6_24\源6_24.cpp
联机搜索后发现需要在拷贝构造函数的形参中加const即可,具体原因详见【c++】=重载,报错:没有合适的复制构造函数https://blog.youkuaiyun.com/C2681595858/article/details/83956459