




























































































Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Prepara i tuoi esami
Studia grazie alle numerose risorse presenti su Docsity
Prepara i tuoi esami con i documenti condivisi da studenti come te su Docsity
Trova i documenti specifici per gli esami della tua università
Preparati con lezioni e prove svolte basate sui programmi universitari!
Rispondi a reali domande d’esame e scopri la tua preparazione
Riassumi i tuoi documenti, fagli domande, convertili in quiz e mappe concettuali
Studia con prove svolte, tesine e consigli utili
Togliti ogni dubbio leggendo le risposte alle domande fatte da altri studenti come te
Esplora i documenti più scaricati per gli argomenti di studio più popolari
Ottieni i punti per scaricare
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Documento scritto da me in inglese sul linguaggio C++. La dispensa spiega il linguaggio, la sua sintassi e la sua logica in maniera chiara e semplice con illustrazioni chiave per trasmettere i concetti che spesso non vengono compresi in modo comprensibile. Si parte dalle basi fino alle struct passando per programmazione dinamica, puntatori, variabili globali e tutorial tecnici. Ogni argomento è corredato di esercizi svolti per comprendere meglio gli argomenti. Questo documento è adatto per chiunque indipendentemente dall'università/corso voglia approfondire la programmazione C++ in maniera strutturata e completa.
Tipologia: Dispense
1 / 128
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!





























































































