# Basic Structure of Programs First, let's take a look at the first program most people learn - `Hello, world`. ```cpp #include<iostream> // Header file, importing libraries." using namespace std; // Namespace, delineating the meaning of functions. int main() { // Main program code. cout << "Hello, world" << endl; // Output. system("pause"); // Program pause. return 0; // Return value. } ``` --- ## Header File and Namespace In `C++`, header files and namespaces are two important concepts that help organize and manage code, improving the readability and modularity of the program. ### Header File Header files are typically used to declare classes, functions, variables, etc., and are included by other code. They usually have the file extension `.h` or `.hpp` and contain class definitions, function prototypes, etc. Here's an example of what a header file might look like: ```cpp= // MyClass.h #ifndef MYCLASS_H #define MYCLASS_H class MyClass { public: MyClass(); // Constructor void someFunction(); private: int someVariable; }; #endif // MYCLASS_H ``` In the example above, `MyClass.h` header file contains the declaration of the MyClass class. To prevent multiple inclusions, commonly referred to as header guards, `#ifndef`, `#define`, and `#endif` macros are used at the beginning. Other code can include this header file using ``#include <MyClass.h>`` to use MyClass. --- ### Namespace Namespaces are used to prevent name collisions. In larger programs, different developers may write code with the same names for classes, functions, etc. To avoid such conflicts, `C++` introduces the concept of namespaces. Namespaces are typically defined in header files but can also be defined in source files. Here's a simple namespace definition: ```cpp= // MyNamespace.h #ifndef MYNAMESPACE_H #define MYNAMESPACE_H namespace MyNamespace { void someFunction(); } #endif // MYNAMESPACE_H ``` Implementing this namespace in a source file: ```cpp= // MyNamespace.h #ifndef MYNAMESPACE_H #define MYNAMESPACE_H namespace MyNamespace { void someFunction(); } #endif // MYNAMESPACE_H ``` Namespaces help different code libraries or modules avoid name collisions and organize code more clearly. Now let's look at the code for the earlier 'Hello, world' program. If we don't include `using namespace std;` at the beginning of the code, it would look like this: ```cpp #include<iostream> // Header file, importing libraries."of functions. int main() // Main program code. { std::cout << "Hello, world" << std::endl; // Output. system("pause"); // Program pause. return 0; // Return value. } ``` As you can see, when using certain code covered by the standard library, it is necessary to precisely use `std::` to indicate that it belongs to the `std` namespace in order for it to be effectively executed by the system, because C++'s standard library is defined under `std`. Let's consider a realistic scenario. For example, today there is a university student attending `NCCU`, and his name is `Aldrich`. (Even though this name is rare, there is still a possibility of having the same name) Suppose there is another university student attending `CCU`, and his name is also `Aldrich`. How can educational institutions avoid confusing their identities? The solution is to prefix their respective schools. (For example: `NCCU::Aldrich` and `CCU::Aldrich`) Think of namespaces as school names, which makes it easier to understand. --- ## Main Program Code Following the `int main() { code... }` is straightforward. It's where the main code of the program resides during execution. If you change the name randomly, the compiler will throw an error because it can't find 'main'. --- ## Output Next is `cout` and `endl`(endline). `cout` is for output, and its counterpart is `cin` for input, while `endl` is for newline. Earlier, it was mentioned that they are defined under the `std` namespace and the `iostream` library. So it's necessary to import both the library and the namespace for them to have meaning. Regarding the rules for output, it's generally: `cout << "text to output";` Where `<<` is necessary syntax. `endl` can be added within or at the end, depending on the need. --- ## Program Termination At the end, the two lines: The purpose of `system("pause");` is mainly significant when running the executable file (.exe). And `return 0;` within `main()` can be understood as returning a value to inform the computer that the program has finished execution. However, `return` in C++ has other uses as well, but that's a topic for later discussion. The presence or absence of the above two lines of code will have different effects. Please try running the executable file (.exe) on your computer and observe any differences in the display information in the command prompt window. --- ## Other Possible Structure ### Input There are several ways to input data in C++: - `std::cin` - `std::getline` - `std::stringstream` - `scanf` - `std::istream::get` #### `std::cin` This is the standard input stream object in the `C++` standard library. You can use the `>>` operator to extract data from the standard input stream and store it in variables. ```cpp= int number; std::cin >> number; ``` #### `std::getline` This function is used to get a line of text from the input stream and store it in a `string`. ```cpp= std::string line; std::getline(std::cin, line); ``` #### `std::stringstream` If you need to extract data from a string, you can use the std::stringstream class. ```cpp= #include <sstream> std::string input_string = "10 20 30"; std::stringstream ss(input_string); int num1, num2, num3; ss >> num1 >> num2 >> num3; //num1 = 10, num2 = 20, num3 = 30. ``` #### `scanf` This is a `C-style` input function that can also be used in `C++`, but it's not recommended as it's less safe for handling incorrect input. ```cpp= int number; scanf("%d", &number); ``` #### `std::istream::get` This function is used to get data character by character from the input stream. ```cpp= char ch; std::cin.get(ch); ``` --- ### Declare Variables In `C++`, you can declare variables of various types. #### Primitive Data Types ```cpp= // Integer types int myInteger; //-2,147,483,648 ~ 2,148,483,647; - 2^31 ~ 2^31 - 1 short myShort; //-32,768 ~ 32,767; - 2^15 ~ 2^15 - 1 long myLong; //-2,147,483,648 ~ 2,147,483,647; - 2^31 ~ 2^31 - 1 long int myLongInteger; //-2,147,483,648 ~ 2,147,483,647; - 2^31 ~ 2^31 - 1 long long myLongLong; //-9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807; - 2^63 ~ 2^63 - 1 long long int myLongLongInteger; //-9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807; - 2^63 ~ 2^63 - 1 //Unsigned integer types unsigned int myUnsignedInteger; //0 ~ 4,294,967,295; 0 ~ 2^32 - 1 unsigned short myUnsignedShort; //0 ~ 65,535; 0 ~ 2^16 - 1 unsigned long myUnsignedLong; //0 ~ 4,294,967,295; 0 ~ 2^32 - 1 unsigned long int myUnsignedLongInteger; //0 ~ 4,294,967,295; 0 ~ 2^32 - 1 unsigned long long myUnsignedLongLong; //0 ~ 18,446,744,073,709,551,615; 0 ~ 2^64 - 1 unsigned long long int myUnsignedLongLongInteger; //0 ~ 18,446,744,073,709,551,615; 0 ~ 2^64 - 1 // Floating-point types float myFloat; //3.4E +/- 38 (Seven decimal places) double myDouble; //1.7E +/- 308 (Fifteen decimal places) long double myLongDouble; //1.7E +/- 308 (Fifteen decimal places) // Character types char myChar; wchar_t myWideChar; // wide character (Not covered in beginner tutorials.) ``` #### Boolean Type ```cpp= bool myBoolean; // True and False; 1 and 0 ``` #### Pointers ```cpp= // Pointer to an integer int* myPointerToInt; // Pointer to a character char* myPointerToChar; ``` #### Arrays ```cpp= // Array of integers int myIntArray[10]; // Array of characters; aka. string char myCharArray[20]; ``` #### Constants ```cpp= // Constant integer const int MY_CONSTANT_INT = 10; // Constant float const float PI = 3.14f; ``` --- ## Practice - `dandanjudge:` - [`a033`](<https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a033>) - [`a196`](<https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a196>) - [`a767`](<https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a767>) - [`a780`](<https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a780>) - (a767 and a780 are just for fun. Try them out!)