64,654
社区成员
发帖
与我相关
我的任务
分享
/*
* 13.10 定义一个Employee的类,包含雇员名字和一个唯一的雇员标识。为该类定义默认构造
* 函数和参数为表示雇员名字的string的构造函数。如果该类需要复制构造函数、赋值
* 符,实现这些函数
*/
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
class Employee {
public:
void add_empoly( Employee &emp, const string &temp );
int get_mark ( ) { return mark++; }
friend void display( ostream &out, Employee &temp );
//default constructor
explicit Employee( ): ID( 0 )
{
//The body is NULL
}
//constructor
explicit Employee( const string &nam ): name( new string( nam ) )
{
ID = get_mark( );
}
//copy constructor
Employee( const Employee © ): name( new string( *( copy.name ) ) ), ID( 0 )
{
//The body is NULL
}
//assignment operator
Employee& Employee::operator=( const Employee &oper )
{
mark = oper.mark;
*name = *( oper.name );
ID = oper.ID;
return *this;
}
//destructor
~Employee( )
{
delete name;
}
private:
static int mark;
string * name;
int ID;
};
// define the Employee class
void Employee::add_empoly( Employee &emp, const string & temp )
{
*(emp.name) = temp;
emp.ID = get_mark( );
}
int Employee::mark = 0;
void display( ostream &out, Employee &temp )
{
out << temp.name << " " << temp.ID << endl;
}
// --------------------------------
int main( void )
{
string strName;
vector<Employee> vecEmployeeList;
Employee haha;
cout << "请输入一个人名" << endl;
cin >> strName;
Employee.add_empoly( haha, strName ); // 缺少 ; 在 . 前面
vecEmployeeList.push_back( haha );
strName.clear( );
cout << "请输入一个人名" << endl;
cin >> strName;
vecEmployeeList.push_back( Employee( string( strName ) ) );
for( vector<Employee>::size_type index = 0;
index != vecEmployeeList.size( );
++index )
{
display( cout, vecEmployeeList[ index ] );
}
system( "pause" );
return 0;
}
void add_empoly( Employee &emp, const string &temp );
Employee.add_empoly( haha, strName );
Employee.add_empoly( haha, strName );
haha.add_empoly( haha, strName );