码迷,mamicode.com
首页 > 其他好文 > 详细

第十章:对象和类

时间:2015-01-20 23:29:36      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

10.2题

定义一个类来表示银行账户

 1 #include<iostream>
 2 using namespace std;
 3 class BankAccount
 4 {
 5     string m_name;
 6     string m_num;
 7     unsigned int m_balance;
 8     public:
 9             BankAccount(string name,string num,unsigned balance)
10             {
11                 m_name = name;
12                 m_num = num;
13                 m_balance = balance;
14             }
15             void show() const
16             {
17                 cout << m_name << endl;
18                 cout << m_num << endl;
19                 cout << m_balance << endl;
20             }
21             void inDeposit(unsigned amount)
22             {
23                 m_balance = m_balance + amount;
24             }
25             void deDeposit(unsigned amount)
26             {
27                 if(m_balance < amount)
28                     m_balance = 0;
29                 else
30                     m_balance = m_balance - amount;
31             }
32             
33 };
34 int main()
35 {
36     BankAccount bank("Jack","123",10000);
37     bank.show();
38     bank.inDeposit(1000);
39     bank.show();
40     bank.deDeposit(1500);
41     bank.show();
42 }

 

第十章:对象和类

标签:

原文地址:http://www.cnblogs.com/fudianheg/p/4237522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!