Comparison of C++ and Ada CIS 635: Concepts of Programming Languages Assignment # 1 Due March 23, 2000 Syed Rais Ahmad Table of Contents 1 INTRODUCTION: 3 2 COMPARISON: 4 2.1 DATA TYPES: 4 2.1.1Numeric Data Types: 4 2.1.2Character Type Data: 5 2.2 NAMES, BINDING, TYPE CHECKING AND SCOPE: 5 2.2.1Names: 5 2.2.2Binding: 6 2.2.3Type Checking: 6 2.2.4Scope: 6 2.3 READABILITY: 6 2.4 COST: 7 2.4.1Training Programmers: 7 2.4.2Cost of Writing Programs: 7 2.4.3Language Design: 7 2.4.4Maintaining Programs: 7 3 CONCLUSION: 8 4 REFERENCES: 8 1 Introduction: The language I like to program in is C++. C++ is one of the most common and heavily used languages currently in the industry. The other programming language that I've chosen is Ada. The reasons I inclined towards Ada as the language for comparison are its known reliability, strongly disciplined code and internationally recognized standards. Where do these languages intersect? Answers could be many but some main commonalties are: * they both have support from ANSI, * both support multiple platforms and * support Object Oriented Paradigm of programming. How these two languages differ? Again, a whole book can be written on this topic but only a few criteria will be used to compare the two languages. 2 Comparison: The two languages will be differentiated or compared based on following criteria: 1. Data Types 2. Names, Binding, type checking and scope 3. Readability 4. Cost 2.1 Data Types: The data types are used to define the type of data that will be stored in a memory cell e.g. character, integer etc. The data types offered by a language itself are called primitive data types. 2.1.1 Numeric Data Types: Numeric data types include Integers and fractional or decimal numbers. 2.1.1.1 Integer: C++ offers integer data type int while Ada offers INTEGER. C++ int comes in three forms: plain, singed int, and unsigned int.In addition integers can have three sizes, short, long, or plain. Size of various integers mainly depends upon the implementation of C++. However it is guaranteed that a short has at least 16 bits and long has at least 32. The reason for so many choices for integers is for the programmers to take advantage of the hardware characteristics. Ada does not have so many choices in integers. The range of the INTEGER type in Ada is not defined and depends on compiler being used. Ada makes it very easy to specify the range of values that a variable, bound to a certain data-type, can attain. From the base INTEGER type any sub-range can be derived. E.g. a sub-type named 'negative' can be derived from INTEGER and contain only the list of negative integers. The two predefined sub-types of INTEGER are NATURAL and POSITIVE. NATURAL starts from zero while POSITIVE starts from one. Both have max value equal to the max value of INTEGER. The reason for defining these types in Ada is because it helps mimic the real world data requirements. If the data value goes outside the range, Ada can raise an exception. Ada performs strong type checking to provide reliability. 2.1.1.2 Real Numbers: C++ features float while Ada provides FLOAT, FIXED and DECIMAL data types. C++ float comes in three sizes float or single-precision, double or double precision and long double which has extended precision. A FLOAT data type specifies only the number of significant digits to be represented and does not guarantee any degree of accuracy over the entire range. A FIXED data type specifies a degree of accuracy that has to be maintained over the entire range of the type. A DECIMAL type combines a specification of significant digits and degree of accuracy. The most commonly used real data types are the float data types or FLOAT. There are no predefined sub-types of FLOAT. 2.1.1.3 Pointers: C++ and Ada both provide pointers. C++ provides * while Ada provides 'access' to define pointers. In both languages pointers can be used to reference a memory location or to allocate a new memory location to hold data. Ada's access variable, which has just been declared, is given a safe value of null. In C++ a declared pointer could point to any memory location. 2.1.1.4 Other Numeric Data Types: Both C++ and Ada feature Boolean and enumerated data types. Boolean type holds logical value or true or false. C++ provides bool while Ada's equivalent is BOOLEAN. Enumerated data types can be seen as use-defined ranged integer types. 2.1.2 Character Type Data: C++ provides char while Ada provides CHARACTER and STRING data types. C++ can also be modified using unsigned or signed char declaration. The minimum size of char data type is 8 bits. Both char and CHARACTER use eight bits to represent characters. In C++ char is nothing but a byte long integer. Ada also provides another character of type WIDE_CHARACTER which uses two bytes to represent a larger range of characters. Again sub-types of CHARACTER may be defined. For example, a sub-type of Character could be 'capital' allowing characters from (A to Z) only. STRING type is the additional data-type that Ada provides to manipulate strings. C++ does not offer any string type. In C++, strings are implemented as array of characters. 2.2 Names, Binding, Type Checking and Scope: 2.2.1 Names: The rules for forming the names or identifiers in Ada and C++ are almost similar. C++ and Ada do not put any restrictions on the length of names. Reserved words cannot be used as names. Names cannot contain spaces. Ada names must begin with at letter, though after that initial letter they may contain digits and underscores. Underscore can't begin or end and identifier. Also, two underscores cannot appear together. Valid names in Ada are: FIRST_LETTER, FirstLetter, Letter1 In C++ a name may even begin using an underscore. 2.2.2 Binding: Both Ada and C++ support static binding of variable attributes. C++ supports non-static binding while Ada supports restricted dynamic binding. The variables to be used in the program must be declared before they can be used. 2.2.3 Type Checking: C++ is not a strongly type language. Ada is almost strongly typed. C++ includes type void, which could mean any data type. Any data type can be assigned to a void variable. Similarly, C++ can allows functions for which parameters are not typed checked. It also allows unions, which allow variable of different types to share the memory locatoin. Ada can be explicitly programmed to suspend type checking for a particular type conversion. This is done using UNCHECKED_CONVERSON library function. A version of this function is available for every data type. The function returns the bit string of the current value of a variable. This value can then be assigned to another type variable for use. In this way, no actual conversion takes place. 2.2.4 Scope: The declaration introduces a name into a scope; that is, a name can be used only in a specific part of the program text. For a name declared in a declarative region, the scope extends from its point of declaration to the end of the block in which declaration occurs. In C++, a block or declarative region is a section of code delimited by a {} pair. In Ada, it is the region between procedure and the end of the procedure. 2.3 Readability: Readability of a language results in an ease of understanding and maintenance. Readability does not seem to be an accomplishing goal of C++ design. Ada programs however, include much English-like instructions and hence easy to read and maintain. Lets compare two short program of Ada and C++ with ADA.TEXT_IO; use ADA.TEXT_IO; procedure FirstProgram is TheStringr : constant STRING := "Hello World"; begin - First Program begins PUT_LINE( TheAnswer ); end FirstProgram; Equivalent C++ program states: include FirstProgram() { //First Program begins char TheString[]="Hello World"; cout << TheString; } Two output of two programs is a string "Hello World". The word procedure in Ada program designates a start of a procedure, in C++, the name of procedure along with brackets starts the procedure. Similarly, the words 'begin' and 'end' are replaced by curly brackets in C++ code. The short-hand writing style in C++ therefore, increase the maintenance of the programs and hence is expensive. 2.4 Cost: The ultimate cost of programming language is a function of many of its characteristics. 2.4.1 Training Programmers: Ada code being English-like it is easier to train new programmer in Ada than in C++. It is natural that if a new language one learns is similar to the language one already knows, the learning phase will be faster. In terms of training opportunities however, there are many institutes in the market today which are marketing C++ training opportunities far more than those of Ada. 2.4.2 Cost of Writing Programs: The cost of writing programs is less in C++ than in Ada. C++ development environments are available from many different vendors. The Visual Studio from Microsoft, e.g. provides a very user friendly environment to develop C++ programs integrating text editor, compiler, debugger, graphic user interface into one package. Similarly, many other development environment platforms are available for other platforms such as UNIX. Also C++ programmers are easier to find in the market today than those for Ada. Ada seems to be behind C++ on this ground. 2.4.3 Language Design: Language design is another factor for determining the cost of a language. C++ design makes it very close to the Assembly language, the computer mother tongue. This renders faster and efficient programs. Also, Ada requires many runtime type checks that prohibit faster execution of programs. However, Ada programs are far more reliable than those written in C++. 2.4.4 Maintaining Programs: Maintenance and easier addition of new features to the program also determine the language cost. Ada programs are very easy to read and hence easier to understand than C++. On the other hand, C++ programming practices and expertise are increasing day by day making the programmers easily available in the market. 3 Conclusion: Both C++ and Ada have their good and bad aspects. Ada has emerged as a strongly typed and reliable choice for the industry while C++'s faster code execution, brevity and array of supporting platforms is second to no other high level language. Despite its difficult to comprehend code and time-consuming phases to learn, the expertise and use of the C++ is on the rise in the industry. 4 References: The following resources were used web-sites: http://www.adaic.com http://info.acm.org/sigada http://lglwww.epfl.ch/Ada/ Books: The C++ Programming Language by Bjarne Stroustrup.