diff --git a/teoria/a.out b/teoria/a.out new file mode 100644 index 0000000..cbea98f Binary files /dev/null and b/teoria/a.out differ diff --git a/teoria/classi/constructors_and_operators.cpp b/teoria/classi/constructors_and_operators.cpp new file mode 100644 index 0000000..75f38ea --- /dev/null +++ b/teoria/classi/constructors_and_operators.cpp @@ -0,0 +1,90 @@ +#include +using namespace std; + +void testOperators(); +void testConstructors(); + +class Punto{ +public: + float x,y; + + /**** COSTRUTTORI ****/ + Punto() = default; // costruttore di default; + Punto(float x1, float y1){ // costruttore + x = x1; + y = y1; + } + Punto(const Punto& puntoDaCopaire){ // costruttore di copia + x = puntoDaCopaire.x; + y = puntoDaCopaire.y; + } + Punto(Punto&& puntoDaMuovere) noexcept{ // move constructor + + } + + /**** DISTRUTTORE ****/ + ~Punto(){ + cout<<"BYE!"<>(istream& inputStream, Punto& p){ + return inputStream>>p.x>>p.y; +} + +int main(){ + testConstructors(); + testOperators(); + return 0; +} + +void testConstructors(){ + Punto p1; // OK, costruttore di default + //Punto px(); // NO! Il compilatore si inceppa (fa eseguire ma non accedere ai dati) + Punto p2{}; // OK, costruttore di default + Punto p3(1,2); // OK + Punto p4(3,4); // OK + + Punto p5(p3); //OK, costruttore di copia + Punto p6{p4}; // OK; costruttore di copia + cout<>p1; + cout<<"Coordinata X: "<>p2; + cout<<"Coordinata X: "< +using namespace std; +class ClassName{ + int defaultVariable; // di default è privata +public: + int publicVariable; +protected: + int protectedVariable; +private: + int privateVariable; +}; +int main(){ + // Dichiarazione 1 + ClassName obj; + obj.publicVariable = 3; + std::cout << obj.publicVariable << '\n'; + // obj.defaultVariable = 3; // errore, di default è privata + // obj.protectedVariable = 3; // errore + // obj.privateVariable = 3; // errore + + // Dichiarazione 2 + ClassName obj2{}; + obj2.publicVariable = 5; + std::cout << obj2.publicVariable << '\n'; + return 0; +} diff --git a/teoria/classi/destructors.cpp b/teoria/classi/destructors.cpp new file mode 100644 index 0000000..99bb8c1 --- /dev/null +++ b/teoria/classi/destructors.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; +class ClassName{ +public: + int x; + int* v = new int[10]; + ~ClassName(){ // ~ si fa con ALT GR + ì + delete[] v; + cout<<"BYE!"; + } +}; +int main(){ +ClassName obj; +ClassName* ptr = new ClassName(); +delete ptr; // viene chiamato il distruttore di ClassName per cancellare ptr; +return 0; // viene chiamato il distruttore di ClassName per cancellare obj +} diff --git a/teoria/classi/methods.cpp b/teoria/classi/methods.cpp new file mode 100644 index 0000000..76ead54 --- /dev/null +++ b/teoria/classi/methods.cpp @@ -0,0 +1,14 @@ +/* + I metodi in C++ sono siffatti: + [virtual] type nomeMetodo(parametri) [const] [noexcept] [override] [=assignment] + + virtual: + const: il metodo non può modificare le variabili della ClassName + noexcept: già visto. Non può sollevare eccezioni pena l'aborto del programma + override: il metodo sovrascrive un metodo nella gerarchia + assignment: può essere: + = 0: pure virtual. A questo livello l'implementazione di questo metodo non esiste + = default: ammesso solo per cost. e dist. che dice di creare quelli di default + =delete: elimina un metodo precedentemente scritto nella gerarchia + +*/ diff --git a/teoria/esempi_professore/examples/classes/build.sh b/teoria/esempi_professore/examples/classes/build.sh new file mode 100644 index 0000000..f409c84 --- /dev/null +++ b/teoria/esempi_professore/examples/classes/build.sh @@ -0,0 +1,4 @@ + +#! /bin/bash + +g++ -O3 -o main main.cpp diff --git a/teoria/esempi_professore/examples/classes/main.cpp b/teoria/esempi_professore/examples/classes/main.cpp new file mode 100644 index 0000000..4089ebc --- /dev/null +++ b/teoria/esempi_professore/examples/classes/main.cpp @@ -0,0 +1,225 @@ + +#include + +using namespace std; + +// Example 1 + +class A { + +protected: + + uint size = 0; + + char* str = nullptr; + +public: + + A() = default; + + A(uint num) : size(num) { + cout << "A new object is created" << endl; + // size = num; + str = new char[num](); + } + + ~A() { + delete[] str; + cout << "The object is destructed" << endl; + } + + char& operator[](const uint idx) { + return str[idx]; + // if (idx < size) + // { + // return str[idx]; + // } + // else + // { + // throw out_of_range("An unexpected access occurred!"); + // } + } + + bool operator==(const A& obj) const noexcept { + if (size == obj.size) + { + for (uint i = 0; i < size; i++) + { + if (str[i] != obj.str[i]) + { + return false; + } + } + return true; + } + return false; + } + + friend ostream& operator<<(ostream&, const A&); + +}; + +ostream& operator<<(ostream& outstr, const A& a) { + for (uint i = 0; i < a.size; i++) { cout << i << ": '" << a.str[i] << "' "; } + return outstr; +} + +// // Example 2 +// +// class A { +// private: +// bool flag = false; +// // protected: +// public: +// A() { cout << "A new base object is created" << endl; } +// ~A() { cout << "The base object is destructed" << endl; } +// }; +// +// class B: public A { +// public: +// B() { cout << "A new derived object is created" << endl; flag = false; } +// ~B() { cout << "The derived object is destructed" << endl; } +// }; + +// // Example 3 + +// class A { +// public: +// A() { cout << "A new base object is created" << endl; } +// ~A() { cout << "The base object is destructed" << endl; } // virtual +// }; +// +// class B: public A { +// public: +// B() { cout << "A new derived object is created" << endl; } +// ~B() { cout << "The derived object is destructed" << endl; } +// }; + +// // Example 4 +// +// class A { +// protected: +// uint size = 0; +// public: +// A() { cout << "A new base object is created" << endl; } +// ~A() { cout << "The base object is destructed" << endl; } // virtual +// }; +// +// class B: public A { // virtual public A +// public: +// B() { cout << "A new derived B object is created" << endl; size = 1; } +// ~B() { cout << "The derived B object is destructed" << endl; } +// }; +// +// class C: public A { // virtual public A +// public: +// C() { cout << "A new derived C object is created" << endl; size = 2; } +// ~C() { cout << "The derived C object is destructed" << endl; } +// }; +// +// class D: public B, C { // public C, B +// public: +// D() { cout << "A new derived D object is created" << endl; } +// ~D() { cout << "The derived D object is destructed" << endl; } +// void PrintSize() { cout << size << endl; }; +// // void PrintSize() const noexcept { cout << B::size << " " << C::size << endl; }; +// }; + +// // Example 5 +// +// class A { +// protected: +// uint val = 0; +// public: +// +// A() { val++; std::cout << "A " << val << std::endl; } +// +// A(const A& a) { val = a.val; val += 2000; std::cout << "Copy A " << val << std::endl; } +// +// ~A() { std::cout << "Delete A " << val << std::endl; } +// }; +// +// class B : public A { // virtual +// public: +// +// B() { val += 10; std::cout << "B " << val << std::endl; } // Equivalently, you can add ": A()" +// +// B(const B& b) : A(b) { val += 20000; std::cout << "Copy B " << val << std::endl; } // Remove ": A(b)" +// +// ~B() { std::cout << "Delete B " << val << std::endl; } +// }; +// +// class C : public B { // virtual +// public: +// +// C() { val += 100; std::cout << "C " << val << std::endl; } // Equivalently, you can add ": B()". +// +// C(const C& c) : B(c) { val += 200000; std::cout << "Copy C " << val << std::endl; } // Remove ": B(c)"; Add ": A(c)" when A is virtual in B +// +// ~C() { std::cout << "Delete C " << val << std::endl; } +// }; + + +int main() { + + // Example 1 + + // A* ptr = new A(); + + A var1(2); // 0, {} + A var2(2); + + cout << "var1: " << var1 << endl; + cout << "var2: " << var2 << endl; + + cout << "var1 ?= var2: " << (var1 == var2) << endl; + + var1[0] = 'x'; + + cout << "var1: " << var1 << endl; + cout << "var2: " << var2 << endl; + + cout << "var1 ?= var2: " << (var1 == var2) << endl; + + var2[1] = 'y'; + + cout << "var1: " << var1 << endl; + cout << "var2: " << var2 << endl; + + cout << "var1 ?= var2: " << (var1 == var2) << endl; + + var1[1] = 'y'; + var2[0] = 'x'; + + cout << "var1: " << var1 << endl; + cout << "var2: " << var2 << endl; + + cout << "var1 ?= var2: " << (var1 == var2) << endl; + + // // Example 2 + // + // B* b = new B(); + // delete b; + // return 0; + + // // Example 3 + // + // A *a = new B(); + // delete a; + // return 0; + + // // Example 4 + // + // D d; + // d.PrintSize(); + + // // Example 5 + // + // std::cout << std::endl; + // C x; + // std::cout << std::endl; + // C y(x); + // std::cout << std::endl; + + return 0; +} diff --git a/teoria/esempi_professore/examples/templates/build.sh b/teoria/esempi_professore/examples/templates/build.sh new file mode 100644 index 0000000..f409c84 --- /dev/null +++ b/teoria/esempi_professore/examples/templates/build.sh @@ -0,0 +1,4 @@ + +#! /bin/bash + +g++ -O3 -o main main.cpp diff --git a/teoria/esempi_professore/examples/templates/main.cpp b/teoria/esempi_professore/examples/templates/main.cpp new file mode 100644 index 0000000..ff151e9 --- /dev/null +++ b/teoria/esempi_professore/examples/templates/main.cpp @@ -0,0 +1,135 @@ + +#include + +using namespace std; + +// template +// class A; +// +// template +// ostream& operator<< (ostream& outstr, const A& a); + +template +class A { + +private: + +protected: + + uint size = 0; + + Data* str = nullptr; + +public: + + A() = default; + + A(uint num) : size(num) { + cout << "A new object is created" << endl; + str = new Data[num](); + } + + ~A() { + delete[] str; + cout << "The object is destructed" << endl; + } + + Data& operator[](const uint idx) { + return str[idx]; + } + + bool operator==(const A& obj) const noexcept { + if (size == obj.size) + { + for (uint i = 0; i < size; i++) + { + if (str[i] != obj.str[i]) + { + return false; + } + } + return true; + } + return false; + } + + template + friend ostream& operator<<(ostream&, const A&); + + // friend ostream& operator<< <>(ostream&, const A&); + +}; + +template +ostream& operator<< (ostream& outstr, const A& a) { + for (uint i = 0; i < a.size; i++) { cout << i << ": '" << a.str[i] << "' "; } + return outstr; +} + + +int main() { + + A var1(2); + A var2(2); + + cout << "var1: " << var1 << endl; + cout << "var2: " << var2 << endl; + + cout << "var1 ?= var2: " << (var1 == var2) << endl; + + var1[0] = 'x'; + + cout << "var1: " << var1 << endl; + cout << "var2: " << var2 << endl; + + cout << "var1 ?= var2: " << (var1 == var2) << endl; + + var2[1] = 'y'; + + cout << "var1: " << var1 << endl; + cout << "var2: " << var2 << endl; + + cout << "var1 ?= var2: " << (var1 == var2) << endl; + + var1[1] = 'y'; + var2[0] = 'x'; + + cout << "var1: " << var1 << endl; + cout << "var2: " << var2 << endl; + + cout << "var1 ?= var2: " << (var1 == var2) << endl; + + cout << endl << endl; + + A var3(2); + A var4(2); + + cout << "var3: " << var3 << endl; + cout << "var4: " << var4 << endl; + + cout << "var3 ?= var4: " << (var3 == var4) << endl; + + var3[0] = 10; + + cout << "var3: " << var3 << endl; + cout << "var4: " << var4 << endl; + + cout << "var3 ?= var4: " << (var3 == var4) << endl; + + var4[1] = 5; + + cout << "var3: " << var3 << endl; + cout << "var4: " << var4 << endl; + + cout << "var3 ?= var4: " << (var3 == var4) << endl; + + var3[1] = 5; + var4[0] = 10; + + cout << "var3: " << var3 << endl; + cout << "var4: " << var4 << endl; + + cout << "var3 ?= var4: " << (var3 == var4) << endl; + + return 0; +}