Thread: Operator Overloading of array output

  1. #1
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    Unhappy Operator Overloading of array output

    Hi all...
    I have a question regarding overloading of the << operator.
    I am using it as a friend function of a class in order to print out
    the private data of each object of the class when created.
    It works beautifully when i create a single object, however...
    if I attempt to create an array of objects of the class a cout <<
    statement of each object simply gives me the output of the memory address.
    I can't seem to find any examples of this behavior, in addition
    I've dynamically allocated my array, don't know if this has cause even more problems.
    The class and member function (including the firiend to overload is below) I also want to overload the >> operator to load data into the class buuut...am alot farther away from that thought process.
    Thanks so much!
    M
    Code:
    #include <iomanip>
    using std::setw;
    using std::left;
    using std::right;
    #include <fstream>
    #include <string>
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    using std::ostream;
    using std::istream;
    
    
    
    
    class Seat
    {
    public:
    	Seat();
    	~Seat();
    void clear();
    		void print() const;
    
    		friend ostream & operator << (ostream & output, const Seat &seat_o );
    		//friend istream & operator << (istream & input,  Seat &seat_o );
    
    		//set
    		void assign(char *first, char *last);
    		
    		void setSeatNum(int sn){iSeatNum = sn;};
    		
    		
    		
    		//get
    		bool getbassigned()const{return bassigned;};
    		
    		int getSeatNum()const{return iSeatNum;};
    		
    		void getFirstname(char*First)const{strcpy(First,FirstName);};
    		
    		void getLastname(char*Last) const{strcpy(Last,LastName);}
    		
    	
    private:
    	int iSeatNum;
    	bool bassigned;
    	char FirstName[20];
    	char LastName[20];
    };
    
    
    //Member Function Definitions
    //constructor:server function
    Seat::Seat()
    	{
    	iSeatNum = 1;
    	strcpy(LastName,"");
    	strcpy(FirstName,"");
    	bassigned = false;
    	}//Fx
    
    //destructor:server function
    Seat::~Seat()
    	{
    	
    	}//Fx
    
    
    //assign(set):server function
    void Seat::assign(char *Fname, char *Lname)
    //in: *Fname, *Lname; out:LastName, Firstname, bassigned;
    //out assigned seat w/ names & bool type to true
    	{
    	strcpy(LastName, Lname);
    	strcpy(FirstName, Fname);
    	bassigned = true;
    	}//Fx
    
    //clear:server function
    void Seat::clear()
    //in: none; out:none
    	{
        strcpy(LastName,"");
        strcpy(FirstName,"");
        bassigned = false;
    	}//Fx
    
    //print:server function
    void Seat::print() const
    //in: none; out:none
    	{
    	if (bassigned == true)
    		{	
    		cout << left  << setw(12) << FirstName 
    			<< left << setw(12) << LastName;
    		cout << right << setw(10) << iSeatNum << "\n";
    		}//if
    	
    	}//Fx
    
    //Friend Function definitions
    ostream &operator<<( ostream & output,  const Seat &seat_o )
    	{
    				output << "Overloaded << Operator :" << endl;
    				output << left << setw(12)<< "FirstName :"<< seat_o.FirstName<<endl;
    			    output << left << setw(12)<< "Lastname :" << seat_o.LastName << endl;
    				output << right << setw(10)<< "Seat Number :" << seat_o.iSeatNum << "\n";
    
    				return output;
    
    	}
    
    istream & operator << (istream & input,  Seat &seat_o ) // VERY confused as to loading here
    	{
    	
    //	int i;
    
    	//	seat_o.setSeatNum(i+1);
    		
    
    			//	return input;
    
    	}
    
    
    
    //NON member, NON friend functions
    void initSeatNum(Seat *seats[], int num);
    
    
    int main()
    
    
    {
    const MAX_ITEMS = 12;
    
    Seat aseat;
    
    
    	
    	Seat *bseat[MAX_ITEMS];
    	for (int i = 0; i < MAX_ITEMS; i++)
    		{
    		bseat[i] = new Seat;
    		cout << bseat[i] << endl;//this only prints out memory addresses for each object!
    		}
    
    initSeatNum(bseat, MAX_ITEMS);
    
    
    cout << aseat; // this works fine
    
    
    
    
    
    
    
    
    	return 0;
    }
    
    //Initialize:client function
    void initSeatNum(Seat *seats[], int Mnum )
    //in: seats, Mnum; out:none
    	{
    	for (int i = 0; i < Mnum; i++)
    		{
    		seats[i]->setSeatNum(i+1);     
    		}//for
    	}//Fx

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    try, i'm not sure, but i think this is right:
    Code:
    cout << *bseat[i] << endl;
    also, you're istream should be:
    Code:
    istream & operator >> (istream & input,  Seat &seat_o ) //not << operator

  3. #3
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    solution

    actually i was using a reference parameter and calling a pointer once i changed that..things were better the << operator instead of >> was an accidental typo - i hadn't attempted that function yet
    thanks alpha

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output an array in a textbox
    By Diablo02 in forum C# Programming
    Replies: 5
    Last Post: 10-18-2007, 03:56 AM
  2. Array output strange
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2006, 06:58 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM