C++ Crash Course

梦里花落 2019-07-28 ⋅ 15 阅读

C++ is a general-purpose programming language that is widely used in various applications, ranging from game development to operating systems. If you are new to C++ or programming in general, this crash course will provide you with the basics to get started.

1. Hello World!

Every programming language tutorial starts with a simple "Hello World" program, and C++ is no exception. Here's how you can print "Hello World" to the console in C++:

#include <iostream>

int main() {
    std::cout << "Hello World!" << std::endl;
    return 0;
}

Explanation:

  • #include <iostream>: This line includes the necessary library to work with input/output streams.
  • int main() { ... }: This is the main function of your program, where execution begins.
  • std::cout: cout is the standard output stream object, and std:: is the namespace that contains it.
  • <<: The << operator is used to stream or output data. Here, we are streaming the "Hello World!" string.
  • std::endl: endl is used to insert a new line after printing "Hello World!".
  • return 0;: This statement indicates the end of the main function and returns 0, indicating successful termination of the program.

2. Variables and Data Types

In C++, you need to declare variables before using them. Here are some common data types in C++:

  • int: Used to store integers.
  • float: Used to store floating-point numbers.
  • double: Similar to float, but with higher precision.
  • char: Used to store single characters.
  • bool: Used to store true or false values.
#include <iostream>

int main() {
    int age = 25;
    float height = 1.75;
    char grade = 'A';
    bool isPassing = true;

    std::cout << "Age: " << age << std::endl;
    std::cout << "Height: " << height << std::endl;
    std::cout << "Grade: " << grade << std::endl;
    std::cout << "Passing: " << isPassing << std::endl;

    return 0;
}

Explanation:

  • int age = 25;: declares an integer variable named age and assigns it the value 25.
  • float height = 1.75;: declares a float variable named height and assigns it the value 1.75.
  • char grade = 'A';: declares a char variable named grade and assigns it the value 'A'.
  • bool isPassing = true;: declares a bool variable named isPassing and assigns it the value true.
  • The data is printed to the console using the << operator.

3. Control Flow

C++ provides various control flow statements to manipulate the execution of a program. Here are a few examples:

  • if statement: Executes a block of code if a specified condition is true.
int number = 10;
if (number > 5) {
    std::cout << "The number is greater than 5." << std::endl;
}
  • for loop: Repeats a block of code a specified number of times.
for (int i = 0; i < 5; i++) {
    std::cout << "Iteration " << i << std::endl;
}
  • while loop: Repeats a block of code while a specified condition is true.
int count = 0;
while (count < 5) {
    std::cout << "Count: " << count << std::endl;
    count++;
}

These are just a few examples, but there are more control flow statements available in C++.

4. Functions

Functions are reusable blocks of code that perform specific tasks. Here's an example of a function that adds two numbers:

#include <iostream>

int add(int num1, int num2) {
    return num1 + num2;
}

int main() {
    int result = add(5, 3);
    std::cout << "Result: " << result << std::endl;
    return 0;
}

Explanation:

  • int add(int num1, int num2) { ... }: defines a function named add that takes two integer parameters num1 and num2, and returns their sum.
  • int result = add(5, 3);: calls the add function with arguments 5 and 3, and assigns the result to the result variable.
  • The result is then printed to the console.

Conclusion

This crash course covered some of the basics of C++ programming, including printing to the console, working with variables and data types, using control flow statements, and creating functions. These concepts serve as a solid foundation for further exploring the world of C++ programming. Happy coding!


全部评论: 0

    我有话说: