In: Computer Science
......C++ PROGRAM....
Teacher would like us to split this program into a header file(filename.h), and a .cpp file(filename.cpp). He gave us all the code, we just need to split it up. I do not quite understand how to do it. Please send output screenshot to show validation.
#include <iostream>
using namespace std;
//public class
class Account{
public:
//instance variables
double amount;
//creates account and sets amount with users account set-up value
Account(double a){
amount = a;
}
//adds to amount in account once deposited
void deposit(double a){
if(a < 0){
return;
}
amount += a;
}
//decreases amount in account once withdrawn
void withdraw(double a){
if(amount-a < 0){
return;
}
amount -= a;
}
//returns balance of account
double get_balance(){
return amount;
}
};
int main()
{
Account my_account(100); // Set up my account with $100
my_account.deposit(50);
my_account.withdraw(175); // Penalty of $20 will apply
my_account.withdraw(25);
cout << "Account balance: " << my_account.get_balance() << "\n";
my_account.withdraw(my_account.get_balance()); // withdraw all
cout << "Account balance: " << my_account.get_balance() << "\n";
return 0;
}
Account.h file:
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <iostream>
using namespace std;
class Account
{
public:
double amount;
Account(double a);
void deposit(double a);
void withdraw(double a);
double get_balance();
};
#endif // ACCOUNT_H
Account.cpp file:
#include "Account.h"
Account::Account(double a){
amount = a;
}
void Account::deposit(double a){
if(a < 0){
return;
}
amount += a;
}
void Account::withdraw(double a){
if(amount-a < 0){
return;
}
amount -= a;
}
double Account::get_balance(){
return amount;
}
Main file:
#include "Account.h"
int main()
{
Account my_account(100); // Set up my account with $100
my_account.deposit(50);
my_account.withdraw(175); // Penalty of $20 will apply
my_account.withdraw(25);
cout << "Account balance: " <<
my_account.get_balance() << "\n";
my_account.withdraw(my_account.get_balance()); // withdraw
all
cout << "Account balance: " << my_account.get_balance()
<< "\n";
return 0;
}
VALIDATION:
ACCOUNT.H FILE
ACCOUNT.CPP
MAIN FILE
OUTPUT:
IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW
PLEASE GIVE A THUMBS UP