Lezione 4 + 1/2

This commit is contained in:
Alessandro Ferro 2021-03-25 22:21:53 +01:00 committed by GitHub
parent 7688efdf80
commit a0fc2e6e6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 515 additions and 0 deletions

BIN
teoria/a.out Normal file

Binary file not shown.

View File

@ -0,0 +1,90 @@
#include<iostream>
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!"<<endl;
}
/**** OPERATORI ****/
bool operator==(const Punto& obj) const noexcept{
return ((x==obj.x) && (y==obj.y));
}
int operator[](const uint position){
switch (position) {
case 0:
return x;
break;
case 1:
return y;
break;
default:
return -1;
}
}
Punto& operator=(const Punto& p){ // operatore di assegnamento COPY
// ?
}
Punto& operator=(Punto&& p) noexcept{ // operatore di assegnamento MOVE
// ?
}
};
ostream& operator<<(ostream& outstr, const Punto& p) {
return outstr<<p.x<<";"<<p.y;
}
istream& operator>>(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<<p5<<endl<<p6<<endl;
}
void testOperators(){
Punto p1,p2;
cin>>p1;
cout<<"Coordinata X: "<<p1[0]<<" Coordinata Y: "<<p1[1]<<endl;
cin>>p2;
cout<<"Coordinata X: "<<p2[0]<<" Coordinata Y: "<<p2[1]<<endl;
if(p1==p2){
cout<<"I due punti sono uguali"<<endl;
}else{
cout<<"I due punti non sono uguali"<<endl;
}
}

View File

@ -0,0 +1,26 @@
#include<iostream>
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;
}

View File

@ -0,0 +1,17 @@
#include<iostream>
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
}

14
teoria/classi/methods.cpp Normal file
View File

@ -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
*/

View File

@ -0,0 +1,4 @@
#! /bin/bash
g++ -O3 -o main main.cpp

View File

@ -0,0 +1,225 @@
#include <iostream>
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;
}

View File

@ -0,0 +1,4 @@
#! /bin/bash
g++ -O3 -o main main.cpp

View File

@ -0,0 +1,135 @@
#include <iostream>
using namespace std;
// template <typename Data>
// class A;
//
// template <typename Data>
// ostream& operator<< (ostream& outstr, const A<Data>& a);
template <typename Data>
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 <typename DataX>
friend ostream& operator<<(ostream&, const A<DataX>&);
// friend ostream& operator<< <>(ostream&, const A<Data>&);
};
template <typename Data>
ostream& operator<< (ostream& outstr, const A<Data>& a) {
for (uint i = 0; i < a.size; i++) { cout << i << ": '" << a.str[i] << "' "; }
return outstr;
}
int main() {
A<char> var1(2);
A<char> 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<int> var3(2);
A<int> 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;
}