Thread: Help with Class Member Functions

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    22

    Help with Class Member Functions

    I am attempting to create a class and subsequent functions simulating a purse.

    Here are my instructions :
    -Declare Purse Class
    -Include 4 private data members
    +int pennies, int nickels, int dimes, & int quarters
    -Include 4 public functions
    +function insert(int p, int n, int d, int q) to initialize pennies, nickels, dimes, & quarters
    +function dollars() to return the dollar amount
    +function remove(int p, int n, int d, int q) to subtract pennies, nickels, dimes & quarters
    +function display() returns a new String with remaining pennies, nickels, dimes & quarters

    -Class should include a test driver main()
    +Should declare the Purse object p with (2, 3, 0, 1) and invokes display() method to print content of the purse


    Here is the code I have, but I am receiving arbitrary values when the program outputs values.

    Code:
    //Purse 
    
    #include <iostream>
    
    #include <iomanip>
    
    #include <string>
    
    using namespace std;  
    
    
    
    class Purse
    
    {          private:
    
                       int pennies;
    
                       int nickels;
    
                       int dimes;
    
                       int quarters;
    
                       
    
               public:
    
                      void insert( int, int, int, int );
    
                        
    
                      void remove( int, int, int, int );
    
                     
    
                      void dollars( int, int, int, int );
    
                      
    
                      void display();
    
    
    
    };
    
    
    
    
    
    
    
    
    
    //Purse::Purse(  
    
    //{
    
    //              insert( int p, n, d, q);
    
    //}
    
    
    
    void Purse::insert( int p, int n, int d, int q )
    
    {                      
    
                        pennies += p;
    
                        nickels += n;
    
                        dimes += d;
    
                        quarters += q; 
    
    }
    
    
    
    void Purse::remove( int p, int n, int d, int q )
    
    {                      
    
                        pennies -= p;
    
                        nickels -= n;
    
                        dimes -= d;
    
                        quarters -= q; 
    
    }
    
    
    
    //void Purse::dollars( int, int, int, int )
    
    //{                   float x = pennies + 5*nickels + 10*dimes + 25*quarters;
    
    //                    x =(float)x/100;
    
    //                    cout << "\nThe current amount inside of the purse in dollars, is : $" << x << " Dollars.\n"; 
    
    //}
    
    
    
    void Purse::display()
    
    { 
    
         cout << "\nCurrent count of purse: " << pennies << " Pennies, " << nickels << " Nickels, " << dimes << " Dimes, and " << quarters << " Quarters.\n" << endl;
    
    }
    
    
    
    
    
    
    
    
    
    int main()
    
    {
    
         Purse objectp;    //Declare object
    
         objectp.insert( 0, 0, 0, 0);
    
         
    
         //objectp.insert( 2, 3, 0, 1 ); 
    
         
    
         objectp.display();
    
         
    
         
    
         return 0;
    
    }
    I have tried messing with the syntax with a few of the functions with no luck. Even inputting all 0's into the insert function returns me with very high values.

    What can I do to fix this? Thanks in advance.
    Last edited by JJohnson; 05-05-2011 at 01:26 PM. Reason: Code didn't compile

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    What code are you actually using? (That code doesn't compile.)

    Alternatively, if that is your best effort so far, what errors are you getting?

    Soma

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Random thoughts:
    fflush(stdin) is undefined and you don't need it anyway. Get rid of it.
    Have a read: SourceForge.net: Do not remove parameter names - cpwiki
    Purse.pennies is illegal; Purse is not an instance of your class.
    mickels, dimes are undefined. How can you assign to something that doesn't exist?
    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.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    22
    Thank you for the fast responses. I'm sorry I was trying to 0 out the values because I was getting such weird output, I understand it is illegal. I will edit my first post rather than reposting the whole code.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Adding zero to an undefined value results in another undefined value.

    Set those variables to a defined value. (Use the constructor.)

    Soma

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    22
    Are you saying define int p,n,d,q in the public functions? I'm a little confused....

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, Soma means is that you should initialize your variables to 0 in the constructor.
    Be sure to read appropriate posted links also and fix said problems.
    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.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    19
    you can write like this :
    Code:
    //Purse 
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;  
    
    class Purse
    {          
    private:
    	int pennies;
    	int nickels;
    	int dimes;
    	int quarters;
    public:
    	Purse();
    	Purse(int,int,int,int);
    	void insert( int, int, int, int );
    	void remove( int, int, int, int );
    	void dollars( int, int, int, int );
    	void display();
    };
    Purse::Purse():pennies(),nickels(),dimes(),quarters(){};
    Purse::Purse(int p,int n,int d,int q):pennies(p),nickels(n),dimes(d),quarters(q){};
    void Purse::insert( int p, int n, int d, int q )
    {                      
    	pennies += p;
    	nickels += n;
    	dimes += d;
    	quarters += q; 
    }
    
    void Purse::remove( int p, int n, int d, int q )
    {                      
    	pennies -= p;
    	nickels -= n;
    	dimes -= d;
    	quarters -= q; 
    }
    
    void Purse::display()
    { 
    	cout << "\nCurrent count of purse: " << pennies << " Pennies, " << nickels << " Nickels, " << dimes << " Dimes, and " << quarters << " Quarters.\n" << endl;
    }
    
    int main()
    
    {
    	Purse objectp;    //Declare object
    	objectp.insert( 0, 0, 0, 0);
    	objectp.display();
    }

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    boyhailong: You would also benefit from reading SourceForge.net: Do not remove parameter names - cpwiki
    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. class member functions.
    By darren78 in forum C++ Programming
    Replies: 2
    Last Post: 07-14-2010, 10:07 AM
  2. Some help with class member functions and pointers
    By k_rock923 in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2005, 12:28 AM
  3. Class member variables only readable by member functions?
    By _Elixia_ in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2003, 03:52 PM
  4. DLL class member functions
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 06:59 AM
  5. member functions having the class's name
    By Linette in forum C++ Programming
    Replies: 8
    Last Post: 01-30-2002, 11:04 AM