Cplusplus || GeeksForGeeks
1. Low level Programming
2. Speed of Execution
3. Derived from C
4. Richer Library than C
5. Object Oriented Programming
```
#include<iostream>
using namespace std;
int main(){
cout<<"Hello World"; //Insertion Operator
return 0;
}
```
1. cout -> std O/P stream.
2. cin -> std I/P stream.
3. cerr -> std unbuffered error stream.
4. clog -> std buffered I/P stream.
5. endl -> New line => flushed the buffer while '\n' simply puts a new line.
1. Syntax Errors
2. Semantic Errors
3. Linker Errors
4. Runtime Errors
5. Logical Errors
1. Arithmetic Operator
2. Relational Operator
3. Logical Operator
4. Assignment Operator
1. To do something repeatedly.
2. To iterate through containers like arrays, list, etc.
3. To run something forever like wen servers or other system.
Iterating through an array, list or any other containers, we generally prefer for loop.
1. Complex Logic Implementation(easily possible).
2. Web servers.
3. Game (Something coming Infinitely).
1. break
2. continue
3. return
4. goto
1. Avoid code Redundary.
2. Make code modular.
3. Abstraction (for e.g. In Library functions, we do not have to worry about how they work).
1. Inline function.
2. Function Overloading.
3. Default Argument.
1. Random Access.
2. Cache Friendliness.
We need to know the size before we create them. So, in CPP we use Vector from STL Library.
& when used before a variable name (while not declaring it) given address of the variable.
int main(){
int x = 10;
cout<<(&x);
return 0;
}
| Operator | Precedence | Address |
| :———: | :————: | :——-: |
| x | 10 | 0x1234 |
*(Dereference) When used before an address (or address variable, i.e, pointer) gives the value of the address.
int main(){
int x = 10;
cout<<(*(&x));
return 0;
}
SYNTAX: int ptr; or int ptr; //Creating Pointer
int main(){
int x = 10, int* ptr = &x;
cout<<(*ptr)<<" ";
cout<<ptr<<" ";
return 0;
}
| Variable | Value | Address |
| :———: | :——: | :——-: |
| ptr | 0x1234 | 0x1238 |
1. Dynamic Memory Allocation.
2. Implementation of Data Structure like Linked List, Tree, BST, etc.
3. To do System Level Programming.
4. To return multiple values.
```
try{
//The code that may throw exception.
}
```
throw: used to throw exception.
catch: one or more catch blocks are used to handle the exception.
Stack Unwinding is a concept which says that a functions through an exception and if this function doesn’t handle the exception then the control goes to the caller and if the caller also doesn’t handle the exception then control goes to the caller of the caller and we keep on searching the handler for the exception in the function called stack until we find the handler and once we find the handler and once we find the handler, the handler get controlled and then out program continue after the handler.
All Standard Library Exceptions like bad.alloc(thrown by new operator which there is any error during allocating Dynamic Memory), bad.cast, etc. inbuilt from exception class directly or indirectly.
Memory leak! If we dynamically allocate memory and we don’t deallocate the memory compiler doesn’t give any error or warning and as a programmer its our responsibilty to insure that whatever dynamically allocated memory we have we deallocate it. If we forget to deallocate the memory then our program has memory leak program. And this became very big problem like given below.
void fun(){
int *ptr = new
}
int main()
{
while(true)
{
fun();
}
}
Smart Pointers try to solve this type of memory leak issue by wrapping a given pointer to a class object.