zoukankan      html  css  js  c++  java
  • User类 新增共有属性Current ID

    一、题目描述

    每个用户有用户编号(id),用户名(name),密码(passwd)三个属性。其中:

    • 用户编号(id)由系统自动顺序编号,用户名和密码都是字母、数字、符合的组合,新用户密码,默认“111111”。
    • User类所有对象有一个共有属性Current ID,用来记录当前已经被使用的最大id号(初始值999)。每当新增一个用户时,CurrentID的值加1,同时将这个值作为新用户的id号。

    二、解答内容

    //user.h

     1 #ifndef HEADER_U
     2 #define HEADER_U
     3 #include<string>
     4 #include<iostream>
     5 using namespace std;
     6 
     7 //User类声明
     8 class User {
     9 public:
    10     void setInfo(string name0, string passwd0 = "111111", int id0 = 1);//支持设置用户信息
    11     void changePasswd();                               //支持修改密码
    12     void printInfo();                                  //支持打印用户信息
    13     User(){}                                           //默认构造函数
    14     User(const User &v);                               //复制最新用户信息
    15 private:
    16     int id;
    17     static int CurrentID;
    18     string name;
    19     string passwd;
    20 };
    21 int User::CurrentID = 999;                             //静态成员初始化
    22 
    23 void User::setInfo(string name0, string passwd0, int id0) {
    24     id = ++CurrentID;
    25     name = name0;
    26     passwd = passwd0;
    27 }
    28 
    29 void User::changePasswd() {
    30     string passwd1, passwdN;
    31     int n = 1;
    32     cout << "Enter the old passwd:";
    33     cin >> passwd1;
    34     while (n <= 3) {
    35         if ( passwd == passwd1) {
    36             cout << "Enter the new passwd:";
    37             cin >> passwdN;
    38             break;
    39         }
    40         else if (n < 3 && (passwd != passwd1)) {
    41             cout << "passwd input error,Please re-Enter again:";
    42             cin >> passwd1;
    43             n++;
    44         }
    45         else if (n == 3 && (passwd != passwd1)) {
    46             cout << "Please try after a while." << endl;
    47             break;
    48         }
    49     }
    50 }
    51 
    52 void User::printInfo() {
    53     cout << "CurrentID:	" << CurrentID << endl;
    54     cout << "id:		" << id << endl;
    55     cout << "name:		" << name << endl;
    56     cout << "passwd:		" << "******" << endl;
    57 }
    58 
    59 User::User(const User &v):id(v.id),name(v.name),passwd(v.passwd){
    60     printInfo();
    61 }
    62 
    63 #endif

    //User.cpp

     1 #include<iostream>
     2 #include"user.h"
     3 using namespace std;
     4 
     5 int main() {
     6     cout << "testing 1......" << endl << endl;
     7     User user1;
     8     user1.setInfo("Leonard");
     9     user1.printInfo();
    10 
    11     cout << endl << "testing 2......" << endl << endl;
    12     User user2;
    13     user2.setInfo("Jonny", "92197");
    14     user2.changePasswd();
    15     user2.printInfo();
    16 
    17     cout << endl << "testing 3......" << endl << endl;
    18     User userN(user2);
    19 
    20     system("pause");
    21     return 0;
    22 }

    效果如下:

    三、题后小结

    1. 静态数据成员声明在类内,初始化在外部。
    2. setInfo函数的参数注意把id放于最后并设置默认参数。
  • 相关阅读:
    技术领导要不要写代码?
    资深程序员告诉你:如何用五年时间攒够100万?
    mfc基于对话框的应用程序,如何设置初始对话框大小,移动控件位置
    zend studio,操作记录
    xampp怎么操作数据库mysql
    mysql-font的理解
    delphi 中配置文件的使用(*.ini)和TIniFile 用法
    delphi 创建服务,安装与卸载服务
    sublime Text的一些用法(emmet插件、)
    apache (web服务器) ->php->mysql,xampp与wamp比较,WAMP与WNMP有什么区别
  • 原文地址:https://www.cnblogs.com/zuiyankh/p/10693428.html
Copyright © 2011-2022 走看看