#include<iostream> #include<cstring> using namespace std; class Employee { private: long num; string name; char sex[4]; const char* sect; int wage; static int count; static long totalWage; public: Employee(long n,string nam,char se[4],const char* sec,int wag); void print(); static void ShowStatic(); }; Employee::Employee (long n,string nam,char se[4],const char* sec,int wag) { num=n; name=nam; strcpy(sex,se); sect=new char[strlen(sec)+1]; strcpy(sect,sec); wage=wag; count++; totalWage+=wag; } void Employee::ShowStatic() { cout<<count<<" "<<totalWage<<endl; } void Employee::print() { cout<<num<<" "<<name<<" "<<sex[4]<<" "<<sect<<" "<<wage<<endl; } int main () { int count=0; long totalWage=0; Employee a[2]={Employee(123,"dsb","nan","bzd",2023),Employee(234,"xsb","nv","bzd",2024)}; a[0].print(); a[1].print(); a[0].ShowStatic(); return 0; }
时间: 2023-12-06 15:40:49 浏览: 143
这是一个简单的员工类的实现,包含了员工的基本信息和工资信息。其中,静态成员变量count和totalWage用于统计所有员工的数量和工资总额。
在main函数中,先定义了一个局部变量count和totalWage,然后创建了两个Employee对象,并调用了print函数输出员工信息,最后调用了ShowStatic函数输出所有员工的数量和工资总额。
需要注意的是,sex数组的大小应该为5而不是4,因为字符串末尾需要有一个'\0'作为结束符。同时,在析构函数中应该释放sect指向的内存空间,避免内存泄漏。
相关问题
#include<iostream> #include<string> #include<string.h> using namespace std; class Student { private: long num; int age; char* name; public: Student(long n,int a,const char* nam) { num=n; age=a; name=new char[strlen(nam)+1]; strcpy(name,nam); } void show_Student () { cout<<num<<" "<<age<<" "<<name<<" "<<endl; } void modify_name () { char* nam; cin>>nam; delete []name; name=new char[strlen(nam)+1]; strcpy(name,nam); } }; int main() { Student a(20222484,18,"lyc"); a.show_Student(); a.modify_name(); a.show_Student(); return 0; }优化上面代码
以下是优化后的代码:
```cpp
#include<iostream>
#include<string>
#include<cstring> // 使用cstring头文件代替string.h
using namespace std;
class Student {
private:
long num;
int age;
string name; // 使用string代替char*
public:
Student(long n, int a, const string& nam) : num(n), age(a), name(nam) {} // 使用初始化列表
void show_Student() {
cout << num << " " << age << " " << name << " " << endl;
}
void modify_name() {
string nam;
cin >> nam;
name = nam;
}
};
int main() {
Student a(20222484, 18, "lyc");
a.show_Student();
a.modify_name();
a.show_Student();
return 0;
}
```
优化的点如下:
1. 使用了string代替char*,避免手动内存管理和越界访问等问题。
2. 使用了初始化列表,避免在构造函数内部对成员变量进行赋值,提高代码效率。
3. 使用了cstring头文件代替string.h,cstring是C++中的标准头文件,string.h是C语言的头文件,虽然两者都可以使用,但是cstring更符合C++的风格。
帮我找以下c++代码的bug:#include<iostream> #include<cstring> using namespace std; class Employee { private: long num; string name; char sex[4]; const char* sect; int wage; static int count; static long totalWage; public: Employee(long n,string nam,char se[4],const char* sec,int wag); void print(); static void ShowStatic(); }; Employee::Employee (long n,string nam,char se[4],const char* sec,int wag) { num=n; name=nam; strcpy(sex,se); sect=new char[strlen(sec)+1]; strcpy(sect,sec); wage=wag; count++; totalWage+=wag; } void Employee::ShowStatic() { cout<<count<<" "<<totalWage<<endl; } void Employee::print() { cout<<num<<" "<<name<<" "<<sex[4]<<" "<<sect<<" "<<wage<<endl; } int main () { int count=0; long totalWage=0; Employee a[2]={Employee(123,"dsb","nan","bzd",2023),Employee(234,"xsb","nv","bzd",2024)}; a[0].print(); a[1].print(); a[0].ShowStatic(); return 0; }
在类定义中,静态数据成员 count 和 totalWage 必须在类外进行初始化。
在 main 函数中,变量 count 和 totalWage 与类中的同名静态数据成员重名,造成了混淆。
以下是修正后的代码:
```
#include<iostream>
#include<cstring>
using namespace std;
class Employee {
private:
long num;
string name;
char sex[4];
const char* sect;
int wage;
static int count;
static long totalWage;
public:
Employee(long n,string nam,char se[4],const char* sec,int wag);
void print();
static void ShowStatic();
};
int Employee::count = 0; // 静态数据成员初始化
long Employee::totalWage = 0;
Employee::Employee (long n,string nam,char se[4],const char* sec,int wag) {
num=n;
name=nam;
strcpy(sex,se);
sect=new char[strlen(sec)+1];
strcpy(sect,sec);
wage=wag;
count++;
totalWage+=wag;
}
void Employee::ShowStatic() {
cout<<count<<" "<<totalWage<<endl;
}
void Employee::print() {
cout<<num<<" "<<name<<" "<<sex<<" "<<sect<<" "<<wage<<endl;
}
int main () {
Employee a[2]={Employee(123,"dsb","nan","bzd",2023),Employee(234,"xsb","nv","bzd",2024)};
a[0].print();
a[1].print();
Employee::ShowStatic(); // 静态成员函数调用
return 0;
}
```
修改后的代码中,静态数据成员 count 和 totalWage 在类外进行初始化,避免了混淆。在 main 函数中,使用 Employee::ShowStatic() 调用静态成员函数。
阅读全文