Answer to Question #314181 in C++ for doesitreallymatter

Question #314181

Write an encapsulated class Account as follows:


a) Include instance variable balance to store the amount and Title to store account


title.


b) Write 2 constructors, one will be default constructor and the second will be


augmented constructor which initializes the instance variable balance with non


negative value and Title with account name.


c) Provide a function to deposit money in the account


d) Provide a function to get the value of account


e) Also provide a function called debit that withdraws money from an Account.


Ensure that the debit amount does not exceed the Account’s balance. If it does, the


balance should be left unchanged and the method should print a message indicating


"Debit amount exceeded account balance."


Write main function to test class and function.

1
Expert's answer
2022-03-19T03:51:03-0400


#include <iostream>
#include <string>
using namespace std;




class Account{
private:
	double balance;
	string title;
public:	
	Account(){
		this->balance=0;
	}
	Account(double balance,string title){
		this->balance=balance;
		this->title=title;
	}


	void Deposit(double am){
		this->balance+=am;
	}
	void Withdraw (double am){
		if(this->balance>=am){
			this->balance-=am;
		}else{
			cout<<"Debit amount exceeded account balance.\n\n";
		}
	}


	void displayDetails(){
		cout<<"The account title is: "<<this->title<<"\n";
		cout<<"The account balance is: "<<this->balance<<"\n\n";
	}
	~Account(){


	}
};


int main(){
	double balance;
	string title;
	cout<<"Enter balance: ";
	cin>>balance;
	cin.ignore();
	cout<<"Enter title: ";
	getline(cin,title);
	Account* account=new Account(balance,title);
	int ch=-1;
	double amount;
	while(ch!=4){
		cout<<"1 - Deposit, 2 - Withdraw, 3 - Display balance, 4 - Exit: ";
		cin>>ch;
		switch(ch){
		case 1:{
			cout<<"Enter amount to deposit: ";
			cin>>amount;
			account->Deposit(amount);
			   }
			   break;
		case 2:{
			cout<<"Enter amount to withdraw: ";
			cin>>amount;
			account->Withdraw(amount);
			   }


			   break;
		case 3:{
			account->displayDetails();
			   }
			   break;
		case  4:{
			//exit
				}
				break;
		default:{


				}
				break;
		}
	}


	delete account;
	system("pause");
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog