Overloading Operator + Array

This is a discussion on Overloading Operator + Array within the C++ Programming forums, part of the General Programming Boards category; I'm suppost to display my entries by overloading the "<<" operator using friend functions. I can currently do: Code: Student ...

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    Overloading Operator + Array

    I'm suppost to display my entries by overloading the "<<" operator using friend functions. I can currently do:
    Code:
    Student Stud1("Williams","Jon","22222",4,5);
    cout<<Stud1;
    and everything works. However, all my entries are stored in an array(actually two but i will only refer to one since they are basically the same).
    So i was outputting the entries like:
    Code:
    cout<<endl<<Array1[A_Pos2].getFirst_Name();
    cout<<endl<<Array1[A_Pos2].getLast_Name();
    ...
    however, i am suppost to make use of the overloaded "<<" operator to output the entries, and I am not sure how to do this.
    These are my overloaded friend functions:
    Code:
    header file:
    
    friend ostream &operator<< (ostream &, Student &);
    
    function file:
    
    ostream &operator<<(ostream &output, Student &person)
    {
    	
    	output<<"First Name: "<<person.getFirst_Name()<<endl;
    	output<<"Last Name: "<<person.getLast_Name()<<endl;
    	output<<"Phone Number: "<<person.getPhone_Number()<<endl;
    	output<<"GPA: "<<person.GPA<<endl;
    	output<<"Hours: "<<person.Hours<<endl;
    
    	return output;
    }
    I'm not sure how to output the entries in the array using the overloaded operator. I'm thinking it would be something like:
    Code:
    cout<<Array1[A_Pos2];
    I'm not sure what to do.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    havent used C++ in a long time so not familiar with everything right now.. also, dont have access to a compiler so cant test any ideas i have.

    what i would try is to have a class variable (ie: int size) that keeps track of how large your Student[] is.
    in your overloaded << method change the second argument so that it accepts an array of Student(s) not just one student. have a for loop looping over the size of the array, and have the 'output...' statements you have here inside it.
    Code:
    ostream &operator<<(ostream &output, Student* &people) // would this work? sorry dont remember syntax!
    {
            for (int i = 0; i < size; i ++)
            {
                   output<<"First Name: "<<people[i].getFirst_Name()<<endl;
                   output<<"Last Name: "<<people[i].getLast_Name()<<endl;
                   output<<"Phone Number: "<<people[i].getPhone_Number()<<endl;
                   output<<"GPA: "<<people[i].GPA<<endl;
                   output<<"Hours: "<<people[i].Hours<<endl << endl;
             }
    
            return output;
    }
    when you want to print it pass the entire array not just one element, cout << Array1;
    Last edited by nadroj; 04-02-2007 at 10:56 PM.

  3. #3
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Ahh, ok. It works now.
    Thank You

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    could you post exactly what you did?
    so i can see how far off i was

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The above doesn't seem very good. Why would a Student need to know how many other students there are. If you want to display data for a collection of students, using a single call to <<, put the array in a StudentDatabase and overload the operator for it.

    Without that, use a simple loop to output the students:
    Code:
    Student array[size];
    for (int i = 0; i < size; i++) {
        std::cout << array[i];
    }
    Another problem with having a Student object keep count of all other students is that it might be hard to make more than one array of them.
    Last edited by anon; 04-03-2007 at 01:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. overloading operator problems
    By almich in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2004, 05:10 PM
  4. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 05:06 PM
  5. overloading
    By theLukerBoy in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 08:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21