C++编程练习:设计一个银行账户类,包含户名、帐号以及当前余额属性,并且能完成开户、存款、取款和查询余额等行为。

简介: C++编程练习:设计一个银行账户类,包含户名、帐号以及当前余额属性,并且能完成开户、存款、取款和查询余额等行为。

例题


设计一个银行账户(Account)类,包含户名、帐号以及当前余额属性,可完成开户、存款、取款和查询余额等行为。银行账户类的定义要求如下:

class Account
{
public:
  Account(char name[],long num,float amount); //类的有参构造函数
  Account();            //类的无参构造函数
  void deposit(float amount);   //往账户中存款
    int withdraw(float amount);   //从账户中取款
  float getBalance();       //查询当前余额
private:
  char mName[20];           //银行账户的户名
  long mSN;                 //本账户的帐号
  float mBalance;           //本账户当前的余额
};

请根据上述给定的类,完善其相应的构造函数及成员函数的定义,并执行主函数实现测试。


分析


代码分析:

1、根据题意,我们要设计一个类Account,要将mName、mSN、mBalance均作为其成员数据,将deposit、withdraw、getBalance均作为其成员函数。类图UML设计图如下:

1666888885795.jpg

2、代码中 strcpy()即字符串复制函数,其原型是char * strcpy(char * dest,const char * src),把含有‘ \0 ’结束符的字符串复制到罗一个地址空间,即把从 src 地址开始且含有 NULL 结束符的字符串复制到以 dest 开始的地址空间,其中 src 和 dest 所指内存区域不可以重叠且 dest 有足够空间容纳 src 的字符串。


strcpy(mName, name);      //字符串复制函数

3、无参构造函数跟有参构造函数的区别,即在类里定义一个与类名相同的函数,但它的实参列表为空。

//类的有参构造函数
Account::Account(char name[], long num, float amount)
{
  strcpy(mName, name);      //字符串复制函数
  mSN = num;
  mBalance = amount;
}
//类的无参构造函数 
Account::Account()
{
  cout << "无参函数被调用!"<< endl;
}

4、取款操作中,我们要考虑账户透支,即账户余额为0的情况,这里我们定义一个 if - else if 语句,若取款金额大于卡内金额时,return 0此时函数正常终止,而当取款金额小于或等于卡内金额时,return 1此时函数异常退出,即异常退出此函数。

//从当前账户中取款 
int Account::withdraw(float amount)
{
  if (amount > mBalance)
  {
    return 0;
  }
  else if (amount <= mBalance)
  {
    mBalance = mBalance - amount; 
    return 1;                        //return 1代表函数非正常终止
  }
}

5、查询账户余额操作中,return 关键字,其作用是返回程序流程的控制权,副作用是返回一个值,这里是返回卡内金额 mBalance 的值。

//查询当前账户的余额
float Account::getBalance()
{
  return mBalance;
}

6、Account A(name, num, amount)这里我们建立了一个账户,即实例化一个对象。

int main()
{
  int NO, m;
  char name[20];
  long num;
  float amount;
  cout << "请输入所开账户户名:";
  cin >> name;
  cout << "请输入所开账户帐号:";
  cin >> num; 
  cout << "请输入所开账户初始存款金额:";
  cin >> amount;
  Account A(name, num, amount);
  ......................................
  ......................................
  ......................................
}   

代码


以下是完整的程序代码:

#include<iostream> 
using namespace std;
class Account
{
public:
  Account(char name[], long num, float amount);   //类的有参构造函数 
  Account();                    //类的无参构造函数 
  void deposit(float amount);     //往当前账户中存款 
  int withdraw(float amount);     //从当前账户中取款 
  float getBalance();         //查询当前账户的余额 
private:
  char mName[20];       //银行账户的户名 
  long mSN;           //本账户的帐号 
  float mBalance;       //本账户当前的余额 
};
//类的有参构造函数
Account::Account(char name[], long num, float amount)
{
  strcpy(mName, name);      //字符串复制函数
  mSN = num;
  mBalance = amount;
}
//类的无参构造函数 
Account::Account()
{
  cout << "无参函数被调用!" << endl;
}
//往当前账户中存款 
void Account::deposit(float amount)
{
  mBalance = mBalance + amount;
}
//从当前账户中取款 
int Account::withdraw(float amount)
{
  if (amount > mBalance)
  {
    return 0;
  }
  else if (amount <= mBalance)
  {
    mBalance = mBalance - amount; 
    return 1;              //return 1代表函数非正常终止
  }
}
//查询当前账户的余额
float Account::getBalance()
{
  return mBalance;
}
//主函数
int main()
{
  int NO, m;
  char name[20];
  long num;
  float amount;
  cout << "请输入所开账户户名:";
  cin >> name;
  cout << "请输入所开账户帐号:";
  cin >> num; 
  cout << "请输入所开账户初始存款金额:";
  cin >> amount;
  Account A(name, num, amount);   
  cout << "" << endl;
  cout << "------------------------------------------------"<<endl;
  cout << "                   菜单栏                       "<<endl;
  cout << "1、存款请输入“1”"<<endl;
  cout << ""<<endl;
  cout << "2、取款请输入“2”"<<endl;
  cout << ""<<endl;
  cout << "3、查询账户余额请输入“3”"<<endl;
  cout << ""<<endl;
  cout << "4、退出请输入“4”"<<endl;
  cout << ""<<endl;
  cout << "------------------------------------------------"<<endl;
  while (1)
  {
    cout << "请输入选择:" << endl;
    cin >> NO;  
    switch (NO)         //通过switch循环来判断输入的菜单栏选择对应其相应的操作
    { 
    case 1: 
      cout << "请输入存款金额:";
      cin >> amount;
      A.deposit(amount);
      break;                //表示跳出该switch语句体
    case 2: 
      cout << "请输入取款金额:";
      cin >> amount;
      m = A.withdraw(amount);
      if (m == 0) 
        cout << "当前账户余额不足!" << endl;
      else 
        cout << "取款成功!" << endl;
      break; 
    case 3: 
      cout << "当前账户余额为:" << A.getBalance() << endl;
      break;
    case 4: 
      cout << "账户已退出!" <<endl;
      return 0;
    default:
      cout << "输入错误!"<< endl;     //判断输入菜单栏是否输入正确
      exit(0);
    }
    cout << "" <<endl;
  }
}


测试


测试图:

1666888978315.jpg

运行成功!


结语


以上就是本次C++的全部内容,感谢您的阅读和支持,篇幅较长,若有表述或者代码中的不当之处,望指出!您的指出和建议能给作者带来很大的动力!!!


相关文章
|
4天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
19 0
|
4天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
19 0
|
1天前
|
算法 编译器 C语言
探索C++编程的奥秘与魅力
探索C++编程的奥秘与魅力
|
3天前
|
设计模式 Java C++
【C++高阶(八)】单例模式&特殊类的设计
【C++高阶(八)】单例模式&特殊类的设计
|
3天前
|
编译器 C++
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
|
7天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
9天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
9天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
|
9天前
|
存储 编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”