All C++ NOTES
Comprehensive C++ Notes
1. Introduction to C++
-
C++ is a general-purpose programming language created by Bjarne Stroustrup in 1979 as an extension of C.
-
It supports procedural, object-oriented, and generic programming paradigms.
-
Commonly used for system/software development, game programming, embedded systems, and more.
2. Basic Syntax
-
#include <iostream>: Includes input/output stream library. -
using namespace std;: Avoids prefixingstd::before cout, cin, etc. -
int main(): Entry point of C++ program. -
cout: Standard output stream. -
endl: End line (newline) character.
3. Data Types
| Type | Description | Size (typical) |
|---|---|---|
| int | Integer | 4 bytes |
| float | Floating point number | 4 bytes |
| double | Double precision float | 8 bytes |
| char | Single character | 1 byte |
| bool | Boolean (true or false) | 1 byte |
| void | Represents no value | N/A |
4. Variables and Constants
-
Variables hold data, constants are fixed values.
-
Use
constkeyword for constants.
5. Operators
-
Arithmetic:
+,-,*,/,% -
Assignment:
=,+=,-=,*=,/= -
Comparison:
==,!=,>,<,>=,<= -
Logical:
&&(AND),||(OR),!(NOT) -
Increment/Decrement:
++,--
6. Control Structures
6.1 Conditional Statements
6.2 Switch Statement
6.3 Loops
-
For loop:
-
While loop:
-
Do-while loop:
7. Functions
-
Functions encapsulate reusable code.
-
Can have return types, parameters, and default values.
8. Arrays and Strings
8.1 Arrays
-
Fixed size, stores multiple elements of same type.
8.2 Strings
-
Using C-style:
-
Using C++
stringclass:
9. Pointers
-
Pointer stores memory address.
-
Use
&to get address,*to dereference.
10. References
-
Reference is an alias for a variable.
-
Must be initialized when declared.
11. Structures (struct)
-
Group related variables under one name.
12. Object-Oriented Programming (OOP)
12.1 Classes and Objects
-
Class: blueprint for objects.
-
Object: instance of a class.
12.2 Access Modifiers
| Modifier | Access Level |
|---|---|
public | Accessible anywhere |
private | Accessible only inside the class |
protected | Accessible in class and subclasses |
12.3 Constructor and Destructor
12.4 Inheritance
13. Templates (Generic Programming)
-
Allows functions/classes to work with any data type.
14. Exception Handling
-
Use
try,catch, andthrowfor error handling.
15. Standard Template Library (STL)
-
Containers:
vector,list,map,set, etc. -
Algorithms: sort, search, etc.
-
Iterators: used to traverse containers.
Example with vector:
16. Input and Output
-
Input:
cin -
Output:
cout
Example:
17. File I/O
18. Useful Keywords
| Keyword | Purpose |
|---|---|
static | Shared variable/function, persists |
const | Constant value |
volatile | Variable can be changed unexpectedly |
mutable | Allows modification in const object |
friend | Access private members |
namespace | Organizes code into logical groups |
19. Common Best Practices
-
Use meaningful variable names
-
Keep functions small and focused
-
Comment your code for clarity
-
Avoid using
using namespace std;in headers -
Use
nullptrinstead ofNULL -
Prefer
std::vectorover raw arrays for safety
Comments
Post a Comment