//Account class definition #include <iostream> #include <string> using namespace
ID: 3616543 • Letter: #
Question
//Account class definition
#include <iostream>
#include <string>
using namespace std;
class Account
{
public:
int initialBalance;
Account::Account(intbalance) //constructor to receive initialbalance
{
setInitialBalance(balance); //validate andstore initial balance
}
//validates that the initial balance is greater than 0dollars
void Account::setInitialBalance(int balance)
{
if(balance >= 0)
{
initialBalance = balance;
}
else if(balance <0)
{
initialBalance = 0;
cout << "error, initial balanceinvalid!" << endl;
}
}
int Account::credit(int) //adds an amount to the initial balance
{
}
#include <iostream>
#include <string>
#include "Account.h"
using namespace std;
int main()
{
int balance;
cout << "Enter your bank accountbalance: ";
cin >> balance;
//create two Account objects
Account balance;
Account amount(transaction);
cout << "the balance is: "<< balance.getBalance() <<endl;
return 0;
}
Explanation / Answer
This is the way I defined the class. It made things a littlemore clear to me: account.h class Account{ private: int balance; //class data members aretypically private bool verifyAmount(int amount); //Afunction to verify positive amounts can be reused later to verifycredits are also >0 public: Account(int amount); //Constructor needsto be public void credit(int amount); //Outsidecode will need to access this to add money to account int getBalance(){ returnbalance; }; //inline function. This will returnthe amount in balance. }; This is all I left in the header file. The other class memberfunctions I defined in account.cpp, with the program inmain.cpp. I had a little trouble following your class definition. Thisis just what I found easier to read. Actually, it seems that you didn't define a getBalance memberfunction. That could be part of the reason you're havingtrouble accessing the balance. You also have both an int andan Account named balance. I'm pretty sure that would causesome problems. I hoped that helped. LMK if you have other questions.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.