1. 程式人生 > >C7-1 賬戶類(100/100)

C7-1 賬戶類(100/100)

edi str 成員函數 strong using int 默認 構造函數 span

題目描述

定義一個基類Account,數據成員包含string類變量userName用於保存賬戶主人姓名,函數成員包括默認構造函數、帶參構造函數用於初始化數據成員和輸出姓名的成員函PrintName()。從Account類派生出CreditAccount類,增加整型數據成員credit用於記錄該用戶信用額度,函數成員包括帶參構造函數用於初始化數據成員和輸出賬戶信息的成員函數PrintInfo()。要求:在函數PrintInfo()中需要調用基類的成員函數PrintName()。填充以下代碼:

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

class Account
{
string userName;
public:
Account(){};
Account( string name );
void PrintUserName();
};

class CreditAccount : public Account
{
public:
CreditAccount( string name, int credit);
void PrintInfo();
private:
int credit;
};

//請實現Account構造函數Account(string name)
//請實現Account的PrintUserName()函數
//請實現CreditAccount類的構造函數CreditAccount(string name, long number)
//請實現CreditAccount類的PrintInfo()函數

int main()
{
CreditAccount a("I Love CPP", 10000);
a.PrintInfo();
return 0;
}



輸入描述


輸出描述

輸出共兩行,第一行為賬戶姓名,第二行為賬戶信用額度



樣例輸入


樣例輸出

I Love CPP
10000
#include <iostream>
#include <string>
using namespace std;
    
class Account{ 
    string userName;
public:
    Account(){};
    Account( string name );
    
void PrintUserName(); }; class CreditAccount : public Account{ public: CreditAccount( string name, int credit); void PrintInfo(); private: int _credit; }; Account::Account( string name ){//請實現Account構造函數Account(string name) userName=name; } void Account::PrintUserName(){//請實現Account的PrintUserName()函數
cout<<userName<<endl; } CreditAccount::CreditAccount( string name, int credit):Account(name){//請實現CreditAccount類的構造函數CreditAccount(string name, long number) _credit=credit; } void CreditAccount::PrintInfo(){//請實現CreditAccount類的PrintInfo()函數 Account::PrintUserName(); cout<<_credit<<endl; } int main(){ CreditAccount a("I Love CPP", 10000); a.PrintInfo(); return 0; }

C7-1 賬戶類(100/100)