C++ 类定义和使用示例:Employee 类与 Name 类
C++ 类定义和使用示例:Employee 类与 Name 类
本示例展示了如何在 C++ 中定义并使用类,包括 Employee 类和 Name 类,它们包含姓名、地址、城市、邮编等信息,并演示如何创建对象、调用成员函数以及修改属性。
1. Employee 类定义
class Employee{
private:
Name name;
string street_address;
string city;
string zip_code;
public:
Employee(Name n, string sa, string c, string zc): name(n), street_address(sa), city(c), zip_code(zc){}
void display(){
cout'Name: ' << name.display() << endl;
cout'Street Address: ' << street_address << endl;
cout'City: ' << city << endl;
cout'Zip Code: ' << zip_code << endl;
}
void change_name(Name n){
name = n;
}
};
// 创建五个 Employee 对象
Employee employees[5] = {
Employee(Name('John','Doe'),'123 Main St','New York','10001'),
Employee(Name('Jane','Doe'),'456 Oak Ave','Chicago','60601'),
Employee(Name('Bob','Smith'),'789 Elm St','Los Angeles','90001'),
Employee(Name('Sue','Johnson'),'321 Maple St','Boston','02101'),
Employee(Name('Tom','Jones'),'654 Pine Ave','San Francisco','94101')
};
2. Name 类定义
class Name{
protected:
string first_name;
string last_name;
public:
Name(string fn, string ln): first_name(fn), last_name(ln){}
string display(){
return first_name + ' ' + last_name;
}
};
3. 完整的类定义
class Name{
protected:
string first_name;
string last_name;
public:
Name(string fn, string ln): first_name(fn), last_name(ln){}
string display(){
return first_name + ' ' + last_name;
}
};
class Employee{
private:
Name name;
string street_address;
string city;
string zip_code;
public:
Employee(Name n, string sa, string c, string zc): name(n), street_address(sa), city(c), zip_code(zc){}
void display(){
cout'Name: ' << name.display() << endl;
cout'Street Address: ' << street_address << endl;
cout'City: ' << city << endl;
cout'Zip Code: ' << zip_code << endl;
}
void change_name(Name n){
name = n;
}
};
// 创建五个 Employee 对象
Employee employees[5] = {
Employee(Name('John','Doe'),'123 Main St','New York','10001'),
Employee(Name('Jane','Doe'),'456 Oak Ave','Chicago','60601'),
Employee(Name('Bob','Smith'),'789 Elm St','Los Angeles','90001'),
Employee(Name('Sue','Johnson'),'321 Maple St','Boston','02101'),
Employee(Name('Tom','Jones'),'654 Pine Ave','San Francisco','94101')
};
4. 主函数调试
int main(){
// 显示原始信息
for(int i=0;i<5;i++){
cout'Employee ' << i+1 << ':' << endl;
employees[i].display();
cout << endl;
}
// 修改姓名
employees[0].change_name(Name('Johnny','Doe'));
employees[2].change_name(Name('Robert','Smith'));
// 显示修改后的信息
for(int i=0;i<5;i++){
cout'Employee ' << i+1 << ':' << endl;
employees[i].display();
cout << endl;
}
return 0;
}
输出结果
Employee 1:
Name: John Doe
Street Address: 123 Main St
City: New York
Zip Code: 10001
Employee 2:
Name: Jane Doe
Street Address: 456 Oak Ave
City: Chicago
Zip Code: 60601
Employee 3:
Name: Bob Smith
Street Address: 789 Elm St
City: Los Angeles
Zip Code: 90001
Employee 4:
Name: Sue Johnson
Street Address: 321 Maple St
City: Boston
Zip Code: 02101
Employee 5:
Name: Tom Jones
Street Address: 654 Pine Ave
City: San Francisco
Zip Code: 94101
Employee 1:
Name: Johnny Doe
Street Address: 123 Main St
City: New York
Zip Code: 10001
Employee 2:
Name: Jane Doe
Street Address: 456 Oak Ave
City: Chicago
Zip Code: 60601
Employee 3:
Name: Robert Smith
Street Address: 789 Elm St
City: Los Angeles
Zip Code: 90001
Employee 4:
Name: Sue Johnson
Street Address: 321 Maple St
City: Boston
Zip Code: 02101
Employee 5:
Name: Tom Jones
Street Address: 654 Pine Ave
City: San Francisco
Zip Code: 94101
说明
- Employee 类包含一个 Name 类的对象作为其私有成员,用于存储员工姓名。
- Name 类包含两个字符串类型的保护数据成员,用于存储姓和名。
- Employee 类包含构造函数、display 函数和 change_name 函数,分别用于创建对象、显示对象信息和修改对象姓名。
- 主函数创建了五个 Employee 对象,并使用 display 函数显示其信息,以及使用 change_name 函数修改其中两个对象的姓名,然后再次显示其信息。
本示例演示了 C++ 中类定义和使用的基本方法,以及如何将一个类的对象作为另一个类的成员。 通过这种方式,可以实现代码复用和更好的结构化编程。
原文地址: https://www.cveoy.top/t/topic/nhNB 著作权归作者所有。请勿转载和采集!