Saturday, June 30, 2012

Vector Operations in C++ -- Part 3

In the last post, we implemented the searching procedure followed in the Employee Register. Now we will discuss the CRUD operations on the vector with Employee class.

To go to the previous two posts,
Vector Operations in C++ -- Part 1 and
Vector Operations in C++ -- Part 2

For adding an employee, we need to first check if another employee of the same employee ID exists in the vector. If it exists, we should give the relevant message to the user. Since we know that adding or deleting the employees at the ends is done very quickly, we have used the function "push_back" to add the new employee to the end.



For displaying the employee details, we must first check if the vector is empty. If it is empty, we need not traverse through the vector.



For deleting and updating an employee details, we need to check if the employee with the given ID exists. If it does not exist, the relevant message must be displayed.



Till now we have done all the operations required for manipulating or displaying the employee data. But a good user interface which is easily understandable by the user is equally important. Thus, we will have a menu with all the above discussed operations. To take numerical and string input from the user, we use two member functions in the class "EmployeeDisplay". Validation to the complete extent is not done for numerical and string inputs.

Compiling and running the case study:

Compiling:
g++ -o EmployeeRegister main.cpp Display.cpp Employee.cpp EmployeeRegister.cpp
Running the executable:
./EmployeeRegister

Sample Output for an add operation in the following menu is given below.



I have uploaded the entire project at the following link: Vector Operations Case Study. You can download the "Employee Register.zip" from this link.

Vector Operations in C++ -- Part 2

Have some information about vectors and want to implement the vector operations?? You have come to the correct place.
In the last post, we have discussed about the operations in vectors and the functions that can be used in vectors. If you do not know about vectors in C++, then refer this page. In this post, we will see the practical implementation of vectors by using a case study.

We are building an employee register in which an employee has two attributes -- a unique Employee ID and the Employee Name. If you want more attributes such as Date of Birth, Work experience, Designations etc., you can add them. But I thought of keeping it simple. So, I have included only two attributes for an employee. I am not linking this case study to any database programming. But I am sure this case study would be very useful in many of the applications based on CRUD and can be used in applications which include database programming. The following actions are done in this case study:
  1. Add a new employee
  2. Display the existing employees
  3. Delete an employee details
  4. Update an employee details
We will have a total of three classes:
  1. Employee class  -- To define the basic attributes of Employee
  2. EmployeeRegister class  -- To do the vector operations on Employee class
  3. EmployeeDisplay class --  Created for User Interface programming

To ensure data abstraction, the header files are separated from the actual implementation. In the header files, the declarations of the variables, classes and methods will be present, while the implementation is covered in the corresponding CPP source files.

First let us see what Employee class has. The following piece of code is Employee.h which declares the variables and the member functions of Employee class.



The implementation of the Employee class are given below. The getters and setters are defined so as to access the member variables in the Employee class.



The implementation of EmployeeRegister class has the following operations:
  1. Adding, Deleting and Updating an employee.
  2. Displaying the current employee details.
  3. Searching for an element in the vector.
  4. Checking whether the vector for Employee class is empty.
We can check if the vector is empty by using the boolean function "empty()" used in vector STL.



Searching is an important operation and will be used by almost all other functions. Imagine you have large number of employee records and you need to search for a record which is the last in the vector. By using linear search, the search process will consume more time. For binary search to be implemented, we need to have the elements in the vector in an increasing order which is not the case here. They can be any possible order. Thus, due to all these factors, the STL algorithm "find" is used to find the position of an element in the vector. For this function to be used, we need to create another vector of integers "emp_id_records". This vector should have all the employee ID's exactly in the same order as that of the emp_records. So, if we can get the position of an element in one vector, we can the index of the other too. 



Related Posts Plugin for WordPress, Blogger...