Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,650 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,147 people online right now. Registration is fast and FREE... Join Now!




Need help in understanding overloading

 
Reply to this topicStart new topic

Need help in understanding overloading

tdigdug
post 20 Aug, 2008 - 05:55 PM
Post #1


New D.I.C Head

*
Joined: 20 Aug, 2008
Posts: 1

Im currently taking an online c++ programming course and I would have to say that. I wish I was in a class room to ask the teacher. Any way I am currently working on a payrool programm. I stuck on the programm close to the end of the class. I need to use inherit which that is no problem. but I also need to use both classes to sort. Merge sort part I can fig out myself. But i need to sort the data in each class first. This is where I am stuck. I think what I need to do is overlaod operator but not sure on how or if this is what I really need to do. Hopefully someone here can help. It would be greatly appreciated any nudges in the right direction. big or small nudges. Hopefully big. Here is what I have written so far.
CODE
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdlib>
using namespace std;
//declaration of variables
class emp{
public: string id;
  int n, m, hw, oth, indexOfMin;
  float hr, otp, rgp, gp, tax, np, TAXR;
  char fname[15], lname[15], ms;
  void findgp();
  void findtaxr();
  void findtax();
  void findnp();
  void headings();
  void start(); };//STRUCT
class hourly: virtual public emp{
         float temp;
         void findovt();
         void sSort();
         void outputall();
         void soutputall();
      public:
         hourly();
          ~hourly();
         void printreport();
         void finished(); };
class salary: virtual public emp{
  int m;
  float yearsal;
  void yearsalgp();
  void sSort();
  void outputall();
  void soutputall();
public:
  salary();
  ~salary();
  void printreport();
  void finished(); };

hourly::hourly(){
   ifstream fin("payrollfl.txt");}
   hourly::~hourly(){
   ifstream fin();}

salary::salary(){
   ifstream fin("sal.txt");}
   const int SIZE=100;
   salary sal[SIZE];
   salary::~salary(){
   ifstream fin();}

void hourly::findovt(){
       if(hw>40){
        oth=hw-40;
        otp=oth*hr*1.5;
        rgp=hw*hr;
        }//IF
       else{
       oth=0;
       otp=0;
       rgp=hw*hr;
       }
       }

void salary::yearsalgp(){
  hw=40;
  hr=(yearsal/52)/40;
  rgp=hr*40;
  gp=rgp+otp;
  oth=0;
  otp=0;
  }

void emp::findgp(){
    gp=rgp+otp;
}//FINDGP


void emp::findtaxr(){
   if ((gp>1000) && (ms=='S'||ms=='s')) TAXR=0.35;
   else if ((gp>1000) && (ms=='M'||ms=='m')) TAXR=0.30;
   else if ((gp>1000) && (ms=='H'||ms=='h')) TAXR=0.25;
   else if ((gp>800) && (ms=='S'||ms=='s')) TAXR=0.25;
   else if ((gp>800) && (ms=='M'||ms=='m')) TAXR=0.20;
   else if ((gp>800) && (ms=='H'||ms=='H')) TAXR=0.15;
   else if ((gp>500) && (ms=='S'||ms=='s')) TAXR=0.15;
   else if ((gp>500) && (ms=='M'||ms=='m')) TAXR=0.10;
   else if ((gp>500) && (ms=='H'||ms=='h')) TAXR=0.05;
   else TAXR=0.0;
}//FINDTAXR

void emp::findtax(){
     tax=gp*TAXR;
}//FINDTAX

void emp::findnp(){
     np=gp-tax;
}//FINDNP

void emp::headings(){
    cout<<setiosflags(ios::left)<<"                                Digdug's National Bank"<<endl;
    cout<<setiosflags(ios::left)<<"     (Employee id 5000 series hourly employees, 4000 series are salary based employees)"<<endl;
  cout<<setiosflags(ios::left)<<setw(15)<<"FIRST NAME"<<setw(15)
<<"LAST NAME"<<setw(7)<<"EMP ID"<<setw(4)<<"HW"<<setw(6)
<<"HR"<<setw(5)<<"STAT"<<setw(5)<<"OTH"<<setw(7)<<"OTP"
<<setw(7)<<"REGP"<<setw(9)<<"GROSS"<<setw(7)<<"TAX"
<<setw(7)<<"NET"<<endl<<endl;
  cout<<setiosflags(ios::left)<<setw(15)<<"=============="
<<setw(15)<<"=============="<<setw(7)<<"======"
<<setw(4)<<"==="<<setw(6)<<"====="<<setw(5)<<"===="
<<setw(5)<<"==="<<setw(7)<<"======"<<setw(7)<<"======"
<<setw(9)<<"========"<<setw(7)<<"======"<<setw(7)
<<"======"<<endl;
}

