Notes

Introduction to C++ Programming

  • C++ is a fast language commonly used in:
    • Advanced graphics applications (e.g., Adobe software, video editing tools)
    • Embedded systems
    • Video game development
  • C++ is a middle-level language:
    • Middle level means it is faster than higher-level languages like Python, Java, and C#, but it’s not as low-level as assembly.
    • Higher-level languages are easier to understand but slower; C++ offers a balance between human-readable syntax and hardware efficiency.

Why Learn C++?

  • High demand for C++ programmers:
    • Example: Average salary for a C++ software engineer is around $124,000.
  • Learning curve: While C++ has a steeper learning curve, it’s worth the effort.
    • You won’t get a job from just watching a tutorial, but practicing and building a portfolio is essential.

Tools Needed:

  1. Text Editor (IDE):
    • VS Code, Code Blocks, or even Notepad.
    • VS Code and Code Blocks are Integrated Development Environments (IDEs) that also offer additional developer tools.
  2. Compiler:
    • A compiler converts source code into machine instructions.
    • Windows/Linux: Use GCC (GNU Compiler Collection).
    • Mac: Use Clang.

Steps for Setting Up:

  1. Download VS Code:
    • Go to code.visualstudio.com and download the appropriate version for your OS.
    • Install VS Code and launch it.
  2. Install C++ Extensions in VS Code:
    • C++ Extension: Allows coding support for C++.
    • Code Runner Extension: Allows running the code directly from VS Code.
  3. Setting Up Compiler:
    • Linux: Install GCC with the command gcc -v (if it’s not installed, use the command to install it).
    • Mac: Install Clang using terminal commands.
    • Windows:
      • Install MinGW64 from the provided link.
      • Use commands in the terminal to install the necessary compilers and tools (e.g., pacman -S mingw-w64).
      • Add the compiler to Windows environment variables.

Writing Your First C++ Program:

1. Create a new file:

  • The first thing you’ll do is create a new C++ source code file. This file will have the .cpp extension, which indicates that it’s a C++ program.
  • You can name the file something like hello_world.cpp, as it’s a common convention to name your first program “Hello World” since it simply prints a message to the console.

2. Writing Your Code:

a. Include the necessary header file:

  • The first line of the program:
#include <iostream>
 
  • This line tells the compiler to include the iostream library, which is a part of the standard C++ library. The iostream library allows you to perform input and output operations, such as printing text to the screen or reading user input.
    • #include is a preprocessor directive that tells the compiler to include a file before the compilation starts. The iostream file is one of the most basic libraries in C++.
    • iostream stands for input/output stream, which is used for handling input and output in C++.

b. Defining the main function:

  • Next, you write the main function:
int main() {
    // Your code goes here
    return 0;
}
 
  • int main(): This is where the execution of your C++ program begins. In C++, every program must have a main function. When you run your program, the program starts executing from the main function.
    • int: This indicates that the function will return an integer value. The main function traditionally returns 0 to signify successful execution.
    • {}: Curly braces {} define the body of the function. Any code you want to run when the program starts should go inside these braces.

c. Printing output to the console:

  • Inside the main function, the first operation you’re performing is printing output:
std::cout << "I like pizza" << std::endl;
 
  • std::cout: This is used to display output to the console. It stands for standard character output.
  • <<: This is called the stream insertion operator. It takes whatever is on the right side of it (in this case, the string "I like pizza") and sends it to the output stream (the console in this case).
  • “I like pizza”: This is the string that will be printed to the console. Strings are placed inside double quotes (").
  • std::endl: This is used to insert a newline at the end of the output. It stands for end of line and ensures the cursor moves to the next line after the output. Additionally, std::endl flushes the output buffer, which means it forces the program to print the text immediately to the console rather than keeping it in memory.

Note: Instead of std::endl, you can use "\\n" to create a new line:

std::cout << "I like pizza\\n";
 

But using std::endl is often preferred because it ensures the output is immediately printed, though it’s slightly less efficient.

d. Return Statement:

  • At the end of the main function, you return 0:
return 0;
 
  • The return 0; statement is important because it signifies that the program has completed successfully. If the program encounters any errors or doesn’t execute properly, it would return a non-zero value (usually 1 or another error code). The return 0; is simply a convention that indicates “success.”

3. Handling Output with Multiple Lines:

a. Printing multiple lines:

  • If you want to print more than one line, you can add another std::cout statement:
std::cout << "It's really good" << std::endl;
 
  • This will print “It’s really good” on a new line, immediately after “I like pizza”.

b. Endline vs. Newline character:

  • To make a new line in the output, you can either use std::endl or the newline escape character \n.
  • Example using \n:
std::cout << "I like pizza\n";
std::cout << "It's really good\n";
 
  • The \n character will create a new line but is considered faster in terms of performance compared to std::endl because it doesn’t flush the output buffer.

4. Adding Comments:

  • Single-line comments are used for adding notes in your code. These comments are not executed and are simply there for the developer to understand the code.
// This is a single-line comment
 
  • Multi-line comments can be used when you have a larger block of text or notes:
/* This is a
   multi-line comment */
 
  • These comments help you (and others) understand the code, but they are ignored by the compiler.

5. Complete Example Code:

Putting everything together, here’s a simple C++ program that prints two lines of text, includes comments, and explains what’s happening:

#include <iostream> // Including the iostream library for output
 
int main() {
    // This is a single-line comment explaining the next line of code
    std::cout << "I like pizza" << std::endl;  // Print the first line of text
    std::cout << "It's really good" << std::endl;  // Print the second line of text
 
    // End of the main function
    return 0; // Return 0 to indicate that the program executed successfully
}
 
  • What this code does:
    • It includes the iostream library for input/output operations.
    • In the main() function, it prints two lines of text: “I like pizza” and “It’s really good”, each on its own line.
    • It ends the program with a return 0; to indicate success.

Summary of Key Concepts:

  • #include <iostream>: Includes the input/output library.
  • std::cout: Outputs text to the console.
  • <<: Used to insert data into the output stream.
  • std::endl: Inserts a newline and flushes the output buffer.
  • return 0;: Returns a value to the operating system indicating that the program ran successfully.
  • Comments: Helps developers understand the code, ignored by the compiler.

References