当前位置: 代码迷 >> 综合 >> c++实验报告——用户类User
  详细解决方案

c++实验报告——用户类User

热度:65   发布时间:2023-12-13 05:24:57.0
设计并实现一个用户类 User ,并在主函数中使用和测试这个类。具体要求如下:
数据成员
每个用户有用户名 (name) 、密码 (passwd) 、联系邮箱 (email) 三个属性。
还有一个类属性,用于记录用户总数 n
函数成员
构造函数
如果定义用户对象时未设置密码和邮箱,密码默认为 6 1 ,联系邮箱默认为空串。
成员函数
set_email()
设置邮箱。提示用户从键盘输入邮箱。
change_passwd()
修改密码。修改密码前,要求先输入旧的密码,验证无误后,才允许修改;如果输入旧
密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。
print_info()
打印用户名、密码、联系邮箱。其中,密码以 6 * 方式显示。
User 类的定义和实现,保存在文件 User.hpp 中。
User 类的使用和测试,保存在文件 task4.cpp 中。
#include<iostream>
#include<string>
using namespace std;class User
{
public:User(string name,string passwd = "111111",string email = ""):n{name}, p{passwd}, e{email}{count++;}~User() = default;    void set_email();void change_passwd();void print_info();static void print_n();private:string n,p,e;static int count;
};int User::count = 0;void User::set_email()
{cout << "Enter email adress: " ;cin >> e ;cout << "email is set successfully..." << endl;
}void User::change_passwd()
{cout << "Enter old password: ";string temp;int i = 3;while(i){cin >> temp;if(temp == p){cout << "new passwd is set successfully..." << endl;break;}else {i--;if(i != 0)cout << "password input error. Please re-enter again: " ;}if(i == 0){cout << "password input error. Please try after a while." << endl;}}}void User::print_info()
{cout << "name:   " << n << endl;cout << "passwd: " << "******" << endl;cout << "email:  " << e << endl;
}void User::print_n()
{cout << "there are " << count << " users." << endl;
}
#include "User.hpp"
#include <iostream>int main()
{using namespace std;cout << "testing 1......" << endl;User user1("Bill Gates", "666666", "202083290182@nuist.edu.cn");user1.print_info();cout << endl<< "testing 2......" << endl<< endl;User user2("Elon Musk");user2.change_passwd();user2.set_email();user2.print_info();User::print_n();return 0;
}

  相关解决方案