Question

In: Computer Science

C++ Change the program to take user input for first name and last name for five...

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");
}

Solutions

Expert Solution

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


Related Solutions

C++ Write a program that asks a teacher to input a student’s first name, last name,...
C++ Write a program that asks a teacher to input a student’s first name, last name, and four test scores. The program should find the average of the four test scores and should then write the following information to a file named “students.txt” last_name first_name average A student's first name of “XX” should be used as a sentinel value and no numeric grades less than 0 or greater than 100 should be accepted.  The program should then read the information in...
Create a program that allows a user to input customer records (ID number, first name, last...
Create a program that allows a user to input customer records (ID number, first name, last name, and balance owed) and save each record to a file. When you run the main program, be sure to enter multiple records. Once you create the file, open it and display the results to the user Save the file as  CustomerList.java
Write a program that prompts the user to input their first name from the keyboard and...
Write a program that prompts the user to input their first name from the keyboard and stores them in the variable "firstName". It does the same for last name and stores it in the variable "lastName". It then uses strcat to merge the two names, separates them by a space and stores the full name into a string variable called "fullName". In the end, the program must print out the string stored within fullName. ANSWER IN C LANGUAGE ! You...
PYTHON Modify the program in section Ask the user for a first name and a last...
PYTHON Modify the program in section Ask the user for a first name and a last name of several people.  Use a loop to ask for user input of each person’s first and last names  Each time through the loop, use a dictionary to store the first and last names of that person  Add that dictionary to a list to create a master list of the names  Example dictionary: aDict = { "fname":"Douglas", "name":"Lee" } ...
Python Code: Write a program to prompt the user to enter a first name, last name,...
Python Code: Write a program to prompt the user to enter a first name, last name, student ID, number of hours completed and GPA for 5 students. Use a loop to accept the data listed below. Save the records in a text file. Write a second program that reads the records from your new text file, then determines if the student qualifies for Dean’s list or probation. Display the records read from the file on screen with appropriate headings above...
Create a program that keeps track of the following information input by the user: First Name,...
Create a program that keeps track of the following information input by the user: First Name, Last Name, Phone Number, Age Now - let's store this in a multidimensional array that will hold 10 of these contacts. So our multidimensional array will need to be 10 rows and 4 columns. You should be able to add, display and remove contacts in the array. In Java
You have been asked to write program that allows the user to input a first name,...
You have been asked to write program that allows the user to input a first name, middle initial (without the period), and last name of a user and then display that person’s name with the first, middle initial followed by a period, and last name last. BEFORE creating the program, use structured programming principles to document how you are going to develop the program. Use Microsoft Word to complete this part of the assessment. Answer each of the following areas:...
JAVA Take in a user input. if user input is "Leap Year" your program should run...
JAVA Take in a user input. if user input is "Leap Year" your program should run exercise 1 if user input is "word count" your program should run exercise 2 Both exercises should run in separate methods Exercise 1: write a java method to check whether a year(integer) entered by the user is a leap year or not. Exercise 2: Write a java method to count all words in a string.
Design and implement a C++ Application that: Interactively input: Employee First Name Employee Last Name Employee...
Design and implement a C++ Application that: Interactively input: Employee First Name Employee Last Name Employee id Employee hours worked per week Employee Pay Rate Menu with Option to: Print out Employee Report in the following report format Search for an Employee Display the Report in Sorted based on Last Name or ID Calculate the Pay Quit Criteria: Be sure to use Parallel Arrays to Store the Employee Information Output must be formatted and line up with the column header...
Write a C++ Program to print the first letter of your first and last name using...
Write a C++ Program to print the first letter of your first and last name using stars. Note: 1) Using nested For Loop 2) The number of lines is given by user. 3) Using one Outer loop to print your letters. 4) Print the letters beside each other.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT