Class Employee ID count c++ [duplicate]



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0
















This question already has an answer here:



  • What is an undefined reference/unresolved external symbol error and how do I fix it?

    32 answers



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...











share|improve this question















marked as duplicate by Alan Birtles, IInspectable, Baum mit Augen c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 18 '18 at 19:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • you need to implement all the methods you have declared

    – Alan Birtles
    Nov 15 '18 at 8:11

















0
















This question already has an answer here:



  • What is an undefined reference/unresolved external symbol error and how do I fix it?

    32 answers



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...











share|improve this question















marked as duplicate by Alan Birtles, IInspectable, Baum mit Augen c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 18 '18 at 19:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • you need to implement all the methods you have declared

    – Alan Birtles
    Nov 15 '18 at 8:11













0












0








0









This question already has an answer here:



  • What is an undefined reference/unresolved external symbol error and how do I fix it?

    32 answers



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...











share|improve this question

















This question already has an answer here:



  • What is an undefined reference/unresolved external symbol error and how do I fix it?

    32 answers



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...






This question already has an answer here:



  • What is an undefined reference/unresolved external symbol error and how do I fix it?

    32 answers







c++ class count static






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 8:19







lala olala

















asked Nov 15 '18 at 7:56









lala olalalala olala

42




42




marked as duplicate by Alan Birtles, IInspectable, Baum mit Augen c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 18 '18 at 19:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Alan Birtles, IInspectable, Baum mit Augen c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 18 '18 at 19:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • you need to implement all the methods you have declared

    – Alan Birtles
    Nov 15 '18 at 8:11

















  • you need to implement all the methods you have declared

    – Alan Birtles
    Nov 15 '18 at 8:11
















you need to implement all the methods you have declared

– Alan Birtles
Nov 15 '18 at 8:11





you need to implement all the methods you have declared

– Alan Birtles
Nov 15 '18 at 8:11












2 Answers
2






active

oldest

votes


















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++;





share|improve this answer






























    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;






    share|improve this answer

























    • ok but now it doesn't count, 1000 and 1000 again.

      – lala olala
      Nov 15 '18 at 8:15











    • ComEmployee::ComEmployee() ID++;

      – snake_style
      Nov 15 '18 at 8:18












    • now both 1002, it worked but i need to have it as 1001, 1002

      – lala olala
      Nov 15 '18 at 8:22












    • Add new variable int m_ID, in class declaration. Then change ComEmployee::ComEmployee() m_ID = ID++; int ComEmployee::getID() const return m_ID;

      – snake_style
      Nov 15 '18 at 8:38












    • I tried that before, now output both: -858993460

      – lala olala
      Nov 15 '18 at 8:46

















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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++;





    share|improve this answer



























      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++;





      share|improve this answer

























        0












        0








        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++;





        share|improve this answer













        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++;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 9:07









        P.WP.W

        18.1k41657




        18.1k41657























            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;






            share|improve this answer

























            • ok but now it doesn't count, 1000 and 1000 again.

              – lala olala
              Nov 15 '18 at 8:15











            • ComEmployee::ComEmployee() ID++;

              – snake_style
              Nov 15 '18 at 8:18












            • now both 1002, it worked but i need to have it as 1001, 1002

              – lala olala
              Nov 15 '18 at 8:22












            • Add new variable int m_ID, in class declaration. Then change ComEmployee::ComEmployee() m_ID = ID++; int ComEmployee::getID() const return m_ID;

              – snake_style
              Nov 15 '18 at 8:38












            • I tried that before, now output both: -858993460

              – lala olala
              Nov 15 '18 at 8:46















            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;






            share|improve this answer

























            • ok but now it doesn't count, 1000 and 1000 again.

              – lala olala
              Nov 15 '18 at 8:15











            • ComEmployee::ComEmployee() ID++;

              – snake_style
              Nov 15 '18 at 8:18












            • now both 1002, it worked but i need to have it as 1001, 1002

              – lala olala
              Nov 15 '18 at 8:22












            • Add new variable int m_ID, in class declaration. Then change ComEmployee::ComEmployee() m_ID = ID++; int ComEmployee::getID() const return m_ID;

              – snake_style
              Nov 15 '18 at 8:38












            • I tried that before, now output both: -858993460

              – lala olala
              Nov 15 '18 at 8:46













            0












            0








            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;






            share|improve this answer















            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;







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 '18 at 9:16

























            answered Nov 15 '18 at 8:13









            snake_stylesnake_style

            1,170511




            1,170511












            • ok but now it doesn't count, 1000 and 1000 again.

              – lala olala
              Nov 15 '18 at 8:15











            • ComEmployee::ComEmployee() ID++;

              – snake_style
              Nov 15 '18 at 8:18












            • now both 1002, it worked but i need to have it as 1001, 1002

              – lala olala
              Nov 15 '18 at 8:22












            • Add new variable int m_ID, in class declaration. Then change ComEmployee::ComEmployee() m_ID = ID++; int ComEmployee::getID() const return m_ID;

              – snake_style
              Nov 15 '18 at 8:38












            • I tried that before, now output both: -858993460

              – lala olala
              Nov 15 '18 at 8:46

















            • ok but now it doesn't count, 1000 and 1000 again.

              – lala olala
              Nov 15 '18 at 8:15











            • ComEmployee::ComEmployee() ID++;

              – snake_style
              Nov 15 '18 at 8:18












            • now both 1002, it worked but i need to have it as 1001, 1002

              – lala olala
              Nov 15 '18 at 8:22












            • Add new variable int m_ID, in class declaration. Then change ComEmployee::ComEmployee() m_ID = ID++; int ComEmployee::getID() const return m_ID;

              – snake_style
              Nov 15 '18 at 8:38












            • I tried that before, now output both: -858993460

              – lala olala
              Nov 15 '18 at 8:46
















            ok but now it doesn't count, 1000 and 1000 again.

            – lala olala
            Nov 15 '18 at 8:15





            ok but now it doesn't count, 1000 and 1000 again.

            – lala olala
            Nov 15 '18 at 8:15













            ComEmployee::ComEmployee() ID++;

            – snake_style
            Nov 15 '18 at 8:18






            ComEmployee::ComEmployee() ID++;

            – snake_style
            Nov 15 '18 at 8:18














            now both 1002, it worked but i need to have it as 1001, 1002

            – lala olala
            Nov 15 '18 at 8:22






            now both 1002, it worked but i need to have it as 1001, 1002

            – lala olala
            Nov 15 '18 at 8:22














            Add new variable int m_ID, in class declaration. Then change ComEmployee::ComEmployee() m_ID = ID++; int ComEmployee::getID() const return m_ID;

            – snake_style
            Nov 15 '18 at 8:38






            Add new variable int m_ID, in class declaration. Then change ComEmployee::ComEmployee() m_ID = ID++; int ComEmployee::getID() const return m_ID;

            – snake_style
            Nov 15 '18 at 8:38














            I tried that before, now output both: -858993460

            – lala olala
            Nov 15 '18 at 8:46





            I tried that before, now output both: -858993460

            – lala olala
            Nov 15 '18 at 8:46



            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo