继承与派生
来源:互联网 发布:发型p图软件 编辑:程序博客网 时间:2024/06/10 04:59
///刚看了继承和派生 感觉懵懵哒~///13.1 简单的基类/*#include <iostream>#include<cstring>using namespace std;class TableTennisPlayer{private: enum{LIM = 20}; char firstname[LIM]; char lastname[LIM]; bool hasTable;public: TableTennisPlayer(const char *fn = "none", const char *ln = "none", bool ht = false); void Name()const; bool HasTable()const { return hasTable; } void ResetTable(bool v) { hasTable = v; }};TableTennisPlayer::TableTennisPlayer(const char *fn, const char* ln, bool ht){ strcpy(firstname,fn); firstname[LIM-1] = '\0'; strcpy(lastname, ln); lastname[LIM-1] = '\0'; hasTable = ht;}void TableTennisPlayer::Name()const{ cout << lastname << ", " << firstname;}class RatedPlayer: public TableTennisPlayer{private: unsigned int rating;public: RatedPlayer(unsigned int r = 0, const char* fn = "none", const char *ln = "none", bool ht = false); RatedPlayer(unsigned int r,const TableTennisPlayer & tp); unsigned int Rating() { return rating; } void ResetRating(unsigned int r) { rating = r; }};RatedPlayer::RatedPlayer(unsigned int r,const char *fn, const char *ln, bool ht): TableTennisPlayer(fn, ln, ht){ rating = r;}RatedPlayer::RatedPlayer(unsigned int r, const TableTennisPlayer & tp):TableTennisPlayer(tp), rating(r){}int main(){ TableTennisPlayer player1("zheng", "zeyun", false); RatedPlayer rplayer1(1140,"zhang", "ying", true); rplayer1.Name(); if(rplayer1.HasTable()) cout << ":has a table" << endl; else cout << ":hasn't a table" << endl; player1.Name(); if(player1.HasTable()) cout << ":has a table" << endl; else cout << ":hasn't a table" << endl; cout << "Name: "; rplayer1.Name(); cout << ";Rating:" << rplayer1.Rating() << endl; RatedPlayer rplayer2(1440, player1); cout << "Name: "; rplayer2.Name(); cout << ";Rating:" << rplayer2.Rating() << endl; return 0;}*//*ying, zhang:has a tablezeyun, zheng:hasn't a tableName: ying, zhang;Rating:1140Name: zeyun, zheng;Rating:1440Process returned 0 (0x0) execution time : 6.777 sPress any key to continue.*////13.2派生类和基类之间的特殊关系///13.3继承 is-a关系(is a kind of)///13.4多态共有继承///在两个类中行为相同的方法 只需在基类中声明#include<iostream>#include<cstring>using namespace std;class Brass{private: enum{MAX = 35}; char fullName[MAX]; long acctNum; double balance;public: Brass(const char *s = "Nullbody", long an = -1, double bal = 0.0); void Deposit(double amt); virtual void Withdraw(double amt); double Balance()const; virtual void ViewAcct()const; virtual ~Brass(){}};class BrassPlus: public Brass{private: double maxLoan; double rate; double owesBank;public: BrassPlus(const char *s="Nullbody",long an= -1,double bal = 0.0,double ml = 500, double r = 0.10); BrassPlus(const Brass & ba, double ml = 500, double r = 0.1); virtual void ViewAcct()const; virtual void Withdraw(double amt); void ResetMax(double m) { maxLoan = m; } void ResetRate(double r) { rate = r; } void ResetOwes() { owesBank = 0; }};Brass::Brass(const char *s, long an, double bal){ strncpy(fullName, s, MAX-1); fullName[MAX-1] = '\0'; acctNum = an; balance = bal;}void Brass::Deposit(double amt){ if(amt < 0) cout << "Negative deposit not allowed; " << "deposit is canceled" << endl; else balance += amt;}void Brass::Withdraw(double amt){ if(amt < 0) cout << "Withdrawal amount must be positive; " <<"withdrawal canceled." << endl; else if(amt <= balance) balance -= amt; else cout << "Withdrawal amount of $" << amt << " exceeds your balance." << endl << "Withdrawal canceled" << endl;}double Brass::Balance()const{ return balance;}void Brass::ViewAcct()const{ cout << "Client: " << fullName << endl; cout << "Account Number: " << acctNum << endl; cout << "Balance: $" << balance << endl;}BrassPlus::BrassPlus(const char *s, long an, double bal, double ml, double r):Brass(s, an, bal){ maxLoan = ml; owesBank = 0.0; rate = r;}void BrassPlus::ViewAcct()const{ Brass::ViewAcct(); cout << "Maxinum loan: $" << maxLoan << endl; cout << "Owed to bank: $" << owesBank << endl; cout << "Loan Rate: " << 100*rate << "%" << endl;}void BrassPlus::Withdraw(double amt){ double bal = Balance(); if(amt <= bal) Brass::Withdraw(amt); else if(amt <= bal + maxLoan - owesBank) { double advance = amt - bal; owesBank += advance *(1.0 + rate); cout << "Bank advance: $" << advance << endl; cout << "Finance charge: $" << advance * rate << endl; Deposit(advance); Brass::Withdraw(amt); } else { cout << "Credit limit exceeded.Transaction canceled" << endl; }}int main(){ Brass Piggy("Porcelot Pigg", 381299, 4000.00); BrassPlus Hoggy("Horatio Hogg", 382288, 3000.00); Piggy.ViewAcct(); cout << endl; Hoggy.ViewAcct(); cout << endl; cout << "Depositing $1000 into the Hogg Account:" << endl; Hoggy.Deposit(1000); cout << "New balance: $" << Hoggy.Balance() << endl; cout << "Withdrawing $4200 from the Pigg Account:" << endl; Piggy.Withdraw(4200.00); cout << "Pigg account balance: $" << Piggy.Balance() << endl; cout << "Withdrawing $4200 from the Hogg Account:" << endl; Hoggy.Withdraw(4200.00); Hoggy.ViewAcct(); return 0;}/*Client: Porcelot PiggAccount Number: 381299Balance: $4000Client: Horatio HoggAccount Number: 382288Balance: $3000Maxinum loan: $500Owed to bank: $0Loan Rate: 10%Depositing $1000 into the Hogg Account:New balance: $4000Withdrawing $4200 from the Pigg Account:Withdrawal amount of $4200 exceeds your balance.Withdrawal canceledPigg account balance: $4000Withdrawing $4200 from the Hogg Account:Bank advance: $200Finance charge: $20Client: Horatio HoggAccount Number: 382288Balance: $0Maxinum loan: $500Owed to bank: $220Loan Rate: 10%Process returned 0 (0x0) execution time : 4.455 sPress any key to continue.*/
0 0
- 派生类与继承
- C++继承与派生
- C++ [ 继承与派生 ]
- 继承与派生
- 继承与派生
- 继承与派生
- 继承与派生
- C++ 继承与派生
- c++继承与派生
- 继承与派生
- 派生与继承
- c++继承与派生
- 20、继承与派生
- c++继承与派生
- C++继承与派生
- 继承与派生-撞错
- 继承与派生
- C++继承与派生
- 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
- 【我的Android进阶之旅】解决 Error:CreateProcess error=216, 该版本的 %1 与您运行的 Windows 版本不兼容。请查看计算机的系统信息,了解是否需要 x86
- Hibernate session错误:
- 第一次写生产者消费者问题
- BZOJ1801: [Ahoi2009]chess 中国象棋
- 继承与派生
- Android之SQLite数据库的使用
- 解决百度地图过段时间只显示网格的问题
- MySQL数据库密码篇
- c语言实现strcpy
- hadoop RPC
- 封装SQL访问方法
- 欢迎使用CSDN-markdown编辑器
- jquery.pagination分页反复提交的问题