CPP piscine from 42 school
### Namespaces, classes, member functions, stdio streams, initialization lists, static, const, and some other stuffUsed to prevent name collisions when using multiple libraries, a namespace is a declarative prefix for functions, classes, types, etc.
A C++ namespace is a collection of C++ entities (functions, classes, variables), whose names are prefixed by the name of the namespace. When writing code within a namespace, named entities belonging to that namespace need not be prefixed with the namespace name, but entities outside of it must use the fully qualified name. The fully qualified name has the format ::.
Namespaces are useful for grouping related definitions together.
Classes are an expanded concept of data structures: like data structures, they can contain:
- data members (attributes)
- functions as members (methods)
An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.
Classes are defined using either keyword class or keyword struct, with the following syntax:
class "name" {
access_specifier_1:
member1;
access_specifier_2:
member2;
};
- private: members of a class are accessible only from within other members of the same class (or from their "friends").
- protected: members are accessible from other members of the same class (or from their "friends"), but also from members of their derived classes.
- public: members are accessible from anywhere where the object is visible.
It is used to define a member of a class outside the class itself.
class Rectangle {
private:
int width, height;
public:
void set_values (int,int);
int area() {return width*height;}
};
void Rectangle::set_values (int x, int y) {
width = x;
height = y;
}
In the example above the method set_values was prototyped inside the class but its definition is outside, the operator of scope ( : : ) is used to specify that the function being defined is a member of the class Rectangle and not a regular non-member function.
Within a member function of a class, the keyword this is a pointer to the instance of the class on which the function was called.
This cannot be used in a static member function.
link
It's aspecial function which is automatically called whenever a new object of this class is created.
The constructor initialize the member variables or allocate storage to avoid an undetermined result in case of a variable had never been assigned a value.
Constructors cannot be called explicitly as if they were regular member functions. They are only executed once, when a new object of that class is created.
Notice how neither the constructor prototype declaration (within the class) nor the latter constructor definition, have return values; not even void: Constructors never return values, they simply initialize the object.
Like any other function, a constructor can also be overloaded with different versions taking different parameters: with a different number of parameters and/or parameters of different types. The compiler will automatically call the one whose parameters match the arguments.
The default constructor is the constructor that takes no parameters, and it is special because it is called when an object is declared but is not initialized with any arguments.
Initialization
Note how rectb is not even constructed with an empty set of parentheses - in fact, empty parentheses cannot be used to call the default constructor.
Rectangle rectb; // ok, default constructor called
Rectangle rectc(); // oops, default constructor NOT called
This is because the empty set of parentheses would make of rectc a function declaration instead of an object declaration: It would be a function that takes no arguments and returns a value of type Rectangle.
- Functional form:
Class();
- Variable initialization syntax:
"class name" "objectname" = initialization_value;
- Uniform initialization:
essentially the same as the functional form, but using braces ({}): class_name object_name { value, value, value, ... }
UML diagram
links:
https://www.youtube.com/watch?v=1nfuYMXjZsA
links:
https://www.youtube.com/watch?v=YpvVnZNMk_g
links:
https://www.youtube.com/watch?v=LXF-wcoeT0o
Computerphile:
https://www.youtube.com/watch?v=PZRI1IfStY0
https://www.youtube.com/watch?v=f4ekifyijIg
Const in C++
https://www.youtube.com/watch?v=4fJBrditnJU
Static
https://www.youtube.com/watch?v=f3FVU-iwNuA
Static in class
https://www.youtube.com/watch?v=V-BFlMrBtqQ
https://www.youtube.com/watch?v=Ldv5i14UhTA
Copy Constructor; Copy Assignment (Deep and Shallow copy)
https://www.youtube.com/watch?v=EBgBM7rPDic
Overloading operator << >>
https://www.youtube.com/watch?v=2972LRdyquk&t=355s
Links:
https://www.youtube.com/watch?v=HYAgJN3x4GA
https://www.youtube.com/watch?v=ntjM9YZP0qk
links:
https://www.youtube.com/watch?v=X8nYM8wdNRE
Function Overiding
https://www.youtube.com/watch?v=Zd_4xa071nc
Virtual functions are called depending on the object type instead of the pointer or reference type.
links:
Virtual Functions
https://www.youtube.com/watch?v=oIV2KchSyGQ
Vtable
https://www.youtube.com/watch?v=hS7kPtVB1vI
Multiple Inheritance
https://www.youtube.com/watch?v=KVREEoovDd4
links:
Deep and Shallow copy
https://www.youtube.com/watch?v=BvR1Pgzzr38&t=875s
Virtual Functions
https://www.youtube.com/watch?v=oIV2KchSyGQ
Vtable
https://www.youtube.com/watch?v=hS7kPtVB1vI
Abstract class - It is a class that has at least one pure virtual function.
links:
https://www.youtube.com/watch?v=T8f4ajtFU9g
Try and Catch
links:https://www.youtube.com/watch?v=kjEhqgmEiWY
conversion hierarchy
Used only for polymorfic classes.
Cherno - https://www.youtube.com/watch?v=CiHfz6pTolQ
There are two places where we can apply templates:
- function templates
- class templates
Templates are the way to write generic programs
The main point is we pass data type as parameter to function or class.
Templates will be evaluated at compile time. It is just created when is called in the code. It will be created with the type that was called.
Cherno - https://www.youtube.com/watch?v=I-hZkUa9mIs
Function as parameter of another function - https://www.youtube.com/watch?v=67vTjgbT56Q
It is a container adapter. It's not a container, it's adapting a existing container and mapping the functions.
By default internaly uses the std::deque STL container.
It's a LIFO(last-in, first-out) data structure