lezione 3

This commit is contained in:
Alessandro Ferro 2021-03-19 11:30:28 +01:00 committed by GitHub
parent b1b5f30109
commit 53043b9f42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 91 additions and 1 deletions

View File

@ -0,0 +1,26 @@
#include<iostream>
using namespace std;
enum class Color{red, white, green, black};
ostream& operator<<(ostream& outputStream, const Color& color){ //la keyword const non è obbligatoria, ma meglio metterla
switch(color){
case Color::red:
outputStream<<"Rosso";
break;
case Color::white:
outputStream<<"Bianco";
break;
case Color::green:
outputStream<<"Verde";
break;
case Color::black:
outputStream<<"Nero";
}
return outputStream;
}
int main(){
Color myColor = Color::red;
cout<<myColor<<endl;
return 0;
}

View File

@ -0,0 +1,27 @@
#include <iostream>
using namespace std;
enum class Color1{green, white, red};
enum class Color2{black, blue, brown, green, red};
enum Color3{rosso, arancione};
//enum Color4{rosso, viola}; //errore, rosso già dichiarato
int main(){
// Posso accedere ai valori di Color3 come fossero variabili globali
cout<<rosso<<" "<<arancione<<endl; // 0 1
// Non posso accedere direttamente ai valori di Color1 e Color2
//cout<<green;
//Ma devo richiamarli in questo modo:
cout<<(int)Color1::green<<endl; // a differenza dell'enum normale, devo fare un cast
//if(rosso == Color1::green) //errore
if(rosso == (int)Color1::green) cout<<"Cast obbligatorio"<<endl;
Color1 mioColore = Color1::green;
if(mioColore == Color1::green) cout<<"Tipi uguali direttamente confrontabili"<<endl;
}

19
teoria/friendFunction.cpp Normal file
View File

@ -0,0 +1,19 @@
#include<iostream>
using namespace std;
struct Studente{
friend void printSecureNumber(Studente); // posso mettere questa linea ovunque all'interno della struct
string nome;
string cognome;
private:
int secureNumber = 999;
};
void printSecureNumber(Studente s){
cout<<s.secureNumber<<endl;
}
int main(){
Studente st;
printSecureNumber(st);
return 0;
}

View File

@ -0,0 +1,17 @@
#include <iostream>
using namespace std;
struct Studente{
string nome, cognome, matricola;
int annoCorso;
};
ostream& operator<<(ostream& outputStream, const Studente& st){ //la keyword const non è obbligatoria, ma meglio metterla
return outputStream<<st.nome<<" "<<st.cognome<<" "<<st.matricola<<" "<<st.annoCorso<<endl;
}
istream& operator>>(istream& inputStream, Studente& st){
return inputStream>>st.nome>>st.cognome>>st.matricola>>st.annoCorso;
}
int main(){
Studente s;
cin>>s;
cout<<s;
}

View File

@ -9,6 +9,7 @@ int main(){
} }
void studentStructWithConstructor(){ void studentStructWithConstructor(){
struct StudentNoDefaultConstructor{ struct StudentNoDefaultConstructor{
string nome, cognome, matricola; string nome, cognome, matricola;
int annoCorso; int annoCorso;
@ -46,7 +47,7 @@ void studentStruct(){
int annoCorso; int annoCorso;
}; };
struct Student st1{"Alan","Turing","N86000000",1}; struct Student st1{"Alan","Turing","N86000000",1};
struct Student st2 = {"Kurt","Godel","N86000001",1}; struct Student st2 = {"Kurt","Godel","N86000001",1}; // non preferibile
//Student st3("Kurt","Godel","N86000001",1); // errore, richiede esplicitamente un costruttore //Student st3("Kurt","Godel","N86000001",1); // errore, richiede esplicitamente un costruttore
struct Student st4; struct Student st4;
struct Student st5(); struct Student st5();