In: Computer Science
c++
The above Account class was created to model a bank account. An
account has the properties (keep the properties private) account
number, balance, and annual interest rate, date created, and
functions to deposit and withdraw. Create two derived classes for
checking and saving accounts. A checking account has an overdraft
limit, but a savings account cannot be overdrawn. Define a constant
virtual toString() function in the Account class and override it in
the derived classes to return the account number and balance as a
string.
Implement the classes. Write a test program that creates objects of
Account, SavingsAccount, and CheckingAccount and invokes their
toString() functions.
#include <iostream>
#include<sstream>
using namespace std;
class Account{ //account class having all details of
account
int AccNo=0; //variable
double balance=0;//variable
double interest=0;//variable
string date=""; //variable
public:
Account(){ //constructor
}
Account(double a,double b){ //constructor
AccNo=a; //private member assign value
balance=b; //private member assign value
}
void deposit(double amt){ //function for deposit
balance=balance+amt;
}
void withdraw(double amt){ //function for withdraw
if(amt<=balance){ //checking if amount is sufficient for
withdrawal
balance=balance-amt;}
else{
cout<<"Insufficient amount"<<endl;
}
}
int getAcc(){ //getting AccNo
return AccNo;
}
void setAcc(double AccNo1){ //setting AccNo
AccNo=AccNo1;
}
int getbalance(){ ///getting balance
//cout<<balance;
return balance;
}
void setbalance(double balance1){ //setting balance
balance=balance1;
}
virtual string toString(){ //function that to display the details
of Account
stringstream ss; //stringstream object
ss<<AccNo;//streaming the intger to object
string a;
ss>>a;//output to a string
stringstream ss1; //stringstream object
ss1<<balance; //streaming the intger to object
string b;
ss1>>b; //output to a string
string c="Account number: "+a+" and balance: "+b+" ";
return c;
}
};
class Checking:public Account{//inheritance
double overdraft=0;
public:
Checking(double a,double b){//constructor
setAcc(a);//setter
setbalance(b); //setter
}
string toString(){
//cout<<getbalance();
stringstream ss; //stringstream object
ss<<getAcc(); //streaming the intger to object
string a;
ss>>a; //output to a string
stringstream ss1; //stringstream object
ss1<<getbalance(); //streaming the intger to object
string b;
ss1>>b; //output to a string
//cout<<b;
string c="Account number: "+a+" and balance: "+b+" ";
return c;
}
};
class Saving:public Account{ //saving account
public:
Saving(double a,double b){ //constructor
setAcc(a); //setting AccNo.
setbalance(b); //settin balance
}
string toString(){ //display details of account
stringstream ss;
ss<<getAcc();
string a;
ss>>a;
stringstream ss1;
string b;
ss1<<getbalance();
ss1>>b;
//cout<<b;
string c="Account number: "+a+" balance: "+b+" ";
return c;
}
};
int main()
{
Saving s(639745,5000);//instance of saving class
Checking d(7146372,6000); //instance of checking class
Account c(724642,10000); //instance of Account class
cout<<s.toString()<<endl;
cout<<d.toString()<<endl;
cout<<c.toString()<<endl;
return 0;
}
If you found this answer helpful please give a thumbs up.