Problem Description
某网站需要记录登录用户的用户名、密码和登录总人数。
要求:
(1)设计一个用户类User,用户名、密码都用字符指针,登录总人数用静态成员。
(2)定义必要的成员函数,其中有一个实现显示用户信息的成员函数Display。
完善以下程序。
//你的代码写在这里
int main()
{
char name1[] = “AAA”;
char password1[] = “12345”;
User u1(name1, password1);
u1.Display();
User u2 = u1;
u2.Display();
char name3[] = “BBB”;
char password3[] = “67890”;
User u3(name3, password3);
u3.Display();
cout << User::n << endl;
return 0;
}
Input Description
无
Output Description
输出各用户信息和用户个数。
Sample Output
AAA 12345
AAA 12345
BBB 67890
3
#includeusing namespace std; class User { private: char* name; char* password; public: //设置 User(char c[], char d[])//User(char * c, char * d)也行 { name = c; password = d; } static int n;//静态 //获取 void Display() { cout << name << " " << password << endl; n++; } }; int User::n = 0;//User作用域下的n清零 int main() { char name1[] = "AAA"; char password1[] = "12345"; User u1(name1, password1); u1.Display(); User u2 = u1; u2.Display(); char name3[] = "BBB"; char password3[] = "67890"; User u3(name3, password3); u3.Display(); cout << User::n << endl; return 0; }
#include#include #include using namespace std; class User { public: char x[100], y[100]; static int n; User(char a[], char b[]) { strcpy_s(x, a);//x = a; //不加_s也行,就是vs会报错 strcpy_s(y, b);//y = b; } void Display() { cout << x << ' ' << y << endl; n++; } }; int User::n = 0; int main() { char name1[] = "AAA"; char password1[] = "12345"; User u1(name1, password1); u1.Display(); User u2 = u1; u2.Display(); char name3[] = "BBB"; char password3[] = "67890"; User u3(name3, password3); u3.Display(); cout << User::n << endl; return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)