FaustoFaggion / 42_school_CPP

CPP piscine from 42 school

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

42_school_CPP

CPP piscine from 42 school

CPP00

### Namespaces, classes, member functions, stdio streams, initialization lists, static, const, and some other stuff

Namespace

Used 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.

Class

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;
};

access_specifiers

  • 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.

scope operator '::'

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.

Keyword This

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

Constructors

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.

Overloading constructors

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.

Default Constructor

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.

Uniform Initialization

  • 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, ... }

EX00

EX01

conversion hierarchy

UML diagram

EX02

CPP01

Memory Allocation, pointers to members, references, switch statement

EX00

EX01

EX02

EX03 - Inicialization List

links:

https://www.youtube.com/watch?v=1nfuYMXjZsA

EX04

EX05

EX06 - Switch Case

links:

https://www.youtube.com/watch?v=YpvVnZNMk_g

CPP02

Ad-hoc Polimorphism, operator overloading and Ortodox Canonical class

EX00

Float Point Numbers, Const, Static.

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

EX01

EX02

EX03 BSP Binary Space Partioning

Links:

https://www.youtube.com/watch?v=HYAgJN3x4GA

https://www.youtube.com/watch?v=ntjM9YZP0qk

CPP03

Inheritanc

EX00

EX01

links:

https://www.youtube.com/watch?v=X8nYM8wdNRE

Function Overiding

https://www.youtube.com/watch?v=Zd_4xa071nc

EX02

EX03 Vitual fuctions, Vtable, Multiple inheritance

Vtable vs Pointers

Runtime polimorphism

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

CPP04

EX00

EX01 Deep ans Shallow copy

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

EX02 Viltual Function, Pure Virtual Function, Abstract Class

Abstract class - It is a class that has at least one pure virtual function.

links:

https://www.youtube.com/watch?v=T8f4ajtFU9g

EX03

CPP05 Exceptions( try and catch)

EX00

Try and Catch

links:https://www.youtube.com/watch?v=kjEhqgmEiWY

EX01

EX02

EX03

CPP06 - Type Conversions

EX00 Conversion of Scalar Types

conversion hierarchy

conversion hierarchy

EX01

EX02 Dynamic Casting

Used only for polymorfic classes.

Cherno - https://www.youtube.com/watch?v=CiHfz6pTolQ

CPP07 - Template

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

CPP08

std::stack

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

About

CPP piscine from 42 school


Languages

Language:C++ 88.4%Language:Makefile 10.8%Language:Shell 0.9%