首页 > 试题广场 >

给出下列的银行账户类的构造函数的代码。 #inclu

[问答题]
给出下列的银行账户类的构造函数的代码。
// #include <cstring>
// class definition
class BankAccount
{
private;
   char name[40];     // or std::string name;
   char acctnum[25];  // or std::string acctnum;
   double balance;
public;
   BankAccout(const char * client, const char * num, double bal = 0.0);
//or BankAccout(const std::string & client,
//              const std::string & num,double bal = 0.0);
    void show(void) const;
    void deposit(double cash);
    void withdraw(double cash);
};

推荐
有两种可能的解决方案(要使用strncpy(),必须包含头文件cstring或string.h;要使用string类,必须包含头文件string):
BankAccount::BankAccount(const char * client, const  char * num, double bal)
{
        strncpy(name, client, 39);
        name[39] = '\0';
        strncpy(acctnum, num, 24);
        acctnum[24] = '\0';
        balance = bal;
}

或者:
BankAccount::BankAccount(const std::string & client,
                                              const std::string & num, double bal)
{
        name = client;
       acctnum = num;
       balance = bal;
}
请记住,默认参数位于原型中,而不是函数定义中。
发表于 2018-05-08 08:44:40 回复(0)