Thread: Question about cout an stack object?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    77

    Wink Question about cout an stack object?

    I created an object called MyRowstack form the class Rowstack
    Code:
    Rowstack MyRowstack;  // Instantiate a Rowstack object;
    and then I push some cards inside MyRowstack
    Code:
    	for(int iCtr = 0; iCtr < 6; iCtr++)
    	{
    		MyRowstack.pushFaceDownCard(MyDeck[iCtr]);
    	}
    If I want to cout the cards inside the object which I pushed in, what should I code?

    I tried to code it like that:
    Code:
            for(iCtr = 0; iCtr < MyRowstack.size(); iCtr++)
    {
            cout << MyRowstack << endl;
    }
    I get this error code
    Compiling...
    rowstackTest.cpp
    C:\Lab8\rowstackTest.cpp(20) : error C2039: 'size' : is not a member of 'Rowstack'
    c:\lab8\rowstack.h(9) : see declaration of 'Rowstack'
    Does anyone know what it is?
    I think Stack supports object.size() operator.
    Thanks.

    I have some other questions posted in other topics, If it breaks the rules, pls remind me and sorry about that.
    Last edited by joenching; 05-08-2005 at 06:14 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    C:\Lab8\rowstackTest.cpp(20) : error C2039: 'size' : is not a member of 'Rowstack'
    I think Stack supports object.size() operator.
    Yes, it does but...
    I created an object called MyRowstack form the class Rowstack
    so MyRowstack is not a stack object. If MyRowstack was a stack object, it would have been declared like this:

    stack MyRowstack;
    Last edited by 7stud; 05-08-2005 at 06:28 PM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    So, according the rules, so the object is not a stack object and it doesn't .empty() operator?

    And If I want to cout the elements inside this object, what should I do?

    Thanks for advance.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    What is your understanding of how a class you create works?
    Last edited by 7stud; 05-08-2005 at 07:24 PM.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    I don't really understand what you said.
    For me, I realize if i create an object and insert elements inside the obects, then i can use a for loop to show each elements.
    but the problem is i don't know what I should do to show that...

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I don't really understand what you said.
    Well, for instance suppose you have a class defined like this:
    Code:
    class Applestack
    {
    private:
    
    	int size;
    	string color;
    
    public:
    	
    	Applestack(int s, string str)
    	{
    		size = s;
    		color = str;
    	}
    
    	Applestack()
    	{
    		size = 0;
    		color = "";
    	}
    
    	void display()
    	{
    		cout<<size<<endl;
    		cout<<color<<endl;
    	}
    };
    1) What are the two ways you can create objects of that class?
    2) Besides the constructors, what methods does the class contain?
    3) How would you call a method of the Applestack class using an Applestack object?
    4) Suppose you have another class called DogKennel that is defined like this:
    Code:
    class DogKennel
    {
    private:
    	double capacity;
    
    public:
    	DogKennel(double c)
    	{
    		capacity = c;
    	}
    
    	void show()
    	{
    		cout<<capacity<<endl;
    	}
    };
    Could you call the display() function in the Applestack class using a DogKennel object? Why or why not?


    5) Does the Applestack class have an empty() method? Why or why not?
    6) Can you find out the capacity of a DogKennel object by calling a size() method? Why or why not?
    Last edited by 7stud; 05-08-2005 at 07:55 PM.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    1) I think I can create the objects from either the default constructor - apple and the one I can type the parameter - Applestack(int s, string str).

    2) the methods - void display()

    3) applestack.display();

    4) For Q4, I am not sure, but I think i can call it since the display() method is coded inside the public, not inside the private.

    5 and 6) No, Since both of them are not using STL, so those method are not supported.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    Good. You got them all right except 4).

    I can see in your programs why you got 4) wrong. You don't really have a good understanding yet of what the methods of a class are. When you define a class, it allows you to create objects of that class. The methods of the class describe what the objects of the class can do. If you define a method that allows your objects to display() their private members, then that is one thing your objects will be able to do. If you define a method that allows your objects to set the value of one of their private members, then that is another thing your objects will be able to do. It's up to you to define all the things you want your objects to be able to do, and the methods you create allow the objects to do those things.

    However, the methods of a class can only be called by objects of that class. In fact, the methods will usually only make sense with respect to objects of that class. For instance, the display() function in the Applestack class displays the calling object's member variables named 'size' and 'color'. Therefore, how would you call display() with a DogKennel object? A DogKennel object does not have a 'size' or a 'color' member variable, so the display() function has no meaning with respect to a DogKennel object.

    You need to understand that methods are called by objects and the methods often do something to the objects that call them, e.g. the methods set the value of the object's member variable or display the value of the object's member variable. Because methods often change the object that calls them or operate on the object's member variables in some way, it is often said: "a method is called on an object". That terminology makes it explicit how a method works: it operates 'on' the object that calls it. Therefore, you can't call methods of other classes on an object: you can only call methods on an object that are defined in the object's class.

    If you need to know what methods you can call on an object, you go to that object's class defintion and look down the list. If the method isn't there, then you can't call the method on that object.
    Last edited by 7stud; 05-08-2005 at 11:41 PM.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Thanks so much for it.
    It makes my mind better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. Need Help with Stack
    By trongsi in forum C++ Programming
    Replies: 9
    Last Post: 05-23-2006, 04:14 PM
  3. Question..Help..Real Number Object ?
    By Meth in forum C++ Programming
    Replies: 5
    Last Post: 10-01-2005, 10:11 PM
  4. cout question
    By The Gweech in forum C++ Programming
    Replies: 4
    Last Post: 07-09-2002, 04:11 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM