Object :Declaring a class and using it. Write a new Account class to include Pub
ID: 3548070 • Letter: O
Question
Object :Declaring a class and using it.
Write a new Account class to include
Public variables: string accHolderName, string accHolderAddress, private variable: accBalance
Public member: functions showCurrAddress(), showCurHolder(), showAllInfo(), accDeposite(), accWithdrawal(), showCurrBal(),
Write main to create three instances of class Account. Then use the member use constructors (one is written for you, write the other one, which passes values accHolderName, accHolderAdress )and initialize the accBalance = 25000.
Then use deposit, withdrawal, showCurrBal showAllInfo as instructed in the main function.
A directional .cpp code is also attached in the file.
Class will be declared in some manner like this. This is incomplete, complete it.
class Account{
public:
string accHolderName, accHolderAddress;
private:
double accBalance;
public:
Account(){
accHolderName= " ";
accHolderAddress = " ";
accBalance=0.0;
}
Account(string a, string b){
//intialize the values
accBalance=25000.0;
}
function //Show current address;
function //Show Current Holder
}
void showAllInfo(){
cout<< "Account Holder name = "<< accHolderName <<endl;
cout<< "Account Holder Address = "<< accHolderAddress <<endl;
showCurrBal();
cout<< endl<<endl;
}
void accDeposite(double mon_amt)
{
//Update the accBalance with deposited amount
}
void accWithdrawal(){
//withdawing money
}
void showCurrBal();
};
If you want you can change the program as you want.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
class Account {
double accBalance; //By default the members are private in class
public :
char accHolderName[30],accHolderAddress[50];
Account() {
accHolderName = "";
accHolderAddress = "";
accBalance = 0.0;
}
Account(char name[], char address[]) {
strcpy(accHolderName,name);
strcpy(accHolderAddress,address);
accBalance = 25000.0;
}
void showCurrAddress() {
cout<<" Current Address : "<<accHolderAddress;
}
void showCurHolder() {
cout<<" Account Holder : "<<accHolderName;
}
void showAllInfo() {
cout<<" Account Holder : "<<accHolderName;
cout<<" Current Address : "<<accHolderAddress;
cout<<" Current Balance : "<<accBalance;
}
void accDeposite(double deposite) {
accBalance = accBalance + deposite;
cout<<" Deposit successful :)";
}
void accWithdrawal(double withdrawal) {
if(withdrawal < accBalance) {
accBalance = accBalance - withdrawal;
cout<<" Withdrawal successful :)";
}
else {
cout<<" Not sufficient balance !!!!";
return;
}
}
void showCurrBal() {
cout<<" Current Balance : "<<accBalance;
}
};
int main() {
Account instance1("Xander Cage","Miami Florida USA");
Account instance2("X Cage","Chicago Illinois USA");
Account instance3("Xander C","LA California USA");
//You can call the functtions for any instance you want, I have taken here some for example
instance1.deposit(2500);
instance2.withdrawal(3000);
instance3.showCurrBal();
instance2.showAllInfo();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.