-
Notifications
You must be signed in to change notification settings - Fork 0
/
panini.notes
275 lines (201 loc) · 11 KB
/
panini.notes
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
== codam notes ==
only funtcion templates in the header and function
implemantation in a seperate .cpp file just like in
the account exercise.
NEW OPERATOR
The new operator is an operator which denotes a request
for memory allocation on the Heap. The constructor of the
class is invoked to properly initialize this memory.
For dynamic initialization new keyword require non parameterized
constructor if we add a parameterized constructor. So we will
use a dummy constructor for it
FUNCTION POINTER
handy website
http://www.newty.de/fpt/fpt.html#defi
- One indirect function call is more expensive than one if condition.
- Several if conditions are more expensive than an indirect function call.
== allowed headers ==
https://en.cppreference.com/w/cpp/header
== Fixed point number stuffs ==
https://stackoverflow.com/questions/19550030/how-to-overload-operator-without-friend-function
https://en.cppreference.com/w/cpp/language/operators
https://stackoverflow.com/questions/79677/whats-the-best-way-to-do-fixed-point-math
https://embeddedartistry.com/blog/2018/07/12/simple-fixed-point-conversion-in-c/
Converting from fixed-point to floating-point is straightforward.
We take the input value and divide it by (2fractional_bits),
putting the result into a double:
inline double fixed_to_double(fixed_point_t input)
{
return ((double)input / (double)(1 << FIXED_POINT_FRACTIONAL_BITS));
}
To convert from floating-point to fixed-point, we follow this algorithm:
Calculate x = floating_input * 2^(fractional_bits)
Round x to the nearest whole number (e.g. round(x))
Store the rounded x in an integer container
Using the algorithm above, we would implement our float-to-fixed
conversion as follows:
inline fixed_point_t double_to_fixed(double input)
{
return (fixed_point_t)(round(input * (1 << FIXED_POINT_FRACTIONAL_BITS)));
}
== NAMESPACES ==
We can define functions and variables with the same name thanks to namespaces.
namespace owo{
int panini = 1;
}
namespace uwu{
int panini = 5;
}
We can just acces them like this (with the scope resolution operator):
uwu::panini
owo::panini
== Shadowing ==
Each block defines its own scope region. So what happens when we have a variable
inside a nested block that has the same name as a variable in an outer block? When
this happens, the nested variable “hides” the outer variable in areas where they
are both in scope. This is called name hiding or shadowing.
== -Wshadow ==
Warn whenever a local variable or type declaration shadows another variable, parameter,
type, class member (in C++), or instance variable (in Objective-C) or whenever a
built-in function is shadowed. Note that in C++, the compiler warns if a local variable
shadows an explicit typedef, but not if it shadows a struct/class/enum. If this warning
is enabled, it includes also all instances of local shadowing. This means that
-Wno-shadow=local and -Wno-shadow=compatible-local are ignored when -Wshadow is used.
Same as -Wshadow=global.
== Inheritance in C++ ==
The capability of a class to derive properties and characteristics from another
class is called Inheritance. Inheritance is one of the most important features
of Object-Oriented Programming.
Inheritance is a feature or a process in which, new classes are created from the
existing classes. The new class created is called “derived class” or “child class”
and the existing class is known as the “base class” or “parent class”.
When we say derived class inherits the base class, it means, the derived class
inherits all the properties of the base class, without changing the properties
of base class and may add new features to its own. These new features in the
derived class will not affect the base class. The derived class is the specialized
class for the base class.
== Assignment Operator vs Copy Constructor ==
A copy constructor is called when a new object is created from an existing object,
as a copy of the existing object. The assignment operator is called when an already
initialized object is assigned a new value from another existing object.
== Virtual Deconstructor ==
Deleting a derived class object using a pointer of base class type that has
a non-virtual destructor results in undefined behavior. To correct this
situation, the base class should be defined with a virtual destructor.
For example, following program results in undefined behavior.
virtual destructor ensures that when derived subclasses go out of
scope or are deleted the order of destruction of each class in a
hierarchy is carried out correctly.
== Virtual and Constructor ==
In C++, the constructor cannot be virtual, because when a constructor of a
class is executed there is no virtual table in the memory, means no virtual
pointer defined yet. So, the constructor should always be non-virtual.
== Virtual Base Class ==
For each distinct base class that is specified virtual, the most derived object
contains only one base class subobject of that type, even if the class appears
many times in the inheritance hierarchy (as long as it is inherited virtual
every time).
== V table ==
V-tables (or virtual tables) are how most C++ implementations do polymorphism.
For each concrete implementation of a class, there is a table of function
pointers to all the virtual methods. A pointer to this table (called the
virtual table) exists as a data member in all the objects. When one calls
a virtual method, we lookup the object's v-table and call the appropriate
derived class method.
It's a dispatch table for all the virtual methods.
== Polymorphism ==
Polymorphism in C++ means, the same entity (function or object) behaves
differently in different scenarios.
The word polymorphism means having many forms. Typically, polymorphism occurs when there
is a hierarchy of classes and they are related by inheritance.
C++ polymorphism means that a call to a member function will cause a different function
to be executed depending on the type of object that invokes the function
Function Overloading && Operator Overloading are polymorphism!
Consider this example:
The “ +” operator in c++ can perform two specific functions at two different
scenarios i.e when the “+” operator is used in numbers, it performs addition.
Function overloading is also a form of polymorphism.
== Shallow Copy ==
In shallow copy, an object is created by simply copying the data of all
variables of the original object. This works well if none of the variables
of the object are defined in the heap section of memory. If some variables
are dynamically allocated memory from heap section, then the copied object
variable will also reference the same memory location.
This will create ambiguity and run-time errors, dangling pointer. Since both
objects will reference to the same memory location, then change made by one
will reflect those change in another object as well. Since we wanted to
create a replica of the object, this purpose will not be filled by Shallow
copy.
== Deep Copy ==
In Deep copy, an object is created by copying data of all variables, and
it also allocates similar memory resources with the same value to the object.
In order to perform Deep copy, we need to explicitly define the copy
constructor and assign dynamic memory as well, if required. Also, it is
required to dynamically allocate memory to the variables in the other
constructors, as well.
== Abstract Class ==
Abstract classes are used to represent general concepts (for example,
Shape, Animal), which can be used as base classes for concrete classes
(for example, Circle, Dog).
No objects of an abstract class can be created (except for base subobjects
of a class derived from it) and no non-static data members of an abstract
class can be declared.
An abstract class is, conceptually, a class that cannot be instantiated
and is usually implemented as a class that has one or more pure virtual
(abstract) functions.
An abstract class is a class that is designed to be specifically used as a
base class. An abstract class contains at least one pure virtual function.
You declare a pure virtual function by using a pure specifier (= 0) in the
declaration of a virtual member function in the class declaration.
== Interfaces ==
There are no interfaces in C++, we can only emulate them with abstract classes.
Interface -> C++ class with only pure virtual methods (i.e. without any code)
Abstract -> C++ class with virtual methods that can be overridden, and some code,
but at least one pure virtual method that makes the class not instantiable.
== nan ==
Not A Number
The NaN values are used to identify undefined or non-representable values for
floating-point elements, such as the square root of negative numbers or the
result of 0/0.
== type casting ==
-> static_cast
This is used for the normal/ordinary type conversion. This is also the cast
responsible for implicit type coersion and can also be called explicitly.
You should use it in cases like converting float to int, char to int, etc.
static_cast < new-type > ( expression )
Returns a value of type new-type.
-> dynamic_cast
dynamic_cast < new-type > ( expression )
new-type -> pointer to complete class type, reference to complete class type,
or pointer to (optionally cv-qualified) void
expression -> lvalue (until C++11) glvalue (since C++11) of a complete class type
if new-type is a reference, prvalue of a pointer to complete class type
if new-type is a pointer.
If the cast is successful, dynamic_cast returns a value of type new-type. If the
cast fails and new-type is a pointer type, it returns a null pointer of that type.
If the cast fails and new-type is a reference type, it throws an exception that
matches a handler of type std::bad_cast.
This cast is used for handling polymorphism. You only need to use it when
you're casting to a derived class. This is exclusively to be used in inheritence
when you cast from base class to derived class.
== serialization ==
Serialization is a mechanism to convert an object into a sequence of bytes so
that it can be stored in memory. The byte stream, once created, also can be
streamed across a communication link to a remote receiving end.
== std::stack ==
The std:: Stack class is a container adapter that gives the programmer the
functionality of a stack - specifically, a LIFO (last-in, first-out) data structure.
The class template acts as a wrapper to the underlying container - only a
specific set of functions is provided. The stack pushes and pops the element
from the back of the underlying container, known as the top of the stack.
== difference pointer and reference ==
POINTER -> is a variable that holds the memory address of another variable.
- can be initialized to any value anytime after it is declared
- can be assigned to point to a NULL value
- need to be dereferenced with a *
- can be changed to point to any variable of the same type
REFERENCE -> is an alias for an already existing variable
- must be initialized when it is declared
- cannot be NULL
- can be used simply by name
- once initialized to a variable, it cannot be changed to refer to a variable object