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...
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" } ...
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.
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.
5. Take user input and give corresponding output. User will enter a sentence. The program will...
5. Take user input and give corresponding output. User will enter a sentence. The program will output the word that appears most frequently in this sentence. If there are multiple words with same frequency, output the first of these words. Please enter a sentence: I like batman because batman saved the city many times. The most frequent word is: batman The frequency is: 2 PYTHON
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
Write the pseudocode that prompts the user for their first and last name. Display the first...
Write the pseudocode that prompts the user for their first and last name. Display the first initial of their first name and their last name to the user. Ask the user to input a phone number. The program checks which part of Colorado a phone number is from using the values below. If the second digit of the phone number is one of the below digits, print the phone number and which part of Colorado it is from. If none...
C# (Thank you in advance) Create an Employee class with five fields: first name, last name,...
C# (Thank you in advance) Create an Employee class with five fields: first name, last name, workID, yearStartedWked, and initSalary. It includes constructor(s) and properties to initialize values for all fields. Create an interface, SalaryCalculate, class that includes two functions: first,CalcYearWorked() function, it takes one parameter (currentyear) and calculates the number of year the worker has been working. The second function, CalcCurSalary() function that calculates the current year salary. Create a Worker classes that is derived from Employee and SalaryCalculate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT