Solved

C++ sorting class objects

Posted on 2004-10-07
3
514 Views
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
      {
            private:
                  int empNo;
                  string name;
                  char gender;
                  int age;
                  double rate, hours, pay, tax, netPay;

            public:
                  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].getEmployee();
                  e[x].calculatePay(15.00, 40.00);
            }
      
      
      for (x=0; x<5; x++)
            {
                  e[x].showEmployee();
            }
      
    return 0;
}
0
Comment
Question by:mjaugust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 30

Expert Comment

by:Axter
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.
0
 
LVL 3

Accepted Solution

by:
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.
0
 
LVL 3

Expert Comment

by:aravindtj
ID: 12260570
hi,
 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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

749 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