Thread: Array inside of a class???

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Array inside of a class???

    Im working on some home work and have been beating my head against the wall for 3 days trying to figure this out here is the code ill explain what im trying to do under the code
    Code:
    // Bankapp chapter 7 case 2.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    // Declaration Section
    
    class Bank
    {
    private:
    	int accountNum;
    	int accountTerm;
    	double accountBal;
    	string output;
    	const static int ANNUAL_RATE;
    public:
    	void displayAccountNum();
    	void setAccountData(int, int, double);
    	void computeInterest();
    	int getAccountNum();
    	double getAccountBal();
    	int getAccountTerm();
    };
    const int Bank::ANNUAL_RATE = 3;
    
    
    
    void Bank::setAccountData(int num, int term, double bal)
    {
    	accountNum = num;
    	accountTerm = term;
    	accountBal = bal;
    	
    }
    void Bank::displayAccountNum()
    {
    	
    	cout << accountNum << " Account has $" 
    		<< accountBal 
    		<< " and will gain interest for " 
    		<< accountTerm << "yrs" << endl;
    }
    int main()
    {
    	Bank aBank;
    	int accountNum = 0;
    	int accountTerm = 0;
    	//double accountBal = 0;
    	const int TIMES = 10;
    	double accountBal = 0.0;
    	for (int x=0; x < TIMES; ++x)
    	{
    		cout << "Enter " << "#" << x+1 << " account term from 1 to 10 years" << endl;							// Account terms prompt
    		cin >> accountTerm;
    		while (accountTerm < 1 || accountTerm > 10)
    		{
    			cout << "Error! Please try again" << endl;
    			cout << "Enter " << "#" << x+1 << " first account term from 1 to 10 years" << endl;
    			cin >> accountTerm;
    		}
    
    		cout << "Enter " << "#" << x+1 << " Account number(Between 1000 and 9999(0000 to quit)) at a time: " << endl;				// Prompt for an account number
    		cin >> accountNum;
    		//aBank[count].setAccountData(accountNum);
    
    		while (accountNum < 1000 || accountNum > 9999)
    		{
    			cout << "Error! Please try again." << endl;
    			cout << "Enter " << "#" << x+1 << " Account number(Between 1000 and 9999(0000 to quit)): " << endl;
    			cin >> accountNum;
    			
    		}
    		cout << "Enter " << "#" << x+1 << " Account Balance(Can not be less than 0 or more than 100000): " << endl;			// Prompt for an account balance
    		cin >> accountBal;
    
    		while (accountBal <= 0 || accountBal > 100000)
    		{
    			cout << "Error! Please try again." << endl;
    			cout << "Enter " << "#" << x+1 << " first Account Balance(Can not be less than 0 or more than 100000): " << endl;
    			cin >> accountBal;
    		}
    		aBank.setAccountData(accountNum, accountTerm, accountBal);
    		aBank.displayAccountNum();
    	}
    	system("pause");
    	return 0;
    }


    What the book wants is a class just like i have but i need to to store the data in an array to store 10 accounts with there balances and the term of the account... im sure im over thinking it and its probably no different then doing a normal array im just getting to mad any help would be greatly appreciated
    Last edited by adamk8875; 04-04-2011 at 10:28 PM.

  2. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    I'm having trouble understanding what you're asking. However, it sounds like you need an array of accounts, which would mean you would need an account class:

    Code:
    class Account
    {
        ...
    }
    And then put an array of Accounts inside the bank:

    Code:
    class Bank
    {
    private:
        Account accounts[NUM_ACCOUNTS];
        ...
    }
    Then again, you should really use a vector, not an array (though either method can be made to work):

    Code:
    class Bank
    {
    private:
        std::vector<Account> accounts;
        ...
    }

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    a vector... is that like ussing an array? my book hasnt gone over vectors and i would hate to ask someone to demo them so ill just try and stick with arrays i think....... and i think i understand what your saying so ill try and get it working

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A vector is a dynamic array; that is, an array that can expand as you push more contents into it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with loading array of class objects
    By McElmurry in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2010, 03:34 AM
  2. Whats the best way to control a global class?
    By parad0x13 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2009, 05:17 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM