Comprehensive C++ Language Cheatsheet for Beginners
Comprehensive C++ Language Cheatsheet for Beginners
This cheatsheet provides a concise overview of C++ programming concepts, syntax, and key features, serving as a handy reference for beginners.
1. Basic Syntax
- Comments:
- Single line:
// This is a comment
- Multi-line:
/* This is a comment */
- Single line:
Program Structure:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
2. Data Types
- Primitive Data Types:
int
: Integer (e.g.,int x = 5;
)float
: Floating-point number (e.g.,float y = 3.14;
)double
: Double precision floating-point (e.g.,double z = 2.718;
)char
: Character (e.g.,char c = 'a';
)bool
: Boolean (true or false) (e.g.,bool flag = true;
)
3. Variables
Declaration and Initialization:
int age = 25; // Declaration and initialization
4. Operators
- Arithmetic Operators:
+
,-
,*
,/
,%
- Relational Operators:
==
,!=
,<
,>
,<=
,>=
- Logical Operators:
&&
,||
,!
5. Control Structures
- Loops:
While Loop:
while (condition) {
// code
}
For Loop:
for (int i = 0; i < 10; i++) {
// code
}
Conditional Statements:
if (condition) {
// code
} else {
// code
}
6. Functions
Example:
int add(int a, int b) {
return a + b;
}
Function Declaration:
returnType functionName(parameters) {
// code
}
7. Arrays
Initialization:
int numbers[5] = {1, 2, 3, 4, 5};
Declaration:
int numbers[5]; // Array of integers
8. Pointers
Example:
int x = 10;
ptr = &x; // ptr now holds the address of x
Declaration:
int* ptr; // Pointer to int
9. Classes and Objects
Object Creation:
MyClass myObj; // Create an object of MyClass
myObj.myNumber = 5;
Class Declaration:
class MyClass {
public:
int myNumber;
void myFunction() {
// code
}
};
10. Standard Library
Including Libraries:
#include <vector> // For using vectors
#include <string> // For using strings
Conclusion
This cheatsheet covers the fundamental aspects of C++ programming. It serves as a quick reference for syntax and basic concepts, making it easier for beginners to understand and write C++ code effectively.