Array of Objects

本文介绍了一个简单的员工管理系统的设计与实现,该系统能够添加员工信息、显示所有员工的数据,并为所有员工提供加薪功能。通过主函数调用实现了基本的菜单驱动交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

You should be comfortable with the content in the modules up to and including the module "Arrays" for this project.

Create a class called consultCo that holds a private class called employee that contains the name, pay rate and social security number of an employee of a consulting firm called consultCo. The consultCo class should also have an array that holds all of the objects of all of the employees in the array.  This will be an array of employee objects.

Here is a logical diagram of this array of objects:

 

 

You will also need functions to satisfy the following requirements:

Minimum Requirements:

·        An add function to hire a new employee.  This function should create a new object with the employee information (name, payrate and social security number) and then it should add that new object to the array.  You are not required to update an employee’s data if the employee exists. (5 points)

·         A report function that will display the name, pay rate and social security number of all employees. (5 points).

·        A raise function that will give all employees a 10% raise. (5 points).

·        You will also need a main function that creates calls the function(s) you created to meet the requirements of this project (5 points).

Notes:

The Module Lab Activity "Lookup!" can be extremely helpful for this project.  Just by following the lab activity (and the video demos if needed), you can make significant progress toward completing this assignment.

 

 

Answer

 

#include"class.h"


int main()
{

   bool running = true;
   companyCo database;

   int choice = 0;

   while (running == true)
   {
      std::cout << "\n1. Add an employee" << std::endl;
      std::cout << "\n2. Check database" << std::endl;
      std::cout << "\n3. This is a good day... INCREASE ALL SALARIES BY 10%!" << std::endl;
      std::cout << "\n4. Exit\n" << std::endl;
      std::cin >> choice;

      if (choice == 1)
      {
         std::cout << "Add an employee" << std::endl;
         database.addEmployee();
      }

      else if (choice == 2)
      {
         std::cout << "Check database" << std::endl;
         database.displayData();
      }

      else if (choice == 3)
      {
         database.increaseSalary();
      }

      else
      {
         std::cout << "Exit" << std::endl;
         running = false;
      }
   }

   return 0;
}

 


#include"class.h"

companyCo::companyCo()
{
   //to keep the size of the array small enough for testing purposes...
   size = 7;
   populated = 0;
}

companyCo::employee::employee()
{
   //Default value for all the unpopulated areas within the array
   name = "not applicable";
   payRate = 0.0;
   ssN = 000;
}


void companyCo::addEmployee()
{
   employee record;

   std::string name;
   float payRate;
   int ssN;

   std::cout << "Please enter the Name: " << std::endl;
   std::cin >> name;
   std::cout << "Please enter the Payrate: " << std::endl;
   std::cin >> payRate;
   std::cout << "Please enter the SSN: " << std::endl;
   std::cin >> ssN;

   record.name = name;
   record.payRate = payRate;
   record.ssN = ssN;
   std::cout << "Record added\n\n\n" << std::endl;

   //update table
   entries[populated] = record;
   populated++;
}


void companyCo::displayData()
{
   int limit; //user can specify how much to output

   std::cout << "Enter the number of eployees you want to see: " << std::endl;
   std::cin >> limit;

   for (int i = 0; i<limit; i++)
   {
      std::cout << entries[i].name << std::endl;
      std::cout << entries[i].payRate << std::endl;
      std::cout << entries[i].ssN << std::endl;
      std::cout << std::endl;
   }
}


void companyCo::increaseSalary() //increases salary by 10% however, all new members of the array will have no benefit from this...
{
   for (int i = 0; i<size; i++)
   {
      entries[i].payRate = entries[i].payRate * 1.1;
   }

   std::cout << "\nSalary increased.\n" << std::endl;

  木其工作室

 

#include<iostream>
#include<sstream>


class companyCo
{
private:
   class employee
   {
   public:
      std::string name;
      float payRate;
      int ssN;
      employee();
   };
   int size;
   int populated;
   employee entries[7]; //7.. is a magic number

public:
   companyCo();
   void addEmployee();
   void displayData();
   void increaseSalary();
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值