Account Class C++

Any help is greatly appreciated, thank you

Design a class named Account with Account.h (including class data fields and function prototype) and Account.cpp (including constructor and function definitions) that contains:
1.A private int data field named id for the account (default 0).
2.A private double data field named balance for the account (default 0).
3.A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate.
4.A constructor that creates an account with the specified id and initial balance
5.The accessor (get) and mutator (set) methods for id, balance, and annualInterestRate.
6.A method named getMonthlyInterestRate() that returns the monthly interest rate.
7.A method named getMonthlyInterest() that returns the monthly interest.
8.A method named withdraw that withdraws a specified amount from the account.
9.A method named deposit that deposits a specified amount to the account.
10.withdraw() should check available balance before withdraw. Throw an Exception if there is no sufficient balance.
11. All getter functions should add the const keyword
Write a test program that creates five Account objects. The account IDs are 1000 to 5000, balance of $1,000 to 5000, and an annual interest rate of 3%. Use the withdraw method to withdraw $500, use the deposit method to deposit $1000, and print the account ID, the monthly interest, and the balance for each account via object name.
Print out account ID and account balance information via object references and object pointers.
(Hints)
The method getMonthlyInterest() is to return monthly interest, not the interest rate. Monthly interest is balance * monthlyInterestRate. monthlyInterestRate is annualInterestRate / 12. Note that annualInterestRate is a percentage, e.g.,like 3%. You need to divide it by 100.)
withdraw() should check available balance before withdraw. Throw an Exception if there is no sufficient balance.
You may create five Account objects, or create one size=5 Account array: Account accountArray[5]; or array<Account, 5> accountArray;
Sample output: https://imgur.com/a/KkODJ
===============
So far I understand how to create the header file and the .cpp file with the function definitions and constructor, but I am having trouble understanding how to
create the test program that creates five Account objects,Print out account ID and account balance information via object references and object pointers within the main.cpp file Int main()

my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 // Account.h
#ifndef Account_h
#define Account_h

class Account
{
	int id = 0;
	double balance = 0.0;
	double annualInterestRate = 0.0; 

public:

	Account(int = 0, double = 0.0); // Constructor

	// set functions 
	void setID(int); // set Account ID
	void setBalance(double); // set Account Balance
	void setAnnualInterestRate(double); // Set Annual Interest Rate

	// get functions
	unsigned int getID() const;
	double getBalance() const;
	double getAnnualInterestRate() const;

	double getMonthlyInterestRate() const; 
	double getMonthlyInterest() const;  

	void withdraw (double);
	void deposit (double);

};
#endif // Account_h 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 // Account.cpp
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include "Account.h"
using namespace std;

Account::Account(int id, double balance) // constructor initialises each data member

{
	setID(id);
	setBalance(balance);
}

void Account::setBalance(double newbalance)
{
	balance = newbalance;
}

void Account::setAnnualInterestRate(double newannualInterestRate)
{
	annualInterestRate = newannualInterestRate;
}

void Account::setID(int newID)
{
		id = newID;
}

unsigned int Account::getID() const
{
	return id;
}

double Account::getBalance() const
{
	return balance;
}

double Account::getAnnualInterestRate() const
{
	return annualInterestRate;
}

double Account::getMonthlyInterestRate() const // ???
{

	return (annualInterestRate / 12 ); 
}
double Account::getMonthlyInterest() const  
{
	// getMonthlyInterest() is to return monthly interest, not the interest rate. 
	// Monthly interest is balance * monthlyInterestRate.

	return balance*(annualInterestRate / 12); 
}

void Account::withdraw (double withdrawAmount) 
{
	// withdraw() should checkavailable balance beforewithdraw. 
	// Throw an Exception if there is no sufficient balance.

	if (balance >= withdrawAmount)
	{
		balance -= withdrawAmount;

	}
	else
		throw invalid_argument("No sufficient balance");
}

void Account::deposit(double depositAmount) 
{
	balance += depositAmount;

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 // AccountMain.cpp (where I need help)

#include <iostream>
#include <iomanip>
#include <stdexcept>
#include "Account.h"
using namespace std;

int main()
{
	Account accountArray; // instantiate OBJECT of class Account
	Account &accountArray_refer = accountArray; // reference to object accountArray
	Account *accountArrayPtr = &accountArray; // pointer to object accountArray

	Account accountArray[5]; // Create an array of 5 accountArray objects
	
	cout << "Print out account information using object names \n";
	cout << setw(5) << "Account ID " << setw(5) << "Annual Interest Rate" << setw(5) << "Monthly Interest $ " << setw(5) << "account Balance $ " << endl;
	cout << "====================================================================\n";

	cout << "Print out account information using reference of objects \n";
	cout << setw(5) << "Account ID " << setw (5) << "Account Balance $ " << endl;
	cout << "================================================\n";

	cout << "Print out account information using pointers of objects \n";
	cout << setw(5) << "Account ID " << setw(5) << "Account Balance $ " << endl;
	cout << "================================================\n";

	return 0;
}

Last edited on
Just saying, the first object you make,and it's reference and pointer should be names account, as it's not an array. For the info, you just do:
 
cout << account.getID() << " " << account./*whatever you need*/ << */etc*/ << "\n";


With a pointer, things are a tiny bit different. Instead of using the dot (.) to access functons, you use an arrow (->). Like this:
 
cout << accountPtr->getID() << "/*etc*/; 


Hope this helps
Topic archived. No new replies allowed.