📜  c++ 代码 - C++ (1)

📅  最后修改于: 2023-12-03 15:13:59.159000             🧑  作者: Mango

C++ Code

C++ is a versatile programming language that is commonly used for developing software applications. It features object-oriented programming concepts, templates, and standard libraries for efficient code.

Hello World Example

Here's a simple program that prints "Hello, World!" to the console:

#include <iostream>

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

The #include <iostream> statement adds the input/output library to the program, enabling the use of cout to output text. The int main() function is a required function that is executed first in the program. Finally, return 0 indicates that the program executed successfully.

Variables and Data Types

Variables are used to store values in C++. Here are some data types:

| Data Type | Description | | --- | --- | | int | integer | | double | floating point number | | bool | boolean value (true or false) | | char | character |

Here's an example of how variables can be used:

#include <iostream>

int main()
{
    int age = 25;
    double height = 1.75;
    bool isMarried = false;
    char gender = 'M';

    std::cout << "Age: " << age << std::endl;
    std::cout << "Height: " << height << "m" << std::endl;
    std::cout << "Married? " << isMarried << std::endl;
    std::cout << "Gender: " << gender << std::endl;

    return 0;
}

The output will be:

Age: 25
Height: 1.75m
Married? 0
Gender: M
Conditional Statements

Conditional statements are used to execute different actions based on different conditions. Here's an example:

#include <iostream>

int main()
{
    int age = 25;

    if (age >= 18)
    {
        std::cout << "You are an adult." << std::endl;
    }
    else
    {
        std::cout << "You are a minor." << std::endl;
    }

    return 0;
}

The output will be:

You are an adult.
Loops

Loops are used to execute a block of code repeatedly. Here are two different types of loops in C++:

for loop

The for loop is used to execute a block of code a specific number of times.

#include <iostream>

int main()
{
    for (int i = 1; i <= 5; i++)
    {
        std::cout << i << std::endl;
    }

    return 0;
}

The output will be:

1
2
3
4
5
while loop

The while loop is used to execute a block of code as long as a condition is true.

#include <iostream>

int main()
{
    int i = 1;

    while (i <= 5)
    {
        std::cout << i << std::endl;
        i++;
    }

    return 0;
}

The output will be the same as the for loop example.

Functions

Functions are used to divide a program into smaller, more manageable pieces. Here's an example:

#include <iostream>

void printMessage()
{
    std::cout << "Hello, World!" << std::endl;
}

int main()
{
    printMessage();

    return 0;
}

The printMessage() function is defined outside of main() and is used to output text to the console.

Conclusion

C++ is a powerful programming language with many features that make it a popular choice for developing software. From basic input/output to more advanced concepts like functions and object-oriented programming, the possibilities are endless.