void hourly::outputall(){
  cout<<fixed;
     cout<<setiosflags(ios::showpoint|ios::fixed|ios::left)<<setw(15)<<fname
       <<setw(15)<<lname<<setw(7)<<id<<setw(4)<<setprecision(1)<<hw
       <<setw(6)<<setprecision(2)<<hr<<setw(5)<<ms<<setw(5)
       <<setprecision(1)<<oth<<setw(7)<<setprecision(2)<<otp<<setw(9)
       <<rgp<<setw(9)<<gp<<setw(7)<<tax<<setw(7)<<np<<endl;
}//OUTPUTALL

void salary::outputall(){
  cout<<fixed;
     cout<<setiosflags(ios::showpoint|ios::fixed|ios::left)<<setw(15)<<fname
       <<setw(15)<<lname<<setw(7)<<id<<setw(4)<<setprecision(1)<<hw
       <<setw(6)<<setprecision(2)<<hr<<setw(5)<<ms<<setw(5)
       <<setprecision(1)<<oth<<setw(7)<<setprecision(2)<<otp<<setw(9)
       <<rgp<<setw(9)<<gp<<setw(7)<<tax<<setw(7)<<np<<endl;
}//OUTPUTALL
void hourly::soutputall(){
cout<<fixed;
        for (int i=0; i<n-1; i++){
   cout<<setiosflags(ios::showpoint|ios::fixed|ios::left)<<setw(15)<<fname
       <<setw(15)<<lname<<setw(7)<<id<<setw(4)<<setprecision(1)<<hw
       <<setw(6)<<setprecision(2)<<hr<<setw(5)<<ms<<setw(5)
       <<setprecision(1)<<oth<<setw(7)<<setprecision(2)<<otp<<setw(9)
       <<rgp<<setw(9)<<gp<<setw(7)<<tax<<setw(7)<<np<<endl;
}//OUTPUTALL
}
void salary::soutputall(){
cout<<fixed;
   cout<<setiosflags(ios::showpoint|ios::fixed|ios::left)<<setw(15)<<fname
       <<setw(15)<<lname<<setw(7)<<id<<setw(4)<<setprecision(1)<<hw
       <<setw(6)<<setprecision(2)<<hr<<setw(5)<<ms<<setw(5)
       <<setprecision(1)<<oth<<setw(7)<<setprecision(2)<<otp<<setw(9)
       <<rgp<<setw(9)<<gp<<setw(7)<<tax<<setw(7)<<np<<endl;
}//OUTPUTALL

void hourly::sSort(){
    for (int pass=1;pass<=n;pass++){
        for (int i=0; i<n-1; i++)
            if (np[i] > np[i+1]) {
             int temp = np[i]; np[i] = np[i+1]; np[i+1] = temp;
             }
        }
}


void salary::sSort(){
    for (int pass=1;pass<=m;pass++){
        for (int i=0; i<5-1; i++)        
            if (np[i] > np[i+1]) {
                salary temp = np[i]; np[i] = np[i+1]; np[i+1] = temp;
             }
        }
}

void emp::start(){
     headings();
     }

void hourly::printreport(){
     ifstream fin("payrollfl.txt");
     n=0;
     while(fin>>fname>>lname>>id>>hw>>hr>>ms){
     findovt();
     findgp();
     findtaxr();
     findtax();
     findnp();
     outputall();
     n++;}
}
void salary::printreport(){
     ifstream fin("sal.txt");
     while(fin>>fname>>lname>>id>>yearsal>>ms){
     yearsalgp();
     findtaxr();
     findtax();
     findnp();
     outputall();}
}

void hourly::finished(){
     sSort();
     soutputall();}

void salary::finished(){
     sSort();
     soutputall();}

//main function
int main(void){
  emp a;
  hourly b;
  salary c;
  a.start();
  b.printreport();
  c.printreport();
  b.finished();
  c.finished();
system("pause");
}//MAIN
// function definitions
//end  
User is offlineProfile CardPM

Go to the top of the page

lordms12
post 21 Aug, 2008 - 07:07 AM
Post #2


D.I.C Regular

Group Icon
Joined: 16 Feb, 2008
Posts: 312



Thanked 13 times

Dream Kudos: 225
My Contributions


If you are going to sort just numbers you will not need to use operator overloading.
Operator overloading example:
c++
class MyString 
{
public:
char member1[100];
void operator +(MyString val)
{
strcat(member1, val.member1);
}
};


If you wanted to make your sort used in whatever you want you can write your code to take a pointer to comparing function before started.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 05:10AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month