|
 |
 |
[an error occurred while processing this directive]
- 7. Should we change the main function?
No, we will not have to change the main function in our example.
The changes that we have made to the class implementation are internal changes. A user of the class usually doesnt know how the class is implemented. Because the data is hidden, and the function declarations havent been changed, an external program remains the same.
The changes that we made to the class constructor dont change the main program because we allow a default initialization value for the stack length. However, because the class provides us now with a more powerful constructor, we can select the length of the class ourselves in the external program. For example
SStack TestStack(800);
would initialize a stack 800 characters long.
Comments
Classes provide us with a lot of powerful features to do object-oriented programming. We can encapsulate data and create member functions for the data access. This produces modular code and allows parts of the code to be changed without redesigning the whole program.
Interestingly, that C++ syntax allows us to use structures exactly as we use classes. The only difference between classes and structures is that the default access is private in classes and public in structures. Basically, the question When do I use structures and when do I use classes? is a question of programming style. The most common programming style is to use structures only for pure data handling and for creating new data types without specifying operations on them. If you create a program that uses member functions in structures, it will work but other programmers will have a tougher time maintaining it.
Encapsulation is a methodology that hides data in classes, allowing access to that data only by functions defined in the class. The functions also provide necessary operations on the data, thereby increasing the power of classes as new data types. Classes can be distributed as class libraries in an object form with the interface defined by the member functions.
4.3 Use encapsulation? What steps are required to encapsulate data?
Problem
I want to hide data in my class library in a way that nobody has access to it. How can I use encapsulation to make sure that nobody will be able to change or destroy my data?
Technique
A very important thing to understand is that hiding data from other programmers working on the same project is not always a wise choice. This can result in making the code more complicated and therefore less maintainable. Encapsulation does not put vulnerable data in restricted memory areas; it just hides the data at compilation time.
Another very important thing to understand is that the intention to hide the data from other people is wrong. There is no technique on the language level for securing the data, and encapsulation was invented to make the code more readable and object-oriented. The process of data encapsulation has a few steps.
We are going to give an example of encapsulation by creating an employee data type for a business application.
Steps
- 1. Determine the data
Creating a new data type involves describing components of the data in the application. If we decide to create a new employee data type, we have to specify the information about employees that our application needs.
First of all, we need FirstName and LastName fields. The fields can be character strings and be defined later in the code or they can be character arrays with fixed lengths. In our example, well use arrays to make the code a little simpler. Another group of fields that we definitely need is a BirthDate (didnt I say that we would create an application for Human Resources?) For the date of birth, we are going to use the DDate structure we defined in How-To 4.1. We will also add a last piece of data named Salary that we can declare as int.
The data can be declared as shown in the following:
struct DDate{
int dday;
int mmonth;
int yyear;
};
char FirstName[35];
char LastName[35];
DDate BirthDate;
int Salary;
- 2. Structures or classes?
Now we have to decide what we are going to use. If we were not going to encapsulate the data, or if we just needed the data for keeping the employee information, or if we were not going to create functions for handling the data, it would be reasonable to use structures:
struct DDate{
int dday;
int mmonth;
int yyear;
};
struct Employee{
char FirstName[35];
char LastName[35];
DDate BirthDate;
int Salary;
};
However, we want to hide the data using encapsulation and create functions to access the data. Therefore, we are going to create a class for supporting the new data type. This is a common practice and following it makes your code more understandable for other programmers.
- 3. Create access functions
Because we are going to hide the employee data from other program modules, it is important to provide functions to access the data. Assuming that the only operation we want to make the data visible is displaying the data, we declare the appropriate function:
void Display(void);
The function will be a member of the class that handles the employee data, so it does not need parameters.
Another function should enable the addition of the employee data; for example, by typing it on the keyboard. We declare the SetEmployee function for this purpose:
void SetEmployee(void);
When we hide data by encapsulation, we often provide functions to access the data: a function for reading the data and/or a function for setting the data.
- 4. Declaring a class
To write a class declaration we have to specify the following: a class name (which is going to be Employee), a private section, and a public section. Because we want to hide the data, we are going to write an appropriate declaration in the private section. The functions are indented to provide data access for external modules, so they will be located in the public section.
// employee class declaration
// file employee.h
class Employee{
private:
struct DDate{
int dday;
int mmonth;
int yyear;
};
char FirstName[35];
char LastName[35];
DDate BirthDate;
int Salary;
public:
Employee(void); // constructor
~Employee(void); // destructor
void DisplayEmployee(void);
void SetEmployee(void);
};
|