From a0fc2e6e6da14c67ed1038f38be9117a949c60e5 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Thu, 25 Mar 2021 22:21:53 +0100 Subject: [PATCH] Lezione 4 + 1/2 --- teoria/a.out | Bin 0 -> 8632 bytes teoria/classi/constructors_and_operators.cpp | 90 +++++++ teoria/classi/declaration.cpp | 26 ++ teoria/classi/destructors.cpp | 17 ++ teoria/classi/methods.cpp | 14 ++ .../examples/classes/build.sh | 4 + .../examples/classes/main.cpp | 225 ++++++++++++++++++ .../examples/templates/build.sh | 4 + .../examples/templates/main.cpp | 135 +++++++++++ 9 files changed, 515 insertions(+) create mode 100644 teoria/a.out create mode 100644 teoria/classi/constructors_and_operators.cpp create mode 100644 teoria/classi/declaration.cpp create mode 100644 teoria/classi/destructors.cpp create mode 100644 teoria/classi/methods.cpp create mode 100644 teoria/esempi_professore/examples/classes/build.sh create mode 100644 teoria/esempi_professore/examples/classes/main.cpp create mode 100644 teoria/esempi_professore/examples/templates/build.sh create mode 100644 teoria/esempi_professore/examples/templates/main.cpp diff --git a/teoria/a.out b/teoria/a.out new file mode 100644 index 0000000000000000000000000000000000000000..cbea98fdc597defa3b26fb56e37641706b34b9f9 GIT binary patch literal 8632 zcmeHMYiwM_6`u9Ri39O^2?;nrnT(C+ZKiWp6NTDi~K}DGIs7oQ+@64TV zefN5eXw^TJm}q@wzVnzf&wJ+NE4>4Kb)k@A39HX2X|*o(kWRyxyvkHSI#q{Sj=!H! z*C-KNqA`!(=@D?fGG|PaHW)k%DtaAt9uTD07&%gUD74=2YLRoxJ(N_bmq zo1igWAw6pRc+gPlNmKMr8@xT=Cci<02Wby@QbRTzzlgkyx0d|Pi{LUVwfs?-*Mgf$-f)A=Iw2gh?+ zr{E^?uHz`De{h$RO6SuDGX*!DAKcYFmdmCG6T@R^k5`*>$L{@ZM&t^8Ap^X9gE9Xmd(elEf&!o44q`?Ppf1}D2toz+}9 z$1z>wd23zfF-`{X;|4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nj0AjbKG$!DH*W>vQw#?b8sv-oEURbZZ2PN62(cC%#BZsrQLMOx^dg~ zj@m+!;mSCPd_Hl=NoU>sAvKaujHjJcaeVv`RD3N5l5Uk~ax76OqzhJZVgl*4{q8{9 z_K8e7nZ7quNIS_~w&3QANu<(*)!{gqoLE4!z0HwKnwv>#n;A*5p@r06BAY_S&(o83 zYbK+dodbKi;sa1A4m+UZ;0=k0c5mWLBMC+kke@KIQa$es_FVe>Zql zZl|gNoocM~u&1wYfA63(81EYBh1GN_;UU8A>haWfOy9NM%-BmrlIAB2`QaH;FM}zvvgYV4 zkkJe}xiA;|pX6Js9aUE0(72lz26giu9c3+(MRIpSS=pSMw&Gp=8{Nb~(>jprJwi9^mid&A|Q{ z-;&aQhK<2+Z8!Ag8%6yyrb2q&^ebVH%oFwb{<#&Hyt$~)c$t*%Hi+mVHvHu-G~`W0 zea6+K{I<}4%8`B%`trtNoAEbkr|C@m7a96*V09Q3nN#XBPABENkNs1h z=SlhQ6#Xmk3+f*SCUe5DlJ9O(zE2^dOK|;5K>hlBACR(tqptIPQjZ$oo1lJuz9UHK zuMw~KPyDffKI0P7zXuka{nFld1NzJ}kOsra;P{^~`dmN8X{1~?_7~LuVL*Se|0i8c z-;1l~_YdDgq!Lg03+Ij4Sybe0!Mp|ISpGk1aK5NdD(9=or_cA*DU-ON97J?soAhO% zviI0O-(mcJY90*kPwLSxd56dlEBZ|_wDAAj;uOm59;y7LKIv=V`1LFCsOfia48o?o=n^gn4PPQEjn@S{JxKV*N? gcfbA_xhznA88FU%J+8b>YU>|1PhNS)`Abm$-yvkTMF0Q* literal 0 HcmV?d00001 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; +}