How to create these two functions with their respective variables?

Pages: 12
Hey guys! How can I create a Dynamic Array of Account objects with different field values? Dynamic array is when the code use delete and new?

And withdraw and deposit functions with their respectie balances?

If anyone help me with these, it would be very appreciated.

Thanx!

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
#include <iostream>
using namespace std;

class Account
{
private:
	int id;
	double balance;
	double annualInterestRate;

public:
	Account(int idc = 0, double balancec = 0.0, double AIR = 0.0)
	{
		id = idc;
		balance = balancec;
		annualInterestRate = AIR;
	}

	int getID()
	{
		return id;
	}

	double getBalance()
	{
		return balance;
	}

	double getAnnualInterestRate()
	{
		return annualInterestRate;
	}

	double getAIR()
	{
		return balance * 0.045;
	}
};

int main()
{
	Account a1(1234, 200, 0.045);
	cout << "ID: " << a1.getID() << endl;
	cout << "Balance: " << a1.getBalance() << endl;
	cout << "IRS: " << a1.getAIR() << endl;

	Account a2(4321, 400, 0.045);
	cout << "ID: " << a2.getID() << endl;
	cout << "Balance: " << a2.getBalance() << endl;
	cout << "IRS: " << a2.getAIR() << endl;

	return 0;
}
Last edited on
Hi,
> How can I create a Dynamic Array of Account objects with different field values?
What is the assignment question? Have you learned array yet?
I am studiyn this by myself! A friend gave me their class topics. I know arrays, but I wanna know how to create a Dynamic Array. I know that a dynamic array is something that used: new whatever and delete whatever...
Dynamic array is when the code use delete and new?
The was in the bad old days. If you don't have an assignment that forces you to use pointer and dynamic memory it's better to use vectors - it will save you much headache.
http://www.cplusplus.com/reference/vector/vector/

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <vector>
int main()
{
   Account a1(1234, 200, 0.045);
   Account a2(4321, 400, 0.045);

   std::vector<Account> bank;
   bank.push_back(a1);
   bank.push_back(a2);
   // and so on
   
   return 0;
}
And with pointers and dynamic memory ? How it could be?
So you really want to learn raw dynamic array?

I can give you some info if you insist.
Yeah! I want learn it.
To create a dynamic array :
<pointer variable> = new <variable type> [array_size];

For example :
Account *accArray = new Account[100]; // an array is created - ready to use :)


And finally, free the array when not needed :
delete [] accArray;

More info :
http://www.learncpp.com/cpp-tutorial/6-9a-dynamically-allocating-arrays/
Last edited on
So please clarify your own homework (challenge) and show us what you intend to do with dynamic array. We will of course assist you :)
My challenge:

Steps A:
1. An int data field id for the account. DONE!
2. A double data field named balance for the account. DONE!
3. A double data field named annualInterestRate that stores the current interest rate. I think this one wasn't done.

4. A no-arg constructor that creates the default account. I think this one is done.
5. The accessor and mutator functions for id, balance and annualInterestRate. This one should be better or is ok?

6. A function named getMonthlyInterestRate() that returns the monthly interest rate. Need support with this one.
7. A function named withdraw that withdraw a specified amount from the account. Need support with this one too.
8. A function named deposit that desposits a specified amount to the account. Need support with this one.

Step B:
1.Test program that create a dynamic array of Account objects with different fields values.
Ok, show us your code you are working on.
Does this assignment contain output samples?
Output Samples:
1. Use withdraw function to withdraw $2,500.
2. Use deposit function to deposit $3,000.
3. Print the balance, and the monthly interest.
4. Print the list of Accounts with their id, balance.

Here is my code:
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
#include <iostream>
using namespace std;

class Account
{
private:
	int id;
	double balance;
	double annualInterestRate;

public:
	Account(int idc = 0, double balancec = 0.0, double AIR = 0.0)
	{
		id = idc;
		balance = balancec;
		annualInterestRate = AIR;
	}

	int getID()
	{
		return id;
	}

	double getBalance()
	{
		return balance;
	}

	double getAnnualInterestRate()
	{
		return annualInterestRate;
	}

	double getAIR()
	{
		return balance * 0.045;
	}
};

int main()
{
	Account a1(1234, 200, 0.045);
	cout << "ID: " << a1.getID() << endl;
	cout << "Balance: " << a1.getBalance() << endl;
	cout << "IRS: " << a1.getAIR() << endl;

	Account a2(4321, 400, 0.045);
	cout << "ID: " << a2.getID() << endl;
	cout << "Balance: " << a2.getBalance() << endl;
	cout << "IRS: " << a2.getAIR() << endl;

	return 0;
}


I am working function with static functions. I have some idea of Dynamic Array but I dont understand it too much.
> I am working on static functions
No need for static functions

> I have some idea of dynamic array but I don't understand it too much.
Please state your current problem.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	int number_of_accounts = 0;
        cin >> number_of_accounts;
        Account* acc_array = new Account[number_of_accounts];

       Account acc_array[0] = Account(1234, 200, 0.045);
       Account acc_array[1] = Account(4321, 400, 0.045);

	for( i = 0; i < number_of_accounts; i++)
       {
               cout << "ID: " << acc_array[i].getID() << endl;
	       cout << "Balance: " << acc_array[i].getBalance() << endl;
	       cout << "IRS: " << acc_array[i].getAIR() << endl;
       }

       delete [] acc_array;

	return 0;
}
Last edited on
> Output Samples :
> 1. Use withdraw function to withdraw $2,500.
> 2. Use deposit function to deposit $3,000.
> 3. Print the balance, and the monthly interest.
> 4. Print the list of Accounts with their id, balance

They are not output samples. They are questions of the assignment.
Okay I see it. But how can I manage for user input? I need set functions, like setBalance, setAIR, setID ?
closed account (48T7M4Gy)
.
Last edited on
closed account (48T7M4Gy)
.
Last edited on
Yes. It would be very helpful kemort!
closed account (48T7M4Gy)
.
Last edited on
Pages: 12