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



LinkBack URL
About LinkBacks


