#ifndef _ACCTABC_H_
#define _ACCTABC_H_
//(*
#include <iostream>
#include <string>
//*)
//Abstract Base Class
class AcctABC
{
private:
std::string fullName;
long acctNum;
double balance; protected:
struct Formatting
{
std::ios_base::fmtflags flag;
std::streamsize pr;
};
const std::string & FullName(void) const {return fullName;}
long AcctNum(void) const {return acctNum;}
Formatting SetFormat(void) const;
void Restore(Formatting & f) const; public:
AcctABC(const std::string & s = "Nullbody", long an = -, double bal = 0.0);
void Deposit(double amt);
virtual void Withdraw(double amt) = ; //pure virtual function
double Balance(void) const {return balance;};
virtual void ViewAcct(void) const = ; //pure virtual function
virtual ~AcctABC() {}
}; //Brass Account Class
class Brass : public AcctABC
{
public:
Brass(const std::string & s = "Nullbody", long an = -, double bal = 0.0) : AcctABC(s, an, bal) {}
virtual void Withdraw(double amt);
virtual void ViewAcct(void) const;
virtual ~Brass() {}
}; //Brass Plus Account Class
class BrassPlus : public AcctABC
{
private:
double maxLoan;
double rate;
double owesBank; public:
BrassPlus(const std::string & s = "Nullbodt", long an = -, double bal = 0.0, double ml = , double r = 0.10);
BrassPlus(const Brass & ba, double ml = , double r = 0.1);
virtual void ViewAcct(void) const;
virtual void Withdraw(double amt);
void ResetMax(double m) {maxLoan = m;}
void ResetRate(double r) {rate = r;}
void ResetOwes(void) {owesBank = ;}
}; #endif
#include <iostream>
#include "acctabc.h"
using std::cout;
using std::ios_base;
using std::endl;
using std::string; //Abstract Base class
AcctABC::AcctABC(const string & s, long an, double bal)
{
fullName = s;
acctNum = an;
balance = bal;
} void AcctABC::Deposit(double amt)
{
if(amt < )
{
cout << "Negative deposit not allowed; deposit is cancelled." << endl;
}
else
{
balance += amt;
}
} void AcctABC::Withdraw(double amt)
{
balance -= amt;
} //protected methods for formatting
AcctABC::Formatting AcctABC::SetFormat(void) const
{
//set up ###.## format
Formatting f;
f.flag = cout.setf(ios_base::fixed, ios_base::floatfield);
f.pr = cout.precision();
return f;
} void AcctABC::Restore(Formatting & f) const
{
cout.setf(f.flag, ios_base::floatfield);
cout.precision(f.pr);
} //Brass methods
void Brass::Withdraw(double amt)
{
if(amt < )
{
cout << "Withdrawal amount must be positive; withdrawal canceled." << endl;
}
else if(amt < Balance())
{
AcctABC::Withdraw(amt);
}
else
{
cout << "Withdrawal amount of $" << amt << " exceeds your balance." << endl << "Withdrawal canceled." << endl;
}
} void Brass::ViewAcct(void) const
{
Formatting f = SetFormat();
cout << "Brass Client: " << FullName() << endl;
cout << "Account Number: " << AcctNum() << endl;
cout << "Balance: $" << Balance() << endl;
Restore(f);
} //BrassPlus Methods
BrassPlus::BrassPlus(const string & s, long an, double bal, double ml, double r) : AcctABC(s, an, bal)
{
maxLoan = ml;
owesBank = 0.0;
rate = r;
} BrassPlus::BrassPlus(const Brass & ba, double ml, double r) : AcctABC(ba)
{
maxLoan = ml;
owesBank = 0.0;
rate = r;
} void BrassPlus::ViewAcct(void) const
{
Formatting f = SetFormat(); cout << "BrassPlus Client: " << FullName() << endl;
cout << "Account Number: " << AcctNum() << endl;
cout << "Balance: $" << Balance() << endl;
cout << "Maxinum loan: $" << maxLoan <<endl;
cout << "Owed to bank: $" << owesBank << endl;
cout.precision();
cout << "Loan Rate: " << * rate << "%\n";
Restore(f);
} void BrassPlus::Withdraw(double amt)
{
Formatting f = SetFormat(); double bal = Balance();
if(amt <= bal)
{
AcctABC::Withdraw(amt);
}
else if(amt <= bal + maxLoan - owesBank)
{
double advance = amt - bal;
owesBank += advance * (1.0 + rate);
cout << "Bank advance: $" << advance * rate << endl;
Deposit(advance);
AcctABC::Withdraw(amt);
}
else
{
cout << "Credit limit exceeded. Transaction cancelled." << endl;
}
Restore(f);
}
#include <iostream>
#include <string>
#include "acctabc.h"
const int CLIENTS = ; int main(void)
{
using std::cin;
using std::cout;
using std::endl; AcctABC * p_clients[CLIENTS];
std::string temp;
long tempnum;
double tempbal;
char kind; for(int i = ; i < CLIENTS; i++)
{
cout << "Enter client's name: ";
getline(cin, temp);
cout << "Enter client's account number: ";
cin >> tempnum;
cout << "Enter opening balance: $";
cin >> tempbal;
cout << "Enter 1 for Brass Account or 2 for BrassPlus Account: "; while(cin >> kind && (kind != '' && kind != ''))
{
cout << "Enter either 1 or 2: ";
} if(kind == '')
{
p_clients[i] = new Brass(temp, tempnum, tempbal);
}
else
{
double tmax, trate;
cout << "Enter the overdraft limit: $";
cin >> tmax;
cout << "Enter the interest rate " << "as a decimal fraction: ";
cin >> trate;
p_clients[i] = new BrassPlus(temp, tempnum, tempbal, tmax, trate);
} while(cin.get() != '\n')
{
continue;
}
}
cout << endl;
for(int i = ; i < CLIENTS; i++)
{
p_clients[i]->ViewAcct();
cout << endl;
} for(int i = ; i < CLIENTS; i++)
{
delete p_clients[i];
}
cout << "DONE.\n"; return ;
}

