Modern C++ - Program structure

Financed from the financial support ELTE won from the Higher Education Restructuring Fund of the Hungarian Government.

3. The structure of C++ programs

C++ programs consist of - Comments - Preprocessor directives - C++ tokens

Comments:

 1 // single line comment (since C99) from // to end of line
 2 
 3   /* multi
 4      line    
 5      comments
 6      // hiding single line comments
 7    */
 8 
 9   /*
10       /*  but must not be nested */
11    */

No comments inside strings: “/* this is not comment */”

1 /*************************************\
2 *                                     *
3 *  exist in various style and format  *
4 *                                     *
5 \*************************************/

Preprocessor directives

starting with ‘#’

 1 #ifdef 
 2 #define MY_HEADER 
 3 #include <header.h>
 4 
 5 #if MY_PLATFORM
 6 #define BUFFER 100
 7 #else
 8 #error platform not defined
 9 #endif
10 
11 #endif

… more on preprocessor in the next lecture

C++ tokens

keywords

all lowercase

identifyers

starts with letter (incl _underscore) continues with letters or numbers

literals

operators

separators

{ } , ;

 1 //
 2 // C++ style
 3 // 
 4 #include <iostream>   <---- preprocessor directvive
 5 #include <string>     <---- preprocessor directvive
 6 
 7 std::string myname();    std <-- namespace name: identifier
 8                          string <-- class name: identifier
 9                          myname <-- function name: identifier
10                          ::     <-- scope: operator
11                          ()     <-- function call: operator
12 int main()               int    <-- type name: keyword
13                          main   <-- function name: identifier
14                          ()     <-- function call: operator
15 {                        {      <-- block begin: separator
16                          cout   <-- variable: identifier
17   std::cout <<           <<     <-- output : operator
18                  "my name is: " <-- string literal, type char[13]
19             << myname()
20             << std::endl;     ; <-- command-end separator
21                          return <-- keyword
22   return 0;              0      <-- decimal int literal, type int
23 }
24 std::string myname()
25 {
26   return "John Cepepe Doe";  // automatic conversion from char * to std::string
27 }

Keywords

we will learn them …

Identifiers

Different conventions:

A research on this: https://whathecode.wordpress.com/2011/02/10/camelcase-vs-underscores-scientific-showdown/

results on February 2015: - camelCase: 52.5 % - underscore: 47.5 %

MACRO_NAMES_ARE_ALL_UPPERCASE by convention

A paper on this: http://www.cs.loyola.edu/~binkley/papers/icpc09-clouds.pdf

Literals

void

Notes the return types of functions with no return value.

Integral types

sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
// at least 16 bit             at least 32 bit    at least 64 bit

Characters

1 == sizeof(char) < sizeof(char16_t) <= sizeof(char32_t) <= sizeof(wchar_t)

Boolean

Boolean type bool is considered as integral type.

Floating point numbers

sizeof(float) <= sizeof(double) <= sizeof(long double)

Complex floating points

Declared as a template type in <complex> header. The template parameter can be any floating point type.

Size is not fixed! (C++ is NOT Java!)

int ==> best for integral computation, “machine word”

double ==> best for floating point computation

 1 //
 2 // This is _very_ compiler/platform specific
 3 //
 4 #include <iostream>
 5 
 6 int main()
 7 {
 8   std::cout << sizeof(char) << std::endl;
 9   std::cout << sizeof(bool) << std::endl;
10   std::cout << sizeof(short) << std::endl;
11   std::cout << sizeof(long) << std::endl;
12   std::cout << sizeof(long long) << std::endl;  // only from C++11
13   std::cout << sizeof(float) << std::endl;
14   std::cout << sizeof(double) << std::endl;
15   std::cout << sizeof(long double) << std::endl;
16   std::cout << sizeof(int*) << std::endl;
17   std::cout << sizeof("Hello world") << std::endl;
18 
19   return 0;
20 }
$ ./a.out
1
1
2
4
8
8
4
8
16
8
12

There is a standard library header <limits> containing MIN and MAX values for various types.