Thread: Help with Array for objects

  1. #1
    curwa
    Guest

    Help with Array for objects

    Hello...im confused on how to go about creating an Array for objects in a class. This is what is specified to occur in the main()...

    element 0 - use a mutator to set values
    element 1 - prompt and obtain from keyboard and use mutator method to initialize the array
    element 2 - prompt and obtain from keyboard , define an object using a two argument constructor..passing the values entered from keyboard, assign object to element 3
    element 3 - use accessor and mutator methods to set one of the values to the value in element 0 and another value to set to element 2.

    I have the mutators, accessors, and constructors done..just don't know how to go and make this part work....
    Any explanation or an example would be great to understand what is needed for this. Thanks...

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I'm not exactly sure what you mean...maybe you could post some code...or maybe it's just me
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    curwa
    Guest

    some code....

    Here is the code I have now...
    Code:
    class CanadianMoney            //CanadianMoney class
    {
    private:
    	int dollars;
    	double cents;
    public:
    	CanadianMoney() : dollars(0), cents(0)
    	{        }      //constructor (no args)  ..good
    
    	CanadianMoney (double c): dollars (static_cast<int> (c) ), cents (c - static_cast<int> (c) ) 
    	{        }       //constructor (one arg)
    
    	CanadianMoney(int d) : dollars(d), cents(0)
    	{        }       //constructor (one arg) ..good
    
    	CanadianMoney(int d, double c) : dollars(d), cents(c)
    	{       }        //constructor (two args) ... good
        
    	~CanadianMoney ()  //destructor
    	 {
           cout << "CanadianMoney object "<<dollars<<"."<<cents<< " deleted";
    		   cout << endl;
    	 }
    	void setDollars(int d);
    	void setCents(double c);
    	int getDollars();
    	double getCents();
    	string toString (int d, double c);
    };
    ///////////////////////////////////////////////////////////
    	void CanadianMoney :: setDollars (int d)	//mutator
    	{
    		dollars = d;
    	}
    	void CanadianMoney :: setCents (double c)		//mutator
    	{
    		cents = c;
    	}
    	int CanadianMoney :: getDollars ()			//accessor
    	{
    		return dollars;
    	}
    	double CanadianMoney :: getCents ()			//accessor
    	{
    		return cents;
    	}
    	string CanadianMoney :: toString (int d, double c)	//string method
    	{
    		char membuff[SIZE];		//buffer in memory
    		ostrstream omem(membuff, SIZE);		//create stream object
    
    		omem << "$" << d << "." << c << endl
    			 << ends;
    		cout << membuff;
    		return membuff;
    
    	}
    ///////////////////////////////////////////////////////////////
    int main ()
    {
    //1
    CanadianMoney money1(34.54);
    CanadianMoney money2(15);
    CanadianMoney money3(20,.61);
    
    money1.getDollars();
    money1.getCents();
    
    money2.getDollars();
    money2.getCents();
    
    money3.getDollars();
    money3.getCents();
    
    //2
    
    const int nmArray = 15;
    CanadianMoney moneyArray[nmArray];
    int n = 0;
    double amount;
    
    moneyArray[n].setCents(.0000);
    moneyArray[n].setDollars(0);		//element 0
    
    cout << "Enter dollar and cents amount: ";
    cin >> amount;
    moneyArray[n++].getDollars();
    moneyArray[n++].getCents();		//element 1
    
    //cout << "Enter dollar and cents amount: ";
    //cout <<endl;
    //moneyArray[1].getDollars();
    //moneyArray[1].getCents();		//element 2
    
    return 0;
    };
    maybe this will give you an idea of what i mean....Thanks

  4. #4
    curwa
    Guest
    Please....any help or examples would be great... i tried looking on the net for some help but couldn't find anything....

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    moneyArray[n].setDollars(0); //element

    cout << "Enter dollar and cents amount: ";
    cin >> amount;

    Your code looks okay until here. I haven't compiled it or anything, but looks acceptable. Let's start from here, anyway.

    What does n++ mean? It means that you use the original value of n to do whatever you want, then increment the value of n by 1 after you are done using the original value. Using your code:

    moneyArray[n++].getDollars();
    moneyArray[n++].getCents(); //element 1

    the first line will use the accessor getDollars() to return the dollar member for moneyArray[0] since n == 0 at this point. Then it increments n to 1. The value returned by the function is ignored so it's an illogical, but legal, call to the accessor function. After the first line is done n is 1 so the call to getCents() is now going to return the value of cents in moneyArray[1], which is undefined at this point then ignore the return value, again, and finally increment n to 2. I recommend you focus on using the accessor and mutator functions, and hardcode the index values, dropping n altogether, like this:

    moneyArray[0].setCents(.0000);


    Now let's take up the accessor function calls. Unless you use the return value somehow it doesn't make any sense to call them. Use them with cout to display the value in an instance of CanadianMoney like this:

    cout << money1.getDollars() << endl;
    cout << money1.getCents() << endl;
    cout << moneyArray[0].getDollars() << endl;

    or assign the return value to another variable like this:

    int x = money1.getDollars();

    but don't just call the accessor and ignore the return.

    Now let's turn to amount. It is a single variable. You can send it to setDollars() and setCents() both if you wish, but it would make more sense to get two user inputs, one to be used for dollar and one to be used for cents. Call them amount1 and amount2. Then send amount1 to setDollar() and amount2 to setCents().

    For element two you ask for input for dollar and cents and store them in two different variables, x and y would be fine again. Then you use x and y as arguments to a new instance of CanadianMoney--say money102 like this:

    CanadianMoney money102(x, y);

    instead of doing what you did for money3. Then you assign money102 to moneyArray[2].

    For element 3 you assign the values of element 0 to money1. You can either use the default assignment operator or do it explicitly by sending the return value of getDollars() to setDollars(), etc. Then ask for user input into x, and send x to money1.setDollars() or setCents(), whichever you prefer. Then assign money1 to element 3 of moneyArray.

  6. #6
    curwa
    Guest
    Would this be right??? Or is there any other mistakes like in element 2, 3 or to display from the string method?

    Code:
    //2
    
    const int nmArray = 15;
    CanadianMoney moneyArray[nmArray];
    int amount1, x;
    double amount2, y;
    
    moneyArray[0].setCents(.0000);
    moneyArray[0].setDollars(0);		//element 0
    
    cout << "Enter dollar amount: ";
    cin >> amount1;
    cout << "Enter cents amount: ";
    cin >> amount2;
    moneyArray[1].setDollars(amount1);
    moneyArray[1].setCents(amount2);		//element 1
    
    cout << "Enter dollar amount: ";
    cin >> x;
    cout << "Enter cents amount: ";
    cin >> y;
    CanadianMoney money102(x,.y);
    moneyArray[3].setDollars(x);
    moneyArray[3].setCents(y);		//element 2
    
    moneyArray[3].getDollars() + (moneyArray[0].getDollars() * 2);
    moneyArray[3].getCents() + (moneyArray[2].getDollars() * 5.5);  //element 3
    
    
    //3
    moneyArray[0].toString();
    moneyArray[1].toString();
    moneyArray[2].toString();
    moneyArray[3].toString();  //display properties of objects one at a time...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM