-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp_concepts.txt
137 lines (101 loc) · 6.5 KB
/
cpp_concepts.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
C++
an insult to the human brain
##################################################################################################################
Namespaces:
- Namespaces solve problems with functions having the same, for example:
You write a function called xyz() and you use a library that has xyz() declared
The compiler has no way of knowing which version you are referring to
Classes:
- A class is a blueprint for an object, which has attributes and methods
Constructors:
- Whenever an Object of a Class is created, its constructor is called
It's parent class constructors are also called! Parent constructors are called before the child
- By default the constructors invoked are the "no-argument" constructors
- If the parent class needs an argument for its constructor, you can use initialization lists
Initialization lists:
- These are useful for:
- Passing arguments to constructors of parent Classes
- Initializing fields of Classes with default values
- It is necessary to initialize const fields
Member functions (methods):
- Functions which are declared inside a class which executes on the class
Static variables inside classes:
- Static variables inside classes are variables that are initialized once per class
they can then be accessed by every instance of that class
Static methods inside classes:
- Static methods inside classes are methods that can access static variables
Const methods inside classes:
- Basically means a method should not be changing the state of an object
Inheritance:
- Why? To reuse functionality and a fast implementation time, it's also easier to create and maintain
- The existing class is the base class, and the new class the derived class
- The logic is as follows: the derived class is a existing class (IS-A logic)
Polymorphism:
- A class that declares or inherits a virtual function is called a polymorphic class.
- There are two types of Polymorphism:
1. Compile time Polymorphism
- Function overloading
- Operator overloading
2. Runtime Polymorphism
- Function overriding (virtual keyword)
Abstract Classes & Templates:
- An abstract class is an a class that has uninitialized methods or attributes
- This class can be used as a base class
- An Template is basically an (pure) abstract class, a class that has no data or concrete member functions
Exception Classes:
- An exception s a problem that arises during the execution of a program, it's a response to something execeptional
- It uses the following three keywords: throw, catch and try
- A program throws an exception, using the throw keyword
- A program catches the exception and handles it with a handler, using the catch keyword
- A try block identifies a block of code where exceptions are activated
Stdio streams:
- Streams are an abstraction that represent a device on which input and output operations are performed
A stream basically represents a source or destination of characters of indefinite length
C++ provides a standard `iostream` library, which you can use to operate with streams
References vs Pointers:
- Pointers are variables which hold a memory address of another variable
- References are aliases for other variables
- References cannot be reassigned
- Pointers can be NULL, references cannot
Shallow vs Deep Copy:
- If variables of an object are dynamically allocated, then it is required to do a Deep Copy in order to create a replica of the object
- Why? If dynamically allocated memory is copied through the shallow way, then the memory address is copied
thus objects contain the same memory address
this keyword in methods
##################################################################################################################
Sources:
- https://www.cprogramming.com
##################################################################################################################
C++ Google Style Guide
##################################################################################################################
clang-format -style=file ${FILE_NAME} or clang-format -style=google ${FILE_NAME}
Classes:
- Declaration Order:
- Types and type aliases (typedef, using, enum, nested structs and classes)
- Static constants
- Factory functions
- Constructors and assignment operators
- Destructor
- All other functions (static and non-static member functions, and friend functions)
- Data members (static and non-static)
Functions:
- Write small and focuses functions
- When a function exceeds 40 lines, think of breaking it up into smaller pieces
- Prefer using return values over output parameters: they improve readability and often provide same or better performance
- Prefer using return by value, failing that, return by reference. Avoid returning a pointer unless it can be NULL
##################################################################################################################
Sources:
- https://google.github.io/styleguide/cppguide.html
##################################################################################################################
Floating Point Numbers
##################################################################################################################
4-byte floating point numbers can hold fewer distinct values than 4-byte integers. This is because the internal representation
of floating point numbers is more complicated than integers.
How floating points are represented (IEEE-754 standard):
- first bit is sign telling whether the number is positive or negative
- second byte is the exponent of the number
- third and fourth bytes are the mantissa (or significant)
- 2 ^ exponent
##################################################################################################################
Pro tips:
- read declaration backwards ( https://stackoverflow.com/a/162615/14639673 )