task4

task4 User.hpp 代码如下:

 

#include<iostream>
#include<string>
using namespace std;
class User{
	public:
		User(string name1);
		User(string name1,string password1,string email1);
		
		void set_email();   //设置email 
		void change_passwd();    //改密码 
		void print_info();    //输出结果 
		static void print_n(); 
		
	private:
		string name,password,email;
		static int n;   //直接操作类的成员需要用静态变量 
};
int User::n=0;   //静态成员只能类外初始化 
User::User(string name1)
{
	name=name1;
	password="111111";
	email="";
	n++;
}
User::User(string name1,string password1,string email1)
{
	name=name1;
	password=password1;
	email=email1;
	n++;
}
111
void User::set_email()
{
	string t;
	cout<<"Enter email address:";
	cin>>t;
	email=t;
	cout<<"email is set successfully···"<<endl;
}
void User::change_passwd()
{
	string t0,t1;  //t0作原密码,t1作新密码 
	cout<<"Enter old password:";
	for(int i=1;i<=3;i++)
	{
		cin>>t0;
		if(t0==password)
		{
			cout<<"Enter new password:";
			cin>>t1;
			password=t1;
			break; 
		}
		else if(i<=2){
			cout<<"password input error. Please re-enter again:";
		}
		else cout<<"password input error. Please try after a while."<<endl;   //三次错误则无法再输入 
	}
}
void User::print_info()
{
	cout<<"name:"<<name<<endl;
	cout<<"passwd:"<<"******"<<endl;
	cout<<"email:"<<email<<endl; 
}
void User::print_n()
{
	if(n==1)
	  cout<<"there are "<<n<<" user."<<endl;
	else cout<<"there are "<<n<<" users."<<endl;
}

  

更改输入数据后运行结果如下:

 

posted @ 2021-10-22 10:18  纸鸢*  阅读(28)  评论(0)    收藏  举报