Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6241803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:50:03+00:00 2026-05-24T11:50:03+00:00

I need to define a read and a print function for a class that

  • 0

I need to define a read and a print function for a class that has an array of objects as a private variable. I have to read in objects from a text file and print them to the screen. To do this I need to overload the << and >> operators. I understand I need to use loops to read and print the information stored in the array but I’m not sure how to accomplish this. My lecturer has given us a skeleton code which is basically function prototypes and the main function which I need to stick to. I understand how this works with public structs as I have done this exact scenario using that but the private variables of class’ are tripping me up.

class EmployeeList {
public:
  //Constructors
  EmployeeList();
  EmployeeList(istream&);
  //Accessors
  bool isEmpty() const;
  bool isFull() const;
  int size() const; //Number of employees in list
  Employee item(int i) const; //i'th employee
  //Mutators
  void setItem(int i,const Employee& e);
  //I/O functions, sets the i'th emplyee to e
  void read(istream&);
  void print(ostream&) const;

private:
  enum {MAXSIZE = 100};
  Employee list[MAXSIZE];
  int count; //Number of employees in the current list
};

EmployeeList::EmployeeList() {
  count = 0;
}

EmployeeList::EmployeeList(istream& in) {
  //list[MAXSIZE] = in;
}

bool EmployeeList::isEmpty() const {
  return (count == 0);
}

bool EmployeeList::isFull() const {
  return (count == MAXSIZE);
}

int EmployeeList::size() const {
  return count;
}

Employee EmployeeList::item(int i) const {
}

void EmployeeList::setItem(int i, const Employee& e) {
}

void EmployeeList::read(istream& in) {
  Employee tempList;
  while (in >> tempList) {
  }
}

void EmployeeList::print(ostream& out) const {
  for (int i=0; i < size(); i++) {
  }

  cout << out;
}

The above part is the Class EmployeeList while the below part are overloading functions. The commented out parts are ideas that I thought might work but didn’t.

istream& operator>>(istream& in, EmployeeList& l) {
  l.read(in);
  return in;
}

ostream& operator<<(ostream& out, const EmployeeList& l) {
  l.print(out);
  return out;
}

Below is the main function given to us.

int main() {
  authorInfo();
  ifstream infile("a1in.txt");
  if(!infile) {
      cout << "file 'alin.txt' not found.";
      return EXIT_FAILURE;
  }
  EmployeeList theList(infile);

  cout << endl;
  cout << theList.size() << " employees read:\n" << theList << endl;
  process(theList);
  return EXIT_SUCCESS;

}

Hope someone can steer me in the right direction! Let me know if you need more of the code. Thanks!

EDIT:
Employee read and print functions:

void Employee::read(istream& in) {
  in >> name >> id >> salary;
}

void Employee::print(ostream& out) const {
  out << getName() <<" "<< getID() <<" "<< getSalary() << endl;
}

Employee overloading:

istream& operator>>(istream& in, Employee& e) {
  e.read(in);
  return in;
}

ostream& operator<<(ostream& out, const Employee& e) {
  e.print(out);
  return out;
}

EDIT 2: Updated read() function. The line with the while is where the error is.

void EmployeeList::read(istream& in) {
  Employee inEmployee;
  while (in >> inEmployee && count < MAXSIZE) {
      list[count] = inEmployee;
      count++;
  }
}

EDIT 3: Here is the print() function I have so far. It does indeed print but I get the default constructor information rather than information from the file. Is this a read or print function issue? I’m thinking read function still.

void EmployeeList::print(ostream& out) const {
  cout << endl;
  for (int i=0; i < count; i++) {
      out << list[count];
  }
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:50:04+00:00Added an answer on May 24, 2026 at 11:50 am

    Array Bounds

    In your class, you have:

    Employee list[MAXSIZE];
    

    Given this, there is an error the code you tried:

    EmployeeList::EmployeeList(istream& in) {
      list[MAXSIZE] = in;
    }
    

    list only has elements from list[0] to list[MAXSIZE - 1]. list[MAXSIZE] is one past the end of the array, and is invalid.

    Constructors

    That said, I’d strongly recommend against having a constructor that takes an istream&. It is far better to construct an empty object with the default constructor, then use its read(istream&) method (via operator <<) to load the data. In other words, rather than:

    EmployeeList theList(infile);
    

    use:

    EmployeeList theList;
    infile >> theList;
    

    If you’re required to have a constructor that takes an istream&, just have it call read() after initializing the object:

    EmployeeList::EmployeeList(istream& in): count(0) {
      read(in);
    }
    

    Note that only one constructor is called, so the initialization in EmployeeList::EmployeeList() does not happen in EmployeeList::EmployeeList(istream&). I hear the new version of C++ deals with this unnecessary repetition, but for the time being that’s where we are.

    Naming

    Another thing: your code will be less confusing with better variable names. In this case:

    void EmployeeList::read(istream& in) {
      Employee tempList;
      while (in >> tempList) {
      }
    }
    

    Don’t say tempList because it’s not a “temporary list”, it’s a single Employee that has been read. Better would be:

    void EmployeeList::read(istream& in) {
      Employee inEmployee;
      while (in >> inEmployee) {
        list[count++] = inEmployee;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to define the constant in the module that use the method from
I need to read a setting from the appsettings section (defined in app.config) in
I need to define a Wix file component that may not exist in certain
I need to define an appender for log4net in a way that I get
I need to define some constant strings that will be used only by one
So I have a FileReader class, it looks like this: #define DISALLOW_COPY(type) \ type(const
I need to define a calculated member in MDX (this is SAS OLAP, but
I need to define new UI Elements as well as data binding in code
I need to define multiple modules in the same file. I would like to
I need to define an <img>'s src attribute in CSS. Is there a way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.