Intermediate
Your First Program (Cin and Cout)
This program starts by including the iostream library, which allows for input and output operations (e.g. printing to the console). It then defines the main function, which is the entry point for all C++ programs.
Inside the main function, the program prints the string "Hello, World!" to the console using the cout stream and the << operator.
The program then declares some variables of different types: an integer a, a float b, and a character c. It assigns them some values and prints them to the console using the cout stream and the << operator.
Inside the main function, the program prints the string "Hello, World!" to the console using the cout stream and the << operator.
The program then declares some variables of different types: an integer a, a float b, and a character c. It assigns them some values and prints them to the console using the cout stream and the << operator.
Loops
The for loop executes a block of code a fixed number of times. It has three parts: an initialization (int i = 0), a condition (i < 10), and an iteration statement (i++). The initialization is executed only once, before the loop starts. The condition is checked before each iteration of the loop. If the condition is true, the loop continues. If the condition is false, the loop terminates. The iteration statement is executed at the end of each iteration of the loop. In this example, the loop prints the values of the i variable from 0 to 9 (inclusive).
The while loop also executes a block of code a fixed number of times. It has a single condition (j < 10). The condition is checked before each iteration of the loop. If the condition is true, the loop continues. If the condition is false, the loop terminates. In this example, the loop prints the values of the j variable from 0 to 9 (inclusive). The do-while loop is similar to the while loop, but the condition is checked at the end of each iteration instead of at the beginning. This means that the loop will always execute at least once. In this example, the loop prints the values of the k variable from 0 to 9 (inclusive). |
Functions
This program declares an array of integers called numbers with 5 elements. It initializes the elements of the array with the values 1, 2, 3, 4, and 5. The program then uses a for loop to print the elements of the array to the console. Next, the program modifies the second element of the array (index 1) and assigns it the value 10.
Finally, the program uses another for loop to print the modified array to the console.
Finally, the program uses another for loop to print the modified array to the console.
Functions
This program defines a function called add that takes two integers as arguments (x and y) and returns their sum. The function prototype (declaration) is at the top of the program, before the main function. It specifies the function's return type (int), name (add), and parameters (int x, int y). This allows the function to be called from the main function before it is actually defined.
The function definition (implementation) is at the bottom of the program. It includes the function's return type, name, and parameters, as well as a block of code that specifies what the function does. In this case, the function simply adds x and y and returns the result.
Inside the main function, the program declares two variables a and b, and assigns them the values 10 and 20, respectively. It then calls the add function, passing a and b as arguments, and stores the result in a variable c. Finally, it prints the value of c to the console.
The function definition (implementation) is at the bottom of the program. It includes the function's return type, name, and parameters, as well as a block of code that specifies what the function does. In this case, the function simply adds x and y and returns the result.
Inside the main function, the program declares two variables a and b, and assigns them the values 10 and 20, respectively. It then calls the add function, passing a and b as arguments, and stores the result in a variable c. Finally, it prints the value of c to the console.
Structures (Structs)
This program defines a structure called Student, which has three fields: a name field of type std::string, an age field of type int, and a GPA field of type float. The main function then creates a Student object called s and initializes its fields with the values "Alice", 20, and 3.8, respectively. Finally, the program prints the student's information to the console.
Classes
This program defines a class called Student, which has three private fields: a name field of type std::string, an age field of type int, and a GPA field of type float. The class also has a constructor, which is a special member function that is called when an object of the class is created. The constructor initializes the fields of the object with the values passed as arguments.
The class also has three public member functions (also known as methods) called getName, getAge, and getGPA, which return the values of the name, age, and GPA fields, respectively. The main function creates a Student object called s using the constructor, and passes the values "Alice", 20, and 3.8 as arguments. Finally, the program prints the student's information to the console using the public member functions.
The class also has three public member functions (also known as methods) called getName, getAge, and getGPA, which return the values of the name, age, and GPA fields, respectively. The main function creates a Student object called s using the constructor, and passes the values "Alice", 20, and 3.8 as arguments. Finally, the program prints the student's information to the console using the public member functions.
Pointers
This program declares an integer variable x and assigns it the value 10. It then declares a pointer p to an integer, and assigns the address of x to p using the & operator. The & operator (also known as the "address-of" operator) returns the memory address of a variable. For example, in this case, &x returns the memory address of the x variable. The * operator (also known as the "dereference" operator) retrieves the value stored at a memory address. For example, in this case, *p retrieves the value stored at the memory address pointed to by p.
The program then prints the value of x, the address of x (using &x), the value of p, and the value pointed to by p (using *p).
The program then prints the value of x, the address of x (using &x), the value of p, and the value pointed to by p (using *p).