C++7-1-继承和派生:账户类

定义一个基类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;
}
*/

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您可以使用C语言中的结构体和指针来实现多态和继承。 首先定义一个基类账户结构体,并定义一个虚函数表,用于存储基类账户的所有虚函数: ``` typedef struct Account{ float balance; char* type; void (*deposit) (struct Account*, float); void (*withdraw) (struct Account*, float); void (*displayInfo) (struct Account*); //... } Account; typedef struct { char* name; char* address; //... } Customer; ``` 然后定义两个派生:储蓄账户和支票账户,并继承基类账户结构体中的所有属性和函数: ``` typedef struct SavingsAccount{ Account base; float interestRate; } SavingsAccount; typedef struct CheckingAccount{ Account base; float fee; } CheckingAccount; ``` 接下来,在子结构体中重新定义和实现虚函数,让它们根据自身的特性来实现不同的功能: ``` void SavingsAccountWithdraw(SavingsAccount* sa, float amount){ //实现储蓄账户的取款功能 } void CheckingAccountWithdraw(CheckingAccount* ca, float amount){ //实现支票账户的取款功能 } ``` 最后,在 main 函数中创建一个指向基类的指针,并根据需要调用虚函数来实现不同的操作: ``` int main() { SavingsAccount sa; CheckingAccount ca; Account *a = (Account*)&sa; a->deposit(a, 200.00); a->withdraw(a, 100.00); a = (Account*)&ca; a->deposit(a, 500.00); a->withdraw(a, 600.00); //... } ``` 这里只是一个简单的示例,实际的银行账户管理系统要比这个复杂得多。但是采用这种多态和继承的设计可以使程序更灵活,更容易扩展和维护。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值