栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

设计并实现一个用户类 User ,并在主函数中使用和测试这个类。具体要求如下: 数据成员 每个用户有用户名 (name) 、密码 (passwd) 、联系邮箱 (email) 三个属性。 还有一个类属性,用于记录用户总数 n 。 函数成员 构造函数 如果定义用户对象时未设置密码和邮箱,密码默认为 6 个 1 ,联系邮箱默认为空串。 成员函数 set_email() 设置邮箱。提示用户从键盘输入邮箱。 change_passwd() 修改密码。修改密码前,要求先输入旧的密码,验证无误后,才允许修改;如果输入旧 密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。 print_info() 打印用户名、密码、联系邮箱。其中,密码以 6 个 * 方式显示。 User 类的定义和实现,保存在文件 User.hpp 中。 User 类的使用和测试,保存在文件 task4.cpp 中。
#include
#include
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 

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;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/347460.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号