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:
- Add a new employee
- Display the existing employees
- Delete an employee details
- Update an employee details
- Employee class -- To define the basic attributes of Employee
- EmployeeRegister class -- To do the vector operations on Employee class
- 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:
- Adding, Deleting and Updating an employee.
- Displaying the current employee details.
- Searching for an element in the vector.
- 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.
Nice n Very Helpful.....
ReplyDelete