Skip to content

Commit 60d4239

Browse files
committed
Imported previous semester example.
1 parent 9e9b5a6 commit 60d4239

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

cpp/cpp5/exceptions.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<iostream>
2+
3+
int quotient(int top, int bot) {
4+
return top / bot;
5+
}
6+
7+
int shift(int a, int b) {
8+
return a * b;
9+
}
10+
11+
void doStuff(int x, int y) {
12+
try {
13+
if ( (x==13) || (y==13) ) {
14+
throw 13;
15+
}
16+
std::cout << " " << x << " * " << y << " = " << shift(x,y) << std::endl;
17+
if ( (y == 0) ) {
18+
throw 0;
19+
}
20+
std::cout << " " << x << " / " << y << " = " << quotient(x,y) << std::endl;
21+
}
22+
catch(int e) {
23+
if (e == 0)
24+
std::cout << "Warning: attempt to devide by 0!" << std::endl;
25+
else
26+
std::cout << "Unlucky number !" << e << "!" << std::endl;
27+
}
28+
}
29+
30+
int main() {
31+
std::cout << "doStuff(2,3):" << std::endl;
32+
doStuff(2,3);
33+
std::cout << std::endl << "doStuff(2,0):" << std::endl;
34+
doStuff(2,0);
35+
std::cout << std::endl << "doStuff(26,13):" << std::endl;
36+
doStuff(13,2);
37+
return 0;
38+
}

cpp/cpp5/smartptr.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<iostream>
2+
#include<memory>
3+
4+
using namespace std;
5+
6+
int main() {
7+
8+
shared_ptr<int> sptr1(new int(1));
9+
shared_ptr<int> sptr2(new int(2));
10+
unique_ptr<int> uptr3(new int(10));
11+
12+
cout << "Shared pointer1: " << *sptr1 << endl;
13+
cout << "Shared pointer2: " << *sptr2 << endl;
14+
cout << "Unique pointer3: " << *uptr3 << endl;
15+
cout << endl;
16+
17+
*sptr1 = 3;
18+
cout << "sptr1 value changed" << endl;
19+
cout << "Shared pointer1: " << *sptr1 << endl;
20+
cout << "Shared pointer2: " << *sptr2 << endl;
21+
cout << "Unique pointer3: " << *uptr3 << endl;
22+
cout << endl;
23+
24+
sptr2 = sptr1;
25+
cout << "sptr2=sptr1 " << endl;
26+
cout << "Shared pointer1: " << *sptr1 << endl;
27+
cout << "Shared pointer2: " << *sptr2 << endl;
28+
cout << "Unique pointer3: " << *uptr3 << endl;
29+
cout << endl;
30+
31+
// sptr2 = new int(4);
32+
sptr2.reset(new int(4));
33+
cout << "sptr2 changed" << endl;
34+
cout << "Shared pointer1: " << *sptr1 << endl;
35+
cout << "Shared pointer2: " << *sptr2 << endl;
36+
cout << "Unique pointer3: " << *uptr3 << endl;
37+
cout << endl;
38+
39+
return 0;
40+
}

0 commit comments

Comments
 (0)