set the data members for Stack.h

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Barbara Baby
    New Member
    • Oct 2011
    • 21

    set the data members for Stack.h

    Here is my code for the Stack.h:

    #ifndef STACK_H
    #define STACK_H

    #include <iostream>
    #include <iomanip>

    using namespace std;

    class Stack
    {
    //friend function

    friend ostream& operator<< (ostream& leftOp, const Stack& rightOp)

    private:
    //data members

    int *arraystack;
    int arraysize;
    int artop;
    public:
    //methods prototype
    Stack(int size);
    Stack(const char* s);
    ~Stack();
    Stack& operator = (const Stack& rightOp) const;
    void clean();
    int size();
    bool empty();
    int top();
    void push(int);
    void pop();
    };
    #endif


    Just want to know if my data members are ok. Here is the instruction for data members:

    Data members
    This class contains a dynamically allocated array of integer data values (the stack array). Because the array is allocated dynamically, an integer value is also maintained inside the class to determine the maximum number of elements that may be stored in the array (the stack capacity).
    The class also requires two other integer data members: the number of data items currently stored in the stack (the stack size), and the subscript of the top item in the stack (the stack top subscript). Don't give these data members the same names as methods of the class.

    Thank you!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Your friend function is private and it should probably bt public.

    You have no copy constructor. Generally, the ABC rule is that if you write a constructor or assignment operator or destructor then you have to write all three. Copy constructors override the compiler's member-by-member copy. If you intend no copies then your copy constructor prototype should be private.

    What is clean() for? Probably you can just call the destructor. A destructor does not destroy your object. It's just a function that the compiler can call so you can clean up before the compiler destroys the object. But it's the compiler that kills the object not the destructor call.

    Comment

    • Barbara Baby
      New Member
      • Oct 2011
      • 21

      #3
      is the Stack(const char* s); not a copy constructor?
      I am sorry, it should not be clean but clear.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Acopy constructor initializes one Stack object using another Stack opject as input. Like tis:

        Code:
        Stack::Stack(const Stack& s)
        {
        etc...
        You need a Stack& argument to avoid the need to make a copy of a Stack object in oreder to make the constructor call. Which would call he cpy cnstructor which would require another call to make the copy for the secnd call. And so on into the night...

        Comment

        • Barbara Baby
          New Member
          • Oct 2011
          • 21

          #5
          Here is my copy constructor:

          Stack::Stack(co nst Stack& s)

          Comment

          • Barbara Baby
            New Member
            • Oct 2011
            • 21

            #6
            Can you please check if the data members are as asked by the instructions given?

            Comment

            • Barbara Baby
              New Member
              • Oct 2011
              • 21

              #7
              The revised code is like this:
              Can you please chech if the data members are correct( in line with instructions given above)

              Thank you very much!


              #ifndef STACK_H
              #define STACK_H

              #include <iostream>
              #include <cstring>
              #include <cctype>
              #include <cstdlib>

              using namespace std;

              class Stack
              {
              //friend function

              friend ostream& operator<< (ostream& leftOp, const Stack& rightOp);

              private:
              //data members

              int *arraystack;
              int arraysize;
              int artop;
              public:
              //methods prototype
              Stack();
              ~Stack();
              Stack(const Stack& s);
              Stack& operator = (const Stack& rightOp) const;
              void clear();
              int size();
              bool empty();
              int top();
              void push(int);
              void pop();
              };
              #endif

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                I still think your inserter (operator<<) needs to be public.

                Comment

                • Barbara Baby
                  New Member
                  • Oct 2011
                  • 21

                  #9
                  Isn't the friend function outside of the private and public access? Since it cannot be a method prototype. Please advise!
                  I made changes also to operator= and the empty() method:

                  Stack& operator= (const Stack& rightOp)
                  bool empty() const;

                  Please advise!

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Oops. Yes you are correct.

                    Comment

                    • Barbara Baby
                      New Member
                      • Oct 2011
                      • 21

                      #11
                      Thank you very much. Are my private data members ok? I mean do they follow the instruction given?

                      Thank you

                      Comment

                      Working...