Structure of a C++ Program:
```cpp
// Preprocessor Directive
#include <iostream>
// Namespace Declaration
using namespace std;
// Main Function
int main() {
// Statements
cout << "Hello, World!"; // Example statement
return 0;
}
```
Basic Commands in C++:
1. Comments:
- Single-line: `// This is a single-line comment`
- Multi-line:
```cpp
/*
This is a
multi-line comment
*/
```
2. Input/Output:
- Output: `cout << "Hello, World!";`
- Input: `cin >> variable;`
3. Variables and Data Types:
- Variable Declaration: `int num;`
- Data Types: `int`, `float`, `char`, `bool`, etc.
4. Operators:
- Arithmetic: `+`, `-`, `*`, `/`, `%`
- Relational: `==`, `!=`, `<`, `>`, `<=`, `>=`
- Logical: `&&`, `||`, `!`
- Assignment: `=`, `+=`, `-=`, `*=`, `/=`
5. Control Structures:
- `if`, `else if`, `else`
- `switch`, `case`, `break`
- `for`, `while`, `do while`
- `break`, `continue`
6. Functions:
- Function Declaration: `int add(int a, int b) { return a + b; }`
- Function Call: `int result = add(5, 3);`
7. Arrays:
- Declaration: `int arr[5];`
- Initialization: `int arr[] = {1, 2, 3};`
- Accessing: `arr[index];`
8. Strings:
- Declaration: `string str = "Hello";`
- Concatenation: `str1 + str2;`
- Length: `str.length();`
9. Pointers:
- Declaration: `int *ptr;`
- Address-of (`&`) and Dereference (`*`) operators
- Dynamic memory allocation: `ptr = new int;`
10. Classes and Objects:
- Class Declaration:
```cpp
class MyClass {
// Class members
};
```
- Object Creation: `MyClass obj;`
11. File Handling:
- `ifstream`, `ofstream`, `fstream`
- Opening, reading, writing, and closing files
12. Exception Handling:
- `try`, `catch`, `throw`, `finally`
13. Namespaces:
- `using namespace std;`
14. Standard Library Functions:
- `cin`, `cout`
- `getline()`, `stoi()`, `to_string()`
- `sort()`, `find()`, `erase()`, `push_back()`
These basic commands cover fundamental aspects of C++ programming. Experimenting with these commands and concepts will aid in understanding and mastering the language.
0 Comments