c++程序设计课后习题6-27

【题目】 定义一个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++】=重载,报错:没有合适的复制构造函数icon-default.png?t=M5H6https://blog.csdn.net/C2681595858/article/details/83956459

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值