Operator overloading problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sooli
    New Member
    • Sep 2014
    • 49

    Operator overloading problem

    I need to create an overloaded cout that will print the contents of an array.

    so I can say output << a << endl;

    and it will print the contents of the object a... which happens to be an array.

    class info:
    Code:
    class List 
    {
    	public:
    
    		List();
    
    		bool empty(); //returns true of false if empty
    
    		void front(); //makes current position at beginning of list
    
    		void end(); //makes current position at the end of list
    
    		void prev(); //places current position at the previous element in the list
    
    		void next(); //places current position at the next element in the list
    
    		int getPos(); //returns current position or where you are in the list
    
    		int SetPos(int); //places current position in a certain position in the list
    
    		void insertBefore(int); //inserts a new element before the current position
    
    		void insertAfter(int); //inserts a new element after the current position
    
    		int getElement(); //returns the one element that current position is pointing to
    
    		int size(); //returns the size of the list(number of elements in the list)
    
    		void replace(int); //replace the current element with a new one
    
    		void erase(); //deletes the current element
    
    		void clear(); //makes the list an empty list
    		
    		void reverse(); //reverse elements in a list
    
    		void swap(List);  //swaps all the elements of one list with another list
    		
    		int getMax(); //return Max Size based on const CAPACITY
    
    		void display(); //displays contents of list
    
            private:
    
    	int current;
    	int length;
    	static const int CAPACITY = 20;
    	int arrayList[CAPACITY];
    	int arrayCopy[CAPACITY];
    
    };

    constructor:
    Code:
    List::List()
    		{
    			//constructor - initialize array's at position 1
    			int maxLen=CAPACITY;
    			
    			length=0;
    			current=0;
    			arrayList[length];
    			arrayCopy[length];
    			//clean up garbage...
    			for(int i=0;i<CAPACITY;i++)
    				{
    					arrayList[i]=0;
    					arrayCopy[i]=0;
    				}
    		}
    I have one overloaded method... which returns the size of the array

    Code:
    ostream& operator <<(ostream& output,  List& out)
    	{
    		 
    		output << out.size()<< endl;
    		
    		return output;
    	}
    I understand this code, I am simply calling the size method from the program, but i don't know how to pass in the array so that i can print it line by line... simple syntax i am sure... but the whole thing is baffling me... I need to be able to call this on any variation of the class, so it cannot be specific to any one array.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Isn't the array:

    Code:
    int arrayList[CAPACITY];
    ?

    That is, the array member of your List class?

    So the overload you have:

    Code:
    ostream& operator <<(ostream& output,  List& out)
         {
            output << out.size()<< endl;
     
             return output;
         }
    already has the array because it's inside the List object named out. However, this function is not a member function of List so you have no access to the array. What you want to so is:

    Code:
    ostream& operator <<(ostream& output,  List& out)
         {
           for (int x =0; x< out.size(); ++x)
           {
            output << out[x];
           }       
     
             return output;
         }
    Unfortunately, out[x] won't compile until you overload the index operator in List:

    Code:
    int List::operator[](int a)
    {
        return arrayList[a];
    }
    Keep me posted on how you are coming along.

    Comment

    • sooli
      New Member
      • Sep 2014
      • 49

      #3
      well, you gave me an idea... and it seems to be working - instead of overloading the list operator, I used my existing methods to produce the data I wanted by creating a loop:

      [code]

      ostream& operator <<(ostream& output, List& out)
      {

      output << out.size()<< endl;
      out.front(); //sets position to the front of array
      for (int i=0; i<out.size(); i++) //increment index i thereby stepping thru array
      {
      output << out.getElement( ) << " " ; //print out value of position i
      out.next(); //advances current position by one in array

      }
      return output;
      }

      Thanks for jogging the brain!

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Yep. Looks like a solution.

        In fact, by not modifying the List class you have increased encapsulation since there is now less code dependent upon the list class implementation. On a real job, this will get you noticed.

        Comment

        • sooli
          New Member
          • Sep 2014
          • 49

          #5
          @ Weaknessforcats : Thanks for the encouragement!

          Comment

          Working...