0

I'm editing it, now ok i apply an empty constructor and my code worked but this way:

Name1 SName1 1000 0.2 100 Name2 SName2 1000 0.3 750

This code normally works perfectly and my teacher especially wants us to use "this->" and "*this" so here's my code: my .h file

#ifndef COMEMPLOYEE_H
#define COMEMPLOYEE_H
#include <string>
using namespace std;
class ComEmployee
{
protected:
string firstName;
string lastName;
static int ID;
double grossSale;
double comRate;

public:
ComEmployee();
ComEmployee(string, string, double, double);
ComEmployee& setfirstName(string);
ComEmployee& setlastName(string);
ComEmployee& setgrossSale(double);
ComEmployee& setcomRate(double);
string getfirstName();
string getlastName();
double getgrossSale();
double getcomRate();
int getID() const;
double calCom() const;
void Display() const;

};


#endif

my .cpp file

#include "ComEmployee.h"
#include <iostream>

using namespace std;

int ComEmployee::ID = 1000;
ComEmployee::ComEmployee(){}

ComEmployee::ComEmployee(string name, string lname, double gs, double comr)
{
this->firstName = name;
this->lastName = lname;
this->grossSale = gs;
this->comRate = comr;
++ID;
}

int ComEmployee::getID() const
{
return ID;
}

ComEmployee & ComEmployee::setfirstName(string name)
{
firstName = name;
return *this;
}

ComEmployee & ComEmployee::setlastName(string lname)
{
lastName = lname;
return *this;
}

ComEmployee & ComEmployee::setgrossSale(double gs)
{
grossSale = gs;
return *this;
}

ComEmployee & ComEmployee::setcomRate(double comr)
{
comRate = comr;
return *this;
}

string ComEmployee::getfirstName()
{
return firstName;
}

string ComEmployee::getlastName()
{
return lastName;
}

double ComEmployee::getgrossSale()
{
return grossSale;
}

double ComEmployee::getcomRate()
{
return comRate;
}


double ComEmployee::calCom() const
{
return grossSale*comRate;
}

void ComEmployee::Display() const
{
cout << firstName << " "  << " " << getID() << " " << comRate << " " << calCom() << endl;
}

here's my main.cpp file

#include <iostream>
#include "ComEmployee.h"

using namespace std;

int main()
{
ComEmployee employee, employee2;
employee.setfirstName("Name1").setlastName("SName1").setgrossSale(500).setcomRate(0.2).Display();
employee2.setfirstName("Name2").setlastName("SName2").setgrossSale(2500).setcomRate(0.3).Display();

system("pause");

}

I want my output as:

Name1 SName1 1001... Name2 SName2 1002...

lala olala
  • 17
  • 3

2 Answers2

0

Add some int variable to your class definition:

int m_ID;

Then, you need to emplement a default constructor in cpp-file:

// cpp
ComEmployee::ComEmployee()
{
    m_ID = ID++;
}

And modify your GetID function:

int ComEmployee::GetID() const
{
    return m_ID;
}
snake_style
  • 1,139
  • 7
  • 16
0

You have to change your design a bit. As ID is employee ID, it cannot be a static variable. A static variable belongs to the class rather than to particular instance of the class.
Define ID as a normal member of the class.

You can use a new static variable CurrentID instead to keep track of the employees.
After defining it like below,

int ComEmployee::CurrentID = 1000;

In the default and non-default constructors, do the following:

this->ID = CurrentID++;
P.W
  • 26,289
  • 6
  • 39
  • 76