27 maggio 2007
How to use classes in C++ (lesson #1)
Visto e considerato che a scrivere in inglese, per quanto i risultati siano ancora tendenti all’orrido, ci sto prendendo gusto. Visto e considerato che mi sembra di essere tornato indietro di qualche mese, a quando ero chino sui libri a studiare per qualche esame. Visto e considerato che la tecnica migliore per studiare, per il sottoscritto, è sempre stata quella di leggere e scrivere riassunti (spesso più lunghi del libro originale, ma la mia incapacità di sintesi è un’altra storia). Beh, ecco che ora provo a farlo in inglese. Con un mini-tutorial su come utilizzare le classi in C++. D’accordo, non uno degli argomenti più stimolanti del pianeta, ma da qualche parte si doveva pur partire. E siccome sto studiando proprio questa roba…

We begin with an example. Imagine you have to develop a class, called “GradeBook” dedicated to manage a grade book that an instructor can use to maintain student test scores. Take a look to the code posted below:
[line 1] #include < iostream >
[line 2] using std::cout;
[line 3] using std::endl;
[line 4] class GradeBook {
[line 5] public:
[line 6] void displayMessage() {
[line 7] cout << "Welcome to the Grade Book!" << endl;
[line 8] }
[line 9] };
[line 10] int main() {
[line 11] GradeBook myGradeBook;
[line 12] myGradeBook.displayMessage();
[line 13] return 0;
[line 14] }
This is a complete and working C++ program. The first three lines are dedicated to preliminary operations: to include the iostream library and to provide the programmer with the possibility of call cout and endl functions, from the STD library, without the need of use the complete std::function_name statement. Lines 2 and 3 provide an advantage (in terms of memory occupation) with respect to the more common using namespace std instruction, which include all the functions contained in the C++ Standard Template Library.
The GradeBook class definition starts at line 4, with a very simple syntax:
class ClassName {
At line 5 we can see a public: statement, better known as an “access-specifier label”. Its role is to signal that the following member functions and/or data members, still belonging to this class, are “available to the public” (i.e., they could be accessed everywhere along the program – not only by the member functions belonging to the class). All the member functions and data members defined after the public: statement – until the compiler will find the right bracket and the semicolon that end the class definition or until a different access-specifier label (e.g., private:) is encountered – belong to the defining class.
The instruction presented at line 6 starts to define a member function for the GradeBook class, called displayMessage. This function doesn’t returns any value (its return type has been set as void) to its calling function, as well as it doesn’t need any input parameters (parenthesis following its name are empty).
Again, the syntax is simple:
return_type functionName(parameters_list) {
Then, at line 7, we have the instruction (yes, just one) composing the body of displayMessage member function. This instruction simply prints a welcome message on the screen.
At line 8 the member function definition ends, as well as at line 9 the right bracket and the semicolon character signal the end of the class definition.
Now it’s time to look at the main function. First of all (line 11), we create a GradeBook object (an instance of GradeBook class, in class-related lexicon) called myGradeBook, using the following syntax:
ClassName instanceName;
Then we call the displayMessage function (line 12), with refer to the just created object. The C++ syntax needs the name of the object, followed by a dot (.) and then by the name of member function to be called (including the eventual list of parameters).
instanceName.functionName(parameters_list);
Lines 13 and 14, finally, close the program.
Congratulation. You just created your first class.
[The example provided has been entirely copied from:
H.M. Deitel, P.J. Deitel - "C++ How to Program"]


