Lập Trình C/C++ - Virtual Template

#Code:
#include<iostream>
using namespace std;

class currentAcc {};

class savingAcc
{
 public:
  virtual void display(string,float) {}
};

template <class vAcc>
class account : private vAcc
{
 public:
  /* Virtuality of display() depends on its declaration in the base class 'vAcc' */
  void display(string name, float bal)
  {
   cout << "Account Holder Name: " << name << endl;
   cout << "Balance: " << bal << endl;
  }
};

template <class saving>
class interestAcc : public account
{
 public:
  void display(string name, float bal)
  {
   /* Calculate 9% interest */
   int interest = bal * .09;
   cout << "Account Holder Name: " << name << endl;
   cout << "Interest: " << interest << endl;
   cout << "Balance: " << bal+interest << endl;
  }
};

int main()
{
 account<currentAcc> *acc1 = new interestAcc<currentAcc>;

 /* Call display() from account */
 acc1->display("Jonh", 5230.65);
 cout << "\n<---------------------------------------------->\n" << endl;
 account<savingAcc> *acc2 = new interestAcc<savingAcc>;

 /* Call display() from 'interestAcc' */
 acc2->display("Ashok", 1550.55);
}

0 Comment:

Đăng nhận xét

Thank you for your comments!