private member wont add.

This is a discussion on private member wont add. within the C++ Programming forums, part of the General Programming Boards category; Hi all. I'm perplexed. I have an array of pointers to Account objects. In the Account object, there's a private ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    163

    private member wont add.

    Hi all. I'm perplexed. I have an array of pointers to Account objects. In the Account object, there's a private data member- float money. I'm trying to add to money, but for some reason it wont keep its value outside of the add_money() function.

    basic mainline:
    Code:
    #include "ActList.h"
    
    int main()
    {
    	ActList newList;
    	newList.add_account(0);
    	newList.get_account(0).add_money();
    	newList.display_account(0);
    	return(0);
    }
    ActList:
    Code:
    //ActList.h
    
    #ifndef     actlist_h
    #define     actlist_h
    
    #include "Account.h"
    
    const int MAX_NUM_ACTS = 20;
    class ActList
    {
        public:
            ActList();
    
            void add_account(int);
            void remove_account(int);
            void display_account(int);
    		int find_account(std::string);
    		Account get_account(int num){return(*actAry[num]);};
    
        private:
            int numActs;
            Account *actAry[MAX_NUM_ACTS];
    
    
    };
    
    #endif
    
    //ActList.cpp
    
    #include "ActList.h"
    
    ActList::ActList()
    {
        numActs = 0;
    };
    
    
    void ActList::add_account(int num)
    {
    	Account * ptrAcct;
    	if(num == 1)
    	{
    		ptrAcct = new Savings;
    	}
    	else
    	{
    		ptrAcct = new Account;
    	}
    
        actAry[numActs] = ptrAcct;
        numActs++;
        return;
    };
    
    
    int ActList::find_account(std::string nameToFind)
    {
    	for(int i = 0; i < numActs; i++)
    	{
    		if(nameToFind == actAry[i]->get_name())
    		{
    			return(i);
    		}
    	}
    	return(-1);
    };
    
    void ActList::display_account(int num)
    {
    	std::cout << "Account name: " << actAry[num]->get_name() << std::endl;
    	std::cout << "Account Balance: " << actAry[num]->get_money() << std::endl;
    
    
    	return;
    };
    Account class:
    Code:
    #ifndef     account_h
    #define     account_h
    
    #include <string>
    #include <iostream>
    
    class Account
    {
        public:
            Account();
            void add_money();
            void remove_money();
            float get_money(){return(money);};
            std::string get_name(){return(name);};
            void set_name();
    		void display();
    
    		virtual float calc_interest(){return(0);}
    		virtual void add_interest(){}
    		virtual void set_intrate(){}
    
        protected:
            float money;
            std::string name;
    
    };
    
    #endif
    
    //Account.cpp
    
    #include "Account.h"
    
    Account::Account()
    {
        money = 0;
        set_name();
    };
    
    void Account::add_money()
    {
        float numToAdd;
        std::cout << money <<"\n";
        do
        {
            std::cout << "How much would you like to add? ";
            std::cin >> numToAdd;
        }while(numToAdd < 0);
        money += numToAdd;
        //it's correct here!
        std::cout << money << "\n";
        return;
    };
    
    void Account::remove_money()
    {
        int numToRemove;
        do
        {
            std::cout << "How much would you like to remove? ";
            std::cin >> numToRemove;
        }while(numToRemove < 0 || numToRemove > money);
        money -= numToRemove;
    };
    
    void Account::set_name()
    {
        std::cout << "What would you like to name this account? ";
        std::cin >> name;
    };
    
    void Account::display()
    {
    	std::cout << "Account name: " << name << std::endl;
    	std::cout << "Account balance: $" << money << std::endl;
    };
    I'm baffled as to why it wont add to money outside of that function. Does anybody have any idea of what's going on here? It's probably gonna be something simple and I'll feel embarassed, but I've been working on this one problem for like 2 hours now.
    Last edited by System_159; 12-12-2006 at 04:18 PM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The return type of get_account is Account. By returning an account, you're creating a copy of it, and add_money is then called with the copy, leaving the original unchanged.
    Change the return type to Account&, i.e. a reference to an account.

    To make the compiler notify you about this problem, you should make Account non-copyable. There are various ways to do this. The one with the least dependencies is to declare the copy constructor and the copy assignment operator in the class's private area and never implement it. A more readable way would be to derive the class from boost::noncopyable, but that requires you to install the Boost libraries, which you may not be ready to do yet or which your course might simply not allow.

    Code:
    class Account
    {
    private:
      // Prevent copying:
      Account(const Account &); // Copy constructor
      Account &operator =(const Account &); // Copy assignment operator
    public:
      // ...
    };
    This way, the compiler will complain about the copy constructor or the assignment operator being inaccessible if you accidentally try to create a copy again.

    By the way, the methods that take user input should handle non-numeric values. Currently they only handle numeric values that are out of range, but give them something non-numeric, and your program's behaviour becomes undefined. (Two likely outcomes on x86: either the program goes into an infinite loop or it adds an absurd amount of money to the account.)
    Last edited by CornedBee; 12-12-2006 at 04:31 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    brilliant! I'm noticing a patern of the only problems I'm having are with the & symbol, guess I need to study up on references and what not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing a private array member
    By csonx_p in forum C++ Programming
    Replies: 13
    Last Post: 09-18-2008, 11:44 AM
  2. Setting And Accessing Private Member Variables
    By bumfluff in forum C++ Programming
    Replies: 4
    Last Post: 04-12-2006, 07:00 PM
  3. can not add member error
    By WaterNut in forum Windows Programming
    Replies: 4
    Last Post: 12-02-2004, 01:18 PM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 12:14 PM
  5. private data member with public set member function
    By Mario in forum C++ Programming
    Replies: 2
    Last Post: 05-28-2002, 11:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21