C++ is a powerful, general-purpose programming language that was developed by Bjarne Stroustrup in 1979 as an extension of the C programming language. Originally called "C with Classes," it was designed to bring the benefits of object-oriented programming (OOP) to C, a language widely known for its efficiency and close relationship with system hardware. The name was later changed to C++ in 1983, reflecting the language’s evolution, with "++" symbolizing the increment operator in C, hinting at its enhancement over the original language. C++ was built to offer both high-level abstractions for easier software design and low- level memory control, making it versatile and performance-oriented. C++ was created to solve some of the limitations of C, particularly the need for a language that could handle both complex software systems and hardware-level tasks. Its core purpose is to provide developers with the tools to write efficient, high- performance code while maintaining the flexibility to manage system resources directly, such as memory allocation and hardware interaction. This balance makes C++ well-suited for system programming, game development, and applications requiring real-time processing. Over the years, C++ has undergone several updates and revisions, with modern features being added through versions like C++11, C+ +14, C++17, and C++20. These updates introduced features like smart pointers, lambda expressions, and concurrency support, ensuring that the language remains relevant for modern development needs. C++ is widely used in a variety of industries and applications. In systems programming, it is often the go-to choice for developing operating systems, device drivers, and embedded systems due to its fine-grained control over hardware and memory. In game development, C++ powers many game engines like Unreal Engine, where performance and real-time rendering are critical. Additionally, C++ is used in high-performance computing, financial applications (such as algorithmic trading systems), and even in the development of large-scale applications like web browsers, including Google Chrome and Firefox. Its flexibility, efficiency, and widespread adoption make C++ a crucial tool for any developer working on resource- intensive or performance-critical applications.
To start programming in C++, you need two main components: a C++ compiler and an Integrated Development Environment (IDE). The compiler is responsible for translating your C++ code into machine code that your computer can execute, while the IDE helps you write, debug, and manage your code more efficiently. Some popular IDEs for C++ include Visual Studio Code (VSCode), CLion, and Visual Studio. Although the installation steps are similar across platforms, certain steps will differ depending on whether you are using Windows or macOS/Linux. Step 1: Install a C++ Compiler
C++ code must be compiled into machine code before it can be executed. The process of compiling and running a C++ program involves several steps, but most modern IDEs streamline this for you.
The basic structure of a C++ program revolves around several factors. A basic structure of a C++ program is: int main() { return 0; } Let's break down all the pieces of the code
C++ applies some specific rules and guidelines for naming variables. These rules ensure that variable names are valid and avoid conflicts with reserved words or other constructs used by the programming language. Rules for Naming Variables are:
Comments are lines or blocks of text in the code that are ignored by the compiler. They are used to explain the code, document its functionality, or temporarily disable code during development. C++ supports two types of comments:
Assignment Assignment refers to the process of assigning a value to a variable. We refere to assignment when the variable is already declared. This can happens when a variable change its stored value during the program logic. The syntax for assignment is: VARIABLE_NAME = VALUE ; It involves using the assignment operator, which is typically the equals sign (=), to store a value in a variable. The value on the right side of the equals sign is assigned to the variable on the left side. For example: int number; number = 42; float temperature; temperature = 36.5; double pi; pi = 3.14159; char grade; grade = 'A'; bool passed; passed = true;
Initialization When you initialize a variable, you assign it an initial value at the time of declaration. This is important because using an uninitialized variable can lead to undefined behavior, as it may contain garbage values left in memory. The syntax is: DATA_TYPE VARIABLE_NAME = VALUE ; or in case of more initialization: DATA_TYPE VARIABLE_NAME1 = VALUE1, VARIABLE_NAME2 = VALUE2 ; It works exactly like the assignment. For example: int number = 42; float temperature = 36.5; double pi = 3.14159; char grade = 'A'; bool passed = true; Memory cell with the variable name now store Value the assigned value. &
In C++, program memory is divided into several distinct regions, each with a specific purpose and behavior. Understanding these memory areas can help you write efficient and safe code, especially when working with dynamic memory allocation or managing variables with different lifetimes. The main areas of memory in a typical C++ program are:
Stack : The stack is a region of memory used for storing local variables, function parameters, return addresses, and function call information. It is managed automatically by the operating system in a Last-In, First-Out (LIFO) manner, which means that the last item added to the stack is the first one removed. The stack is automatically managed by the system. When a function is called (e.g. main()), memory for local variables and parameters is pushed onto the stack in a stack frame. When the function returns (e.g. return 0; in main()), its stack frame is popped off, and the memory is freed immediately. All the variables declared in our code are allocated on the stack when the code is compiled. Thus, during the execution all the necessary memory items will be ready to use. Large data structures can lead to stack overflow, where the program exceeds the stack’s allocated size and crashes. main(): Variables will be stacked one on top of the other
Text : is a read-only area of memory where the compiled machine code of the program is stored.
Pre-processor directives are instructions that are executed by the pre-processor before the actual compilation of the code begins. These directives are commands that tell the compiler to preprocess the information before the code is compiled into machine language. Preprocessor directives are typically used to include external libraries, define constants, or perform conditional compilation, among other tasks. Pre-processor directives are identified by the "#" symbol at the beginning of the code, and they do not end with a semicolon. These commands are handled before the actual code is compiled and are not part of the C++ language itself—they are instructions to the preprocessor.
C++ is supported by a variety of ready-to-use functionalities called libraries: a set of tools and functionalities that were already written and they are ready to be recalled and used. This process have to be done before the compiling process starts. For this reason, exist a specific preprocessor directive used for this purpose. When you use "#include", the preprocessor literally copies the entire content of the specified file or library into your program at the point where the #include directive appears. This makes the specified content available for use in your code. Standard C++ libraries can be imported with: #include < LIBRARY_NAME> Example: #include // Includes the standard input/output library int main() { return 0; } In this example, the #include directive imports the input/output stream library
Streams in C++ are buffered, meaning that data sent through cout doesn’t always appear on the screen immediately. Instead, it is stored in a buffer and then flushed (i.e. transferred to the console) when the buffer is full or when a special command is encountered:
cin: The cin object is the standard input stream in C++. It is used to read input from the user. The name cin stands for "character input," and like cout, it is part of the std namespace. std::cin >> VARIABLE ; where:
To access entities within the standard namespace, you have to specify the "std" prefix: std:: ELEMENT We can bring the entire namespace into scope with: using namespace std; For example, we can avoid to use every time the standard prefix "std::": #include using namespace std; int main() { int age; cout << "Enter your age: "; cin >> age; cout << "You entered: " << age << endl; return 0; }
In C++, a qualifier is a keyword or modifier that is used to change the meaning or behavior of a variable, function, or type. Qualifiers provide additional information about how data can be accessed, modified, or used. The general declaration structure became: QUALIFIERS MODIFIER DATA_TYPE VARIABLE_NAME ; There are different kinds of qualifiers in C++ that serve specific purposes. They will be showed at the right moment.
In C++ we can declare a constant variable, meaning that once a value is assigned, it cannot be changed. This is especially useful when you want to ensure certain values remain fixed, helping prevent accidental modifications. This can be accomplished in two ways: