In: Computer Science
C++
Change the program to take user input for first name and last name for five employees. Add a loop to read the first name and last name.
// EmployeeStatic.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
Employee(const std::string&, const
std::string&); // constructor
~Employee(); // destructor
std::string getFirstName() const; // return first
name
std::string getLastName() const; // return last
name
// static
member function
static unsigned int getCount(); // return # of objects
instantiated
private:
std::string firstName;
std::string lastName;
// static data
static unsigned int count; // number of objects
instantiated
};
// Employee class member-function definitions.
// define and initialize static data member at global namespace
scope
unsigned int Employee::count{ 0 }; // cannot include keyword
static
// define static member
function that returns number of
// Employee objects
instantiated (declared static in Employee.h)
unsigned int Employee::getCount() { return count; }
// constructor initializes non-static data members and
// increments static data member count
Employee::Employee(const string& first, const string&
last)
: firstName(first), lastName(last) {
++count; // increment static count of employees
cout << "Employee constructor for " <<
firstName
<< ' ' << lastName
<< " called." << endl;
}
// destructor decrements the count
Employee::~Employee() {
cout << "~Employee() called for " <<
firstName
<< ' ' << lastName
<< endl;
--count; // decrement static count of employees
}
// return first name of employee
string Employee::getFirstName() const { return firstName; }
// return last name of employee
string Employee::getLastName() const { return lastName; }
// static data member tracking the number of objects of a class.
int main() {
// no objects exist; use class name and binary scope
resolution
// operator to access static member function
getCount
cout << "Number of employees before
instantiation of any objects is "
<< Employee::getCount()
<< endl; // use class name
// the following scope creates and
destroys
// Employee objects before main terminates
{
Employee e1{ "Susan", "Baker"
};
Employee e2{ "Robert", "Jones"
};
// two objects exist; call
static member function getCount again
// using the class name and the
scope resolution operator
cout << "Number of employees
after objects are instantiated is "
<<
Employee::getCount();
cout << "\n\nEmployee 1:
"
<<
e1.getFirstName() << " " << e1.getLastName()
<<
"\nEmployee 2: "
<<
e2.getFirstName() << " " << e2.getLastName() <<
"\n\n";
}
// no objects exist, so call static member function
getCount again
// using the class name and the scope resolution
operator
cout << "\nNumber of employees after objects are
deleted is "
<< Employee::getCount()
<< endl;
system("pause");
}
Employee.cpp
#include<iostream>
#include<string.h>
using namespace std;
// Employee class
class Employee
{
// data members
string firstName;
string lastName;
public:
// default constructor
Employee()
{
firstName=lastName="";
}
// parameterized constructor
Employee(string first,string last)
{
firstName=first;
lastName=last;
}
// function to set first name of employee
void setFirstName(string first)
{
firstName=first;
}
// function to get first name of employee
string getFirstName()
{
return firstName;
}
// function to set last name of employee
void setLastName(string last)
{
lastName=last;
}
// function to get last name of employee
string getLastName()
{
return lastName;
}
};
int main()
{
// create Emplyee object array of size 5
Employee employees[5];
// variable declaration
string firstName,lastName;
for(int i=0;i<5;i++)
{
cout<<"Enter first name for
employee "<<(i+1)<<": ";
cin>>firstName; // read first
name
cout<<"Enter last name for
employee "<<(i+1)<<": ";
cin>>lastName; // read last
name
// set first name to employee
employees[i].setFirstName(firstName);
// set last name to employee
employees[i].setLastName(lastName);
}
cout<<endl<<endl<<"Names of 5
employees: "<<endl;
for(int i=0;i<5;i++)
{
// display employee full name
cout<<employees[i].getFirstName()<<"
"<<employees[i].getLastName()<<endl;
}
return 0;
}
Output