Virtual Template

#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<saving> {
	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);
}



/* Output */
Account Holder Name: Jonh
Balance: 5230.65


<---------------------------------------------->


Account Holder Name: Ashok
Interest: 139
Balance: 1689.55

Comments (0)

  • To add your comment please or

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.

Got It!