C++ sorting class objects

Posted on 2004-10-07
Last Modified: 2010-04-01
I am new to C++ and OOP. I have a program that prompts an end user for user data, calculate pay, and display the information as an employee record. I am attempting to sort the employee records by employee numbers. I have worked out the logic for the sort (below) but I am having problems displaying the data with the corresponding employee numbers.

Here is my logic for the sort:
Employee e[5];
int i,j,Employee temp
for (i=0; i<5; i++) n[i]=rand()%i101
for (i=0; i<4; i++)
      for(j=i+1; j<5; j++)
      if (e[i].getEmpNo() > e[j].getEmpNo())
      {temp = e[i]; e[i]= e[j]; e[j]=temp;}

Here is my working program:
// H81.cpp : This program prompts the end user five times for specific employee data, calculate the pay, and displays the employee record
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Employee
                  int empNo;
                  string name;
                  char gender;
                  int age;
                  double rate, hours, pay, tax, netPay;

                  void setEmployee(int e, string n, char g, int a, double r, double h)
                        {      empNo=e; name=n; gender=g; age=a; rate=r; hours=h;            }

                  void getEmployee() //User is prompted for employee info
                              cout << "+----+----+ ENTER EMPLOYEE DATA +----+----+" << endl;
                              cout << "Employee Number: "; cin >> empNo;
                              cout << "Employee Name: "; cin >> name;
                              cout << "Gender: "; cin >> gender;
                              cout << "Age: "; cin >> age;
                              cout << "Hourly Rate: "; cin >> rate;
                              cout << "Hours Worked: "; cin >> hours;
                              cout << " " << endl;

                  void calculatePay(double r, double h) // Pay calculated here
                        {rate=r; hours=h; pay=r*h; tax=.31*pay; netPay=pay-tax;}

                  int getEmpNo() {return empNo;}

                  void showEmployee() // Employee record displayed here
                        {   cout << "+----+----+  Employee Record +---+----+ " << endl;
                              cout << "Employee Number: " << empNo << endl;
                              cout << "Employee Name: " << name << endl;
                              cout << "Gender: " << gender << endl;
                              cout << "Age: " << age << endl;
                              cout << "Hourly Rate: " << rate << endl;
                              cout << "Hours Worked: " << hours << endl;
                              cout << "Gross Pay: " << pay << endl;
                              cout << "Taxes (31%): " << tax << endl;
                              cout << "Net Pay: " << netPay << endl;                              

int main(int argc, char* argv[])
      Employee e[5];
      int x;

      e[0].setEmployee(55555, "Mary", 'F', 21, 15.50, 40.00); // Initializing arg
      for (x=0; x<5; x++)
                  e[x].calculatePay(15.00, 40.00);
      for (x=0; x<5; x++)
    return 0;
Question by:mjaugust
LVL 30

Expert Comment

ID: 12256034
>>I am having problems displaying the data with the corresponding employee numbers.

Please explain in detail the exact problem you're having, and post a specific question.

Your above post does not have an actual question.

Accepted Solution

teratoma earned 200 total points
ID: 12256237
One way C++ simplifies this type of thing is to provide standard containers and algorithms.  You could put your Employees in a std::vector or a std::list to start with.  Then, given that std::sort can sort any container (see note) given that the contained class defines operator< (less than), you only need to fill in the blanks:

class Employee
  bool operator<(const Employee& e)
    return ???;

Note: your class also needs a copy constructor (maybe an assignment operator too) if you want to use std::sort.  You don't need one if you store your objects in a list and use list::sort.

Expert Comment

ID: 12260570
 y not you use STL? for sorting object, you can just write a sorting algorithm only. All others will be done automatically. Look at STL algorithms and Templates.

All the best.

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question