
A programming tool to ease modular programming with C++ J. Baltasar García Perez-Schofield Francisco Ortín Dpt. of Computer Science, University of Vigo Dpt. of Computer Science, University of Oviedo Edif. Fundición, s/n. Campus As Lagoas-Marcosende Calvo Sotelo s/n. 33007. Vigo, España Oviedo, España [email protected] [email protected] Abstract— Module management support is very rough in the C A. Motivation and C++ programming languages. Modules must be separated in As in many other computer science faculties (for instance, interface and implementation files, which will store declarations [4]), we have selected C++ to be the language of choice for the and definitions, respectively. Ultimately, only text substitution first courses. While in the first course it is preferred over C tools are available, by means of the C/C++ preprocessor, which is because of some higher abstractions (for instance, argument able to insert an interface file in a given point of a translation passing by reference), it does also share the same basic syntax unit. This way of managing modules does not take into account with many other programming languages. Although C-like aspects like duplicated inclusions, or proper separation of syntax is often criticized, its use is broadly considered an declarations and definitions, just to name a few. While the advantage, as learning the syntax part of the programming seasoned programmer will find this characteristic of the language language is time saved for other future programming annoying and error-prone, students will find it not less than languages, such as Java and C#. challenging. In this document, a tool specially designed for improving the support of modules in C++ is presented. Its main C++ was actually not designed for education: it was advantage is that it makes it easier to manage large, module- designed for providing to C a higher programming paradigm based projects, while still allowing to use classic translation units. such as object-oriented programming, while maintaining its This tool is designed for students who have to learn modular performance. C syntax was also designed to be minimal, not programming; not only those in the computer science discipline, simple or easy to understand. Thus, it is not surprising that its but also those in other engineerings in which programming is characteristics have a large room for improvement from an part of the curriculum. educational point of view, while still being a highly successful programming language. Keywords: Learning, module management, preprocessor, compiler, C++. One of the main challenges we have detected students must face with is module management, in which C++ provides no better abstraction than C. While in other programming I. INTRODUCTION languages, such as C#, or Java, modules are specified with high The C++ programming language has its origin in C, which detail, and even automatically managed by programming itself has its origin between 1969 and 1973 [16]. From that date environments [5], in C or C++ modules are simply source code of birth, the C programming language has undergone various scattered in various files (called translation units in C changes, firstly by its author, Dennis Ritchie, later by an ISO terminology). The rules regarding how programs should be standardization committee (ISO JTC1/SC22/WG14). Similarly, factorized in translation units are clearly specified; however, C++ was born at the beginning of 1990 decade, created by the relationship among the modules of the application and the Bjarne Stroustrup [17], and taking the existing C programming translation units which compose that application are not stated language at that time as basis. It has evolved heavily, firstly [4]. due to its author, and lately by an ISO standardization committee (ISO JTC1/SC22/WG21) [19]. An example illustrating the common problems when facing modular programming in C++ follows. The objective of such a Since other, more recent programming languages have not simple example would be to create a very simple module in had such a long history (Java [15], or C# [9]), they have which the PI constant and the sqr() function are defined. The successfully solved many problems found in C and C++, programmer will have to firstly understand that, in C and C++, avoiding them due to a fresh design start (a fresh start has the there is a separation of modules between the so called header advantage of peventing backwards compatability problems). In file (declarations, a .h file), and the implementation file fact, the author of C++ has himself made various proposals (definitions, a .cpp file). It is a good practice to divide regarding the simplification of his programming language [20], declaration and definition parts of modules, clearly stating the empowering new characteristics such as the use of STL library interface of each module with the rest. However, the or the explicit support of modular programming itself and mandatory division in two files is not even common currently. making programming simpler or homogeneous, meaning that This is just done in C++ that way because of the primitive ambiguities are kept to a minimum. mechanisms available at the time when it was designed. This This work has been partially funded by the Department of Science and Technology (Spain) under the National Program for Research, Development and Innovation; project TIN2008-00276 entitled “Improving Performance and Robustness of Dynamic Languages to develop Efficient, Scalable and Reliable Software”. mechanisms for module programming support are just simple subset of C++ is trying to get out from a language [20] that is text substitution. After all, many other modern programming so firmly rooted in C, thus exporting a very complex syntax. languages support modular programming. They emphasize For instance, it is possible to use signed and unsigned module interfaces, without the necessity of creating two characters; it is possible to mark as constant a pointer, the different files (their linkers are able to extract that information content it is pointing to, or even both... these and similar other automatically). characteristics are interesting in some specialized contexts, but from the educational point of view, they are confusing and Next, students will have to understand that the definitely not simple nor homogenous (from an educational implementation file needs to include its own interface as well, point of view, characters should just be able to represent all for just in case the constant PI is used inside it. characters the program can support; and also, marking a value // math.h as constant should have one and only one meaning). #ifndef MATH_H The main difference between standard C++ and this dialect #define MATH_H is the mandatory use of namespaces. While these are optional const double PI = 3.1415927; in C++, design issues suggest the benefits of using them double sqr(double x); (avoiding name clashing by means of making it possible to #endif arrange code in independent scopes, [18][19], introducing the need of a more comprehensive support for modules, in order to // math.cpp complement them). A modular version of C++, aimed at #include “math.h” students must obligatorily empower its higher-level double sqr(double x) characteristics. When low-level C++ features are really needed, { return x * x; } a modular approach is probably not the best one. In that cases, Finally, students will have to create and use the so called programmers will be facing with device driver programming or header guards (MATH_H in the code above), for the case in similar projects; that is why the tool was designed to share its which the header is included more than once in a single existence with standard C++ programming, instead of trying to translation unit (that would duplicate the existence of any class substitute it all. declared or constant defined in the header file, the very case of PI above). There are workarounds for avoiding this problem in More emphasis has been put in two major uses of the this very simple example, (though it involves learning the programming language for module creation: utility (function- meaning of the extern modifier), but header guards are required based) modules and classes (classed-based modules). First ones more often than not, so the beginner will have to understand are basically the best use of procedural programming, the (very low-level) basics of the preprocessor, and get used to regardless of whether object-oriented programming is used or creating them for any header file. not. Second ones are the expected in object-oriented programming in C++, except for the fact that it is not possible In this document, the Cp3-- tool is presented, allowing to to create objects along with the class declaration. program in a simplified C/C++ subset, specifically in regard to module management. The remaining parts of the document are A more detailed discussion on this topic is given elsewhere 1 presented after this section: firstly, the state of the art is [7]. Besides, this tool can be found in the authors' website . discussed, and then the main characteristics of the preprocessor are detailed. Some cases of study are then explained so it is A. Basic cases of study possible to study the main advantages of the tool. Finally, C++ is a hybrid programming language, supporting both results and initial performance measurements are shown, procedural and object-oriented programming. The support of following the conclusions. procedural programming is done through the wrap of a version of the C programming language, meaning that many programs II. THE HELP GIVEN BY THE MODULE MANAGER written in C can be compiled without modifications in C++. PROTOTYPE The main difference between the C programming language and the C version of the programming language included in C++ is Although this module manager was designed for the that the latter provides better compile-time verification standard ISO C++ programming language, the authors took mechanisms, mainly by adding type-safety to the original C.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages6 Page
-
File Size-