|
 |
 |
[an error occurred while processing this directive]
5.2 Use and access a classs data members?
Problem
Many beginning C++ programmers are confused by the issue of public and private variables. A common question is How do I access a data member within a class? Quite often, a beginner simply defines a global (global to the whole program) variable that she can then use anywhere. Obviously, this is not the optimal solution because after you start using global variables, they are not then tied to any particular class. In the end, your program ends up redeclaring several variables that exist both in the class and the main program. In essence, this misses the point of encapsulation (and therefore object-oriented programming) altogether.
Technique
Data members are used within a class to hold internal data that is needed by that classs member functions. It is a basic principle of object-oriented programming that data members can be accessed only by an objects own member functions. For this reason, a classs data members are often declared as private.
The idea of keeping the data members private to the class is the standard method of programming because it takes away the need of the programmer to know how things inside of the class are stored. All the user has to know is how to correctly create an instance of the class to use the class to perform its required job. A class is an abstract notion that states the relationship between member variables that hold data and are hidden within the class and the member functions that manipulate that hidden data. The specification of this relationship needs to be written only once. When a programmer creates an instance of a class, computer memory is allocated to house the member variables and the member functions. This physical presence of the class is called an object; it is said to be concrete. The act of creating an instance of a class can be repeated many times; therefore, many concrete objects can be created from a single class.
Steps
During the design of a class, the programmer should plan what internal data needs to be stored. The programmer then needs to decide whether the data is to be private to the class or public to the program by remembering that the data members should be invisible to the user. As an example, you will design a program that creates a class that makes use of global variables. You will then modify the class to use private data members. Hopefully, you will see the difference.
- 1. Open up your C++ development environment (or a text editor) and create a new C++ source file called list5.21.cpp.
- 2. Add the following class to the editor.
// Usage of Global Variables.
#include <iostream.h>
float Area=0;
int Height=0;
int Length=0;
class Test
{
public:
void CalcArea();
void ShowArea();
};
Immediately, you can see that the variables (data members) are not where they are supposed to be. In fact, they are declared as global variables.
- 3. Add the member function definitions:
void Test::CalcArea()
{
Area += Height*Length; //Find Area
}
void Test::ShowArea()
{
cout << The Area is << Area << endl;
}
Here the member functions are manipulating the variables Area, Height, and Length, but these variables are not declared anywhere in the class. This will cause problems later.
- 4. Finally, add the main section of the program.
void main(void)
{
cout << Enter a Height ; //Gather input
cin >> Height;
cout << Enter a Length ;
cin >> Length;
Test Square; //Create instance
Square.CalcArea(); //Call Member Functions
Square.ShowArea();
cout << Enter a Height ; //Gather input
cin >> Height; cout <<
Enter a Length ;
cin >> Length;
Test Box; //Create Another Instance
Height++; //Main has altered a value!!
Box.CalcArea();
Box.ShowArea();
}
Both main and the class can see and use those variables. The key is that both can alter them. Because two instances of the class are using the same variables, the data might not be up-to-date. In fact, the program has been designed to show this exact thing. In the calculation for Area, you are using +=, which adds to a value. Because Area is being used elsewhere, it might not be initialized to 0. Therefore, the resulting answer will be incorrect.
Figure 5.8 shows whats going on...
Figure 5.8 Using global variables.
The figure shows that all parts of the program can see and use the variables. If Instance 1 alters them, they are altered for Instance 2. Therefore, the result for Instance 2 will be incorrect.
- 5. To correct the problem, you must store the data as member variables of the Test class and pass values to them via a constructor.
Figure 5.9 shows the required design.
Figure 5.9 Correct scope of variables.
In this example, the class has three member variables. All the user needs to do is use a constructor to pass in values to Length and Height. The member functions then perform their duties and calculate a resulting area. By using private data members, the user only needs to know that the class requires two inputs. From there, the class performs the calculation internally. Because the data members are private to their own instance, they are totally separate. This means the data has not been altered before the member functions use it; therefore, every instance will produce the correct answer.
- 6. This sample program fragment uses the correct object-oriented method. First of all, look at the class declaration.
#include <iostream.h>
class Test
{
private:
float Area;
int Height; //Private DataMembers
int Length;
public:
void CalcArea();
void ShowArea();
Test(int H, int L); //Constructor
};
Include the member variables in the private section of the class. By doing this, only the member functions can utilize them.
- 7. Next, add the member function definitions as shown in the next program fragment.
void Test::CalcArea()
{
Area = Height*Length; //Find Area
}
void Test::ShowArea()
{
cout << The Area is << Area << endl;
}
Test::Test(int H, int L)
{
Height = H; //Pass parameters
Length = L;
}
The constructor is used to pass information from the main program across the class boundary to the internal private data members.
- 8. Finally, add the main program section that creates two instances of the class and utilizes the two member functions each time.
|