定义一个基类Account,数据成员包含string类变量userName用于保存账户主人姓名,函数成员包括默认构造函数、带参构造函数用于初始化数据成员和输出姓名的成员函PrintUserName()。从Account类派生出CreditAccount类,增加整型数据成员credit用于记录该用户信用额度,函数成员包括带参构造函数用于初始化数据成员和输出账户信息的成员函数PrintInfo()。要求:在函数PrintInfo()中需要调用基类的成员函数PrintUserName()。
下面是一份可供你填充和测试的代码:
#include <iostream>
#include <string>
using namespace std;
class Account
{
public:
Account(){}
Account(string name);
void PrintUserName();
private:
string userName;
};
class CreditAccount : public Account
{
public:
CreditAccount(string name, int credit);
void PrintInfo();
private:
int credit;
};
//=============================================
// 请实现Account构造函数Account(string name)
Account::Account(string name) { }
// 请实现Account的PrintUserName()函数
void Account::PrintUserName() { }
// 请实现CreditAccount类的构造函数CreditAccount(string name, int credit)
CreditAccount::CreditAccount(string name, int credit) { }
// 请实现CreditAccount类的PrintInfo()函数
void CreditAccount::PrintInfo() { }
//=============================================
int main()
{
CreditAccount a("I Love CPP", 10000);
a.PrintInfo();
return 0;
}
为了确保你按要求编写程序,我们将使用OJ的代码模板功能。
在提交代码时,只能提交代码中要求你填充的四个成员函数,绝不要提交其他的任何代码;在上方的代码中你可以看到两条分隔线一样的注释,你只要提交这两条分隔线中间的部分即可。在提交时你也会看到一份代码模板,其中就是四个空的函数;如果你打算在线编程,那么只需要在大括号中填写对应的代码,以及增加对基类构造函数的调用就行了。
你的代码提交后,会首先替换掉上方代码两条分隔线中间的部分,然后再编译运行。
务必按要求实现每个成员函数,不要尝试直接输出“I love CPP”和10000,否则你可能会得0分。
输入描述
你不知道程序会收到何种输入。你需要做的只是实现题目中要求你实现的四个函数。
输出描述
调用Account::PrintUserName()时,你需要输出一行,包含账户姓名,然后换行。
调用CreditAccount::PrintInfo()时,你需要输出两行,第一行为账户姓名,第二行为账户信用额度;第二行的结尾也要换行。
示例1:
输入:无
输出: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;
}
//请实现Account的PrintUserName()函数
void Account::PrintUserName()
{
cout << userName << endl; // 输出账户名
}
//请实现CreditAccount类的构造函数CreditAccount(string name, int credit)
CreditAccount::CreditAccount(string name,int credit):Account(name),credit(credit){}
//请实现CreditAccount类的PrintInfo()函数
void CreditAccount::PrintInfo()
{
PrintUserName(); // 调用基类的公有成员函数
cout << credit << endl; // 输出账户信用
}
//=============================================
/*
int main()
{
CreditAccount a("I Love CPP", 10000);
a.PrintInfo();
return 0;
}
*/