📜  C++ CHEAT SHEAT - C++ (1)

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

C++ Cheat Sheet

Introduction

This cheat sheet will provide you with the most important concepts and syntax of C++ programming language. It is a quick reference guide for beginner and intermediate level programmers who need to quickly check the syntax or concepts.

Variables and Data Types
Variables

A variable is a container that stores a value. It can be of different data types such as int, char, float, double, etc.

// Variable declaration 
int age;
float weight;
char initial;
Data Types

Following are the data types in C++:

  • int: Used to store integers.
  • float: Used to store floating-point numbers.
  • double: Used to store large floating-point numbers.
  • char: Used to store single character.
  • bool : Used to store true/false values.
// Data Type declaration 
int num1 = 10;
float num2 = 3.14;
double num3 = 3.141592653589793;
char letter = 'A';
bool flag = true;
Control Structures
Conditional Statements

Conditional statements are used to execute a block of code based on some condition.

// If-else statement 
if(condition) {
    // Code to be executed if condition is true
} else {
    // Code to be executed if condition is false
}
Loops

Loops are used to execute a block of code repeatedly until a certain condition is met.

// For loop 
for(initialization; condition; increment) {
    // Code to be executed 
}

// While loop 
while(condition) {
    // Code to be executed 
}

// Do-While loop 
do {
    // Code to be executed 
} while(condition)
Functions

Functions are a block of code that performs a specific task. It can be called multiple times from different parts of the program.

// Function Declaration 
return_type function_name(parameters)
{
    // Code to be executed 
}

// Function Call 
function_name(argument_values)
Arrays and Strings
Arrays

An array is a collection of similar data types. It is a fixed-size container that stores multiple values of the same data type.

// Array declaration 
data_type array_name[size];

// Example 
int numbers[5] = {1, 2, 3, 4, 5};
Strings

A string is a collection of characters. It is actually an array of characters terminated by a NULL character (\0).

// String declaration 
string str = "Hello World";
Pointers

A pointer is a variable that stores the memory address of another variable. It is used to access and manipulate the value of the variable indirectly.

// Pointer declaration 
data_type *var_name;

// Example 
int num = 10;
int *ptr = #

// Dereferencing 
cout << *ptr << endl;
// Output: 10
Classes and Objects

A class is a user-defined data type that encapsulates data and functions together. An object is an instance of a class.

// Class declaration 
class class_name
{
    access_specifier:
        data_type data_member1;
        data_type data_member2;
        function_type function_name();
}

// Object creation
class_name obj;
Input and Output
Input
// Input 
cin >> variable_name;
Output
// Output 
cout << "Text/Value" << endl;
Conclusion

This cheat sheet covers the most important concepts and syntax of C++ programming language. It can serve as a quick reference guide for beginner and intermediate level programmers.