敲代码的第八天:构建储蓄账户类,用到继承性(c++)

#include <iostream>

#include <string.h>
using namespace std;

class Account {
private:
    int id;
    char name[20]; //字符数组
    int money;
public:
    Account();

    Account(int id);

    Account(int id, char name[], int money);
    void inputInfo();
    void print();
};

class SaveAccount : public Account {
private:
    char pwd[20];
    char address[20];
    int minMoney;
    int  rate;
public:
    SaveAccount();

    SaveAccount(int id);

    SaveAccount(char pwd[], char address[], int minMoney, int rate);
    SaveAccount(int id, char name[], int money, char pwd[],
                char address[], int minMoney, int rate);
    void in();
    void out();
    void resetPwd();

};

Account::Account() {
    cout << "父类无参被调用了" << endl;
}

Account::Account(int id, char name[], int money) : id(id),  money(money) {
    strcpy(this->name, name);
}

void Account::inputInfo() {
    cout << "请输入姓名:" << endl;
    cin >> this->name;
    cout << "请输入存款:" << endl;
    cin >> this->money;
}

void Account::print() {
    cout << "id = " << id;
    cout << ", name = " << name;
    cout << ", money = " << money << endl;
}

Account::Account(int id) : id(id) {}
SaveAccount::SaveAccount() {
}

SaveAccount::SaveAccount(char pwd[], char address[], int minMoney, int rate) :
    minMoney(minMoney), rate(rate) {
    strcpy(this->pwd, pwd);
    strcpy(this->address, address);
}
SaveAccount::SaveAccount(int id, char name[], int money, char pwd[],
                         char address[], int minMoney, int rate) :
    Account(id, name, money), minMoney(minMoney), rate(rate) {
    strcpy(this->pwd, pwd);
    strcpy(this->address, address);

}

void SaveAccount::in() {
    inputInfo();
    cout << "请输入地址:" << endl;
    cin >> this->address;
    cout << "请输入密码:" << endl;
    cin >> this->pwd;
    cout << "请输入最少存款:" << endl;
    cin >> this->minMoney;
    cout << "请输入最少利率:" << endl;
    cin >> this->rate;
}
void SaveAccount::out() {
    print();
    cout << "address = " << this->address;
    cout << ", pwd = " << this->pwd;
    cout << ", rate = " << this->rate;
    cout << ", minMoney = " << this->minMoney << endl;

}

SaveAccount::SaveAccount(int id) : Account(id) {}

void SaveAccount::resetPwd() {
    char inputPwd[20];
    char inputPwd2[20];
    cout << "请输入旧密码:" << endl;
    cin >> inputPwd;
    if(strcmp(inputPwd, this->pwd) == 0) {
        cout << "请输入新密码:" << endl;
        cin >> inputPwd;
        cout << "请再次确认密码:" << endl;
        cin >> inputPwd2;
        if(strcmp(inputPwd, inputPwd2) == 0) {
            strcpy(this->pwd, inputPwd2);
            cout << "修改密码成功!" << endl;
        } else {
            cout << "密码输入不一致,亲,麻烦重新来!" << endl;
        }
    } else {
        cout << "旧密码输入错误,不能更改" << endl;
    }

}
int main() {
    cout << strcmp("aa", "aa") << endl;
    /*Account zhangsan;
    zhangsan.inputInfo();
    zhangsan.print();*/
    SaveAccount lisi(1);
    lisi.in();
    lisi.out();

    SaveAccount wangwu(2, "ww", 9000, "meiyou", "xianyang", 2, 2);
    wangwu.out();

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值