Solved

C++ sorting class objects

Posted on 2004-10-07
3
477 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
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now