|
 |
 |
[an error occurred while processing this directive]
- 5. Implementing a class
Writing a class implementation consists of creating constructor and destructor functions and of defining other member functions to access the class data.
// class definition
// file employee.cpp
#include <iostream.h>
#include <string.h>
#include employee.h
Employee::Employee(void)
{
memset(LastName, 0x00, strlen(FirstName));
memset(LastName, 0x00, strlen(LastName));
}
Employee::~Employee(void)
{
}
void Employee::DisplayEmployee(void)
{
cout << FirstName << " " << LastName << endl;
cout << "Date of birth: " << BirthDate.dday;
cout << "/" << BirthDate.mmonth << "/" << BirthDate.yyear;
cout << endl;}void Employee::SetEmployee(void)
{
char bdString[80];
cout << "Enter First Name: ";
cin >> FirstName;
cout << "Enter Last Name: ";
cin >> LastName; cout << "Enter Salary: ";
cin >> Salary;
while (true)
{
cout << Enter Birthdate (mm/dd/yy);
cin >> bdString;
BirthDate.dday = (bdString[3] - 0)*10 +
bdString[4] - 0;
BirthDate.mmonth = (bdString[0] - 0)*10 +
bdString[1] - 0;
BirthDate.yyear = (bdString[6] - 0)*10 +
bdString[7] - 0;
if ((BirthDate.dday >= 1) && (BirthDate.dday <= 31))
if ((BirthDate.mmonth >= 1) && (BirthDate.mmonth <= 12)) break;
}
}
The constructor and destructor functions are quite obvious. The most interesting part of the code is access functions.
The SetEmployee function enables reading the data that the user enters via a keyboard. The function fills the private data members with the entered data. Because we hide the data (we cannot access the data directly), this function becomes very important. Actually, this function provides the only way to set an employees data.
A very common approach is to add validation procedures to this type of function. In our case, the validation routine checks the correctness of the entered date (not very intelligently, however). If the date is incorrect, the function repeats the question to enter the date of birth. Adding a validation code is very important because this is the way to control the class data.
The DisplayEmployee function provides another window to the external world. This function shows the data in the class. Assuming that the Salary field might be confidential, we dont display it.
- 6. Testing the class
To make sure that our class works, we are going to run a simple program that defines an Employee object, gets the data, and then displays the data on the screen.
// file empltest.cpp
#include employee.h
int main()
{
Employee EmplTest;
EmplTest.SetEmployee();
EmplTest.DisplayEmployee();
return 0;
}
If you try to access the data members directly, the compiler will generate error messages. For example, if you try to display the employees last name with the following statement:
cout << EmplTest.LastName;
the compiler will complain.
- 7. Amending the class
The two access functions provide a minimum set of operations on the class data. This pair of operations (sometimes it is Get and Set, or Read and Write) is usually needed for a new data type.
Other operations are usually very specific. In our example, the operations can be GetSalary (for getting the Salary member) and CompareEmployees (for comparing two Employee objects).
int Employee::GetSalary(void)
{
return Salary;
}
int Employee::CompareEmployees(Employee E1, Employee E2)
{
return strcmp(E1.LastName, E2.LastName);
}
Comments
Encapsulation provides a methodology for creating true object-oriented programs. By hiding the data from other objects and specifying access functions, you can create a new data type with complete operational support.
Encapsulation does not provide security for the data. It only prevents programming mistakes and cant prevent the destruction of data if someone really wants to destroy it. Using encapsulation allows for more understandable programs and produces a better programming style.
4.4 Create my own operators?
Problem
When I create new data types and specify functions, sometimes I dont like to use functions when I could be using operators. For example, when creating a class for complex numbers, I prefer to use the (+) operator rather than the Add member function and the (-) operator rather than the Subtract member function. In other words, I prefer
Complex1 = Complex2 + Complex3;
Complex1 = Complex2 - Complex3;
to
Complex1 = Complex2.Add(Complex3);
Complex1 = Complex2.Subtract(Complex3);
where Complex1, Complex2, and Complex3 are objects of the complex numbers class.
Technique
To perform this kind of addition of class objects, you have to create special member functions that overload the binary addition operator (+) and subtraction operator (-) when they appear between two objects of the complex numbers class.
The technique, known as operator overloading, allows you to overload almost all C++ operators except (.), (.*), (::), (?:), and sizeof. In this section we cover in detail the technique for overloading binary operators.
|