Skip to content

Commit 2c3d8b6

Browse files
committed
added lecture 3
1 parent 6b1a05c commit 2c3d8b6

File tree

18 files changed

+722
-5
lines changed

18 files changed

+722
-5
lines changed

Diff for: README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@ Giovedì 06/10 – Aula 128-129
3434
- review of last homework
3535
- survival crush course on git
3636

37-
Martedì 11/10 – Aula 128-129
37+
Martedì 11/10 – cancelled
3838

39-
Giovedì 13/10 – Aula 128-129
39+
Giovedì 13/10 – cancelled
4040

41-
Martedì 18/10 – Aula 004
41+
Martedì 18/10 – zoom
42+
- general program structure
43+
- Makefiles
44+
- operator overloads
45+
- working with text files
4246

43-
Giovedì 20/10 – Aula 128-129
47+
Giovedì 20/10 – zoom
4448

4549
Martedì 25/10 – Aula 128-129
4650

@@ -66,7 +70,7 @@ Martedì 29/11 – Aula 128-129
6670

6771
Giovedì 01/12 – Aula 128-129
6872

69-
Martedì 06/12 – Aula 004
73+
Martedì 06/12 – AULA 139
7074

7175
Giovedì 08/12 – Festa
7276

Diff for: examples/files.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <iostream>
2+
#include <fstream>
3+
4+
int main(){
5+
//you can do the same things as with std::cout and std::cin
6+
7+
//this will create "test.txt" if it doesn't exist or overwrite if it does
8+
std::ofstream filevar("test.txt");
9+
//filvar.open("name.txt")
10+
filevar << "1 2 3"<<std::endl;
11+
filevar.close();
12+
13+
int a{0},b{0},c{0};
14+
std::ifstream filevar2("test.txt");
15+
//better to check if it worked, cause it won't complain
16+
if(filevar2){
17+
filevar2 >> a >>b >>c;
18+
}else{
19+
std::cout<<"failed opening the file"<<std::endl;
20+
}
21+
22+
std::cout<<a<< " " <<b <<" "<< c<<std::endl;
23+
filevar2.close();
24+
25+
//this will append instead of overwriting
26+
filevar.open("test.txt", std::ios_base::app);
27+
filevar << "1 2 3"<<std::endl;
28+
filevar.close();
29+
30+
return 0;
31+
}

Diff for: examples/hello_again/Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This is a comment line
2+
CC=g++
3+
# This is a folder to look for header files
4+
INCDIR=include
5+
# CFLAGS will be the options passed to the compiler.
6+
CFLAGS= -c -I$(INCDIR)
7+
8+
hello.x: main.o hello.o
9+
$(CC) main.o hello.o -o hello.x
10+
main.o: main.cpp
11+
$(CC) $(CFLAGS) main.c
12+
src/hello.o: src/hello.c
13+
$(CC) $(CFLAGS) hello.c
14+
clean:
15+
rm -rf *.o *.x

Diff for: examples/hello_again/Makefile2

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This is a comment line
2+
CC=g++
3+
# where to look for headers:
4+
INCDIR=include
5+
OBJDIR=obj
6+
# CFLAGS will be the options passed to the compiler.
7+
CFLAGS= -c -I$(INCDIR)
8+
OBJECTS= main.o $(OBJDIR)/hello.o
9+
10+
hello.x: $(OBJECTS)
11+
$(CC) $(OBJECTS) -o $@
12+
main.o: main.cpp
13+
$(CC) $(CFLAGS) main.cpp
14+
obj/%.o: src/%.cpp
15+
$(CC) $(CFLAGS) $^ -o $@
16+
clean:
17+
rm -rf *.o *.x
18+

Diff for: examples/hello_again/include/hello.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
void hello();

Diff for: examples/hello_again/main.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "hello.hpp"
2+
3+
int main(){
4+
hello();
5+
return 0;
6+
}

Diff for: examples/hello_again/src/hello.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <iostream>
2+
#include "hello.hpp"
3+
4+
void hello(){
5+
std::cout<<"Hello, World!"<<std::endl;
6+
}
7+

