Write a C++ function for the following:
Suppose that Account class has a method called withdraw, which will be inherited by Checking and Savings class. The withdraw method will perform according to account type. So, the late bind is needed. Declare the withdraw method in Account class. The method should take a double input as withdraw amount and output true if the withdraw successfully and false if the withdraw unsuccessfully.

Respuesta :

Solution :

class Account

[tex]$ \{ $[/tex]

public:

[tex]$\text{Account}()$[/tex];

double [tex]$\text{getBalance}$[/tex]();

void [tex]$\text{setBalance}$[/tex]();

[tex]$\text{bool withdraw}$[/tex](double bal);

[tex]$\text{private}:$[/tex]

double [tex]$\text{balance}$[/tex];

}:

[tex]$\text{Account}()$[/tex] {}

double [tex]$\text{getBalance}$[/tex]()

[tex]$ \{ $[/tex]

[tex]$\text{return balance}$[/tex];

}

void [tex]$\text{setBalance}$[/tex](double [tex]$\text{balance}$[/tex])

[tex]$ \{ $[/tex]

this.[tex]$\text{balance}$[/tex] = [tex]$\text{balance}$[/tex];

}

[tex]$\text{boolean}$[/tex] withdraw([tex]$\text{double bal}$[/tex])

[tex]$ \{ $[/tex]

if([tex]$\text{balance}$[/tex] >= bal)

[tex]$ \{ $[/tex]

[tex]$\text{balance}$[/tex] = [tex]$\text{balance}$[/tex] - bal;

[tex]$\text{return}$[/tex] true;

}

[tex]$\text{return}$[/tex] false;

}

}

ACCESS MORE