抽象基类(ABC),纯虚函数的更多相关文章

  1. C++:抽象基类和纯虚函数的理解

    转载地址:http://blog.csdn.net/acs713/article/details/7352440 抽象类是一种特殊的类,它是为了抽象和设计的目的为建立的,它处于继承层次结构的较上层. ...

  2. (转)(C++)关于抽象基类和纯虚函数

    ★抽象类:一个类可以抽象出不同的对象来表达一个抽象的概念和通用的接口,这个类不能实例化(创造)对象. ★纯虚函数(pure virtual):在本类里不能有实现(描述功能),实现需要在子类中实现.例: ...

  3. 4.6 C++抽象基类和纯虚成员函数

    参考:http://www.weixueyuan.net/view/6376.html 总结: 在C++中,可以通过抽象基类来实现公共接口 纯虚成员函数没有函数体,只有函数声明,在纯虚函数声明结尾加上 ...

  4. C++入门经典-例8.9-抽象类,纯虚函数,创建纯虚函数

    1:包含有纯虚函数的类称为抽象类,一个抽象类至少具有一个纯虚函数.抽象类只能作为基类派生出的新的子类,而不能在程序中被实例化(即不能说明抽象类的对象),但是可以使用指向抽象类的指针.在程序开发过程中并 ...

  5. C++基础知识 基类指针、虚函数、多态性、纯虚函数、虚析构

    一.基类指针.派生类指针 父类指针可以new一个子类对象 二.虚函数 有没有一个解决方法,使我们只定义一个对象指针,就可以调用父类,以及各个子类的同名函数? 有解决方案,这个对象指针必须是一个父类类型 ...

  6. OOP2(虚函数/抽象基类/访问控制与继承)

    通常情况下,如果我们不适用某个函数,则无需为该函数提供定义.但我们必须为每个虚函数都提供定义而不管它是否被用到了,这因为连编译器也无法确定到底会适用哪个虚函数 对虚函数的调用可能在运行时才被解析: 当 ...

  7. C++纯虚函数

    本文较为深入的分析了C++中虚函数与纯虚函数的用法,对于学习和掌握面向对象程序设计来说是至关重要的.具体内容如下: 首先,面向对象程序设计(object-oriented programming)的核 ...

  8. c/c++ 基金会(七) 功能覆盖,虚函数,纯虚函数控制

    1.功能覆盖 ClassA , ClassB ,其中ClassB继承ClassA 类的定义如下面的: #ifndef _CLASSA_H #define _CLASSA_H #include < ...

  9. C++ 虚函数 、纯虚函数、接口的实用方法和意义

    也许之前我很少写代码,更很少写面向对象的代码,即使有写多半也很容易写回到面向过程的老路上去.在写面向过程的代码的时候,根本不管什么函数重载和覆盖,想到要什么功能就变得法子的换个函数名字,心里想想:反正 ...

随机推荐

  1. 笔试之Linux命令的使用

    1. awk文本处理工具,显示ps的最后两列 ps -ef|awk '{print $1,$2}' 打印第一和第二域  $0是全域 2. Linux下查看内存使用情况 free

  2. Effective C++ 总结(三)

    五.实现  条款26:尽可能延后变量定义式的出现时间 如果你定义了一个变量且该类型带一个构造函数或析构函数,当程序到达该变量时,你要承受构造成本,而离开作用域时,你要承受析构成本.为了减少这个成本,最 ...

  3. http状态码对应表

    http状态码 1**:请求收到,继续处理 2**:操作成功收到,分析.接受 3**:完成此请求必须进一步处理 4**:请求包含一个错误语法或不能完成 5**:服务器执行一个完全有效请求失败 100— ...

  4. SPOJ 4053 - Card Sorting 最长不下降子序列

    我们的男主现在手中有n*c张牌,其中有c(<=4)种颜色,每种颜色有n(<=100)张,现在他要排序,首先把相同的颜色的牌放在一起,颜色相同的按照序号从小到大排序.现在他想要让牌的移动次数 ...

  5. spring aop pointcut 切入点是类的公共方法(私有方法不行),还是接口的方法

    spring aop pointcut 切入点是类的公共方法(私有方法不行),还是接口的方法 类的公共方法可以,但是私有方法不行 测试一下接口的方法是否能够捕捉到

  6. 注册表修改IP地址和DNS等信息

    ---------------------win8系统 1. 2. 3. --------------------------------------------------------------- ...

  7. cxf客户端代码设置设置访问用户名、密码、证书域名不匹配认证通过

    最近和第三方联调,需要调用对方的wsdl,但是调用必须的设置用户名.密码验证.在soapUI里面设置用户名.密码调用通过.但是怎么转换成JAVA代码呢,搜索了好多解决方案,现将代码截图如下: 1.SO ...

  8. CentOS7 开启关闭网卡

    ifdown ifcfg-enp7s0 关闭网卡 ifup ifcfg-enp7s0 开启网卡

  9. 正确设置网站title、keywords、description(转载)

    本文转载自蚂蚁HR(www.mayihr.com) 优化技巧是老师在课堂上教不了你的,而自己也不可能在练习中领悟,最便捷的方法就是听取别人的经验,所以转载一下 1.title(网站标题) title, ...

  10. jquery利用event.which方法获取键盘输入值的代码

    jquery利用event.which方法获取键盘输入值的代码,需要的朋友可以参考下. 实例 显示按了哪个键: $("input").keydown(function(event) ...