Diff for: examples/operators1.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <iostream>
2+
#include <cmath>
3+
4+
//example of overloading arithmetic operators
5+
6+
class CCoords{
7+
double x;
8+
double y;
9+
public:
10+
CCoords(){
11+
x=0;y=0;
12+
}
13+
CCoords(const double& x0,const double& y0):x(x0),y(y0){}
14+
CCoords operator+(const CCoords& b) {
15+
CCoords result;
16+
result.x = x + b.x;
17+
result.y = y + b.y;
18+
return result;
19+
}//operator+
20+
/* //this is a wrong way, as it becomes non-commutative
21+
CCoords operator+(const int& b) {
22+
CCoords result;
23+
result.x = x + b;
24+
result.y = y + b;
25+
return result;
26+
}//operator+
27+
*/
28+
friend CCoords operator+(const CCoords& c, const int& b);
29+
friend CCoords operator+(const int& b, const CCoords& c);
30+
31+
//have to assume it can be non-integer
32+
bool operator==(const CCoords& b) {
33+
bool result{false};
34+
if ((std::fabs(x-b.x)<10e-9) && (std::fabs(y-b.y)<10e-9)){
35+
result=true;
36+
}
37+
return result;
38+
}//operator==
39+
void print(){
40+
std::cout<<"x is "<<x<<", y is "<<y<<std::endl;
41+
}
42+
43+
};
44+
45+
46+
CCoords operator+(const CCoords& c, const int& b){
47+
CCoords result;
48+
result.x=c.x+b;
49+
result.y=c.y+b;
50+
return result;
51+
};
52+
53+
CCoords operator+(const int& b, const CCoords& c){
54+
CCoords result;
55+
result.x=c.x+b;
56+
result.y=c.y+b;
57+
return result;
58+
};
59+
60+
int main(){
61+
CCoords var(2,3),var2(4.4,9.1);
62+
63+
auto var3=var + var2;
64+
var3.print();
65+
if(var2==var3){
66+
std::cout<<"equal"<<std::endl;
67+
}else{
68+
std::cout<<"not equal"<<std::endl;
69+
}
70+
auto var4= var+2;
71+
var4=2+var;
72+
return 0;
73+
}

Diff for: examples/operators2.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <iostream>
2+
#include <cmath>
3+
4+
struct CCoords{
5+
double x;
6+
double y;
7+
CCoords(){ x=0;y=0; }
8+
CCoords operator+(const CCoords& b);
9+
bool operator==(const CCoords& b);
10+
11+
//instead of a print function we can overload <<
12+
//get a habit to make it a friend in case you change to proper private values
13+
friend std::ostream& operator<<(std::ostream& os, const CCoords& c);
14+
//a totally nonsense overload just to show that we can
15+
void operator[](const int& N){
16+
std::cout<<"What did you intend to do with calling [] on coordinates?..."<<std::endl;
17+
}
18+
//another totally nonsense overload just to show that we can
19+
void operator()(){
20+
std::cout<<"What did you intend to do with calling () on coordinates?..."<<std::endl;
21+
};
22+
};
23+
24+
std::ostream& operator<<(std::ostream& os, const CCoords& c){
25+
os<<"x is "<<c.x<<", y is "<<c.y<<std::endl;
26+
return os;
27+
};
28+
29+
30+
int main(){
31+
CCoords var,var2;
32+
var.x=2; var.y=3;
33+
var2.x=4.4; var2.y=9.1;
34+
auto var3=var + var2;
35+
std::cout<<var3<<std::endl;
36+
var[7];
37+
var();
38+
return 0;
39+
}
40+
41+
42+
43+
bool CCoords::operator== ( const CCoords& b ) {
44+
bool result{false};
45+
if ((std::fabs(x-b.x)<10e-9) && (std::fabs(y-b.y)<10e-9)){
46+
result=true;
47+
}
48+
return result;
49+
}//operator==
50+
51+
52+
CCoords CCoords::operator+ ( const CCoords& b ) {
53+
CCoords result;
54+
result.x = x + b.x;
55+
result.y = y + b.y;
56+
return result;
57+
}
58+

0 commit comments

Comments
 (0)