#ifndef USER_HPP
#define USER_HPP

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

class User {
private:
    string name;
    string password;
    string email;
    static int count;
public:
    User(string name0, string password0 = "111111", string email0 = "******") :name{ name0 }, password{ password0 }, email{ email0 }{++count; }
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n();
};

int User::count = 0;

void User::set_email() {
    string e;
    cout << "Enter email address:";
    cin >> e;
    email = e;
    cout << "email is set successfully..." << endl;
}

void User::change_passwd() {
    string oldp, newp;
    int i = 0;
    cout << "please enter old password:";
    while (i < 3) {
        cin >> oldp;
        if (oldp == password) {
            cout << "please enter new passwrod:";
            cin >> newp;
            password = newp;
            cout <<"new passwd is successfully..." <<endl;
            break;
        }
        else {
            cout << "password input error.Please re-enter again:";
            i++;
        }
    }
    if(i>=3) {
        cout << "password input error.Please try after a while." << endl;
    }
}

void User::print_info() {
    cout << "name: " << name << endl << "passwd: " << "******" << endl << "email: " << email << endl;
}

void User::print_n() {
    cout << "there are " << count << " users." << endl;
}
#endif // !USER_HPP
#include "User.hpp"
#include <iostream>

int main()
{
    using namespace std;

    cout << "testing 1......" << endl;
    User user1("Jonny", "92197", "xyz@hotmail.com");
    user1.print_info();

    cout << endl
         << "testing 2......" << endl
         << endl;
    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    User::print_n();
}

实验一 类与对象 task4 User类_ios

 

实验一 类与对象 task4 User类_#include_02