Skip to content

Commit 690d4d4

Browse files
committed
Fixing files for seminar 2, adding example
1 parent 351bc70 commit 690d4d4

File tree

6 files changed

+85
-29
lines changed

6 files changed

+85
-29
lines changed

02-lrefs/clref.cc

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
#include <cstdio>
1+
//-----------------------------------------------------------------------------
2+
//
3+
// Source code for MIPT ILab
4+
// Slides: https://sourceforge.net/projects/cpp-lects-rus/files/cpp-graduate/
5+
// Licensed after GNU GPL v3
6+
//
7+
//-----------------------------------------------------------------------------
8+
//
9+
// Example for reference binding: displaying addresses
10+
//
11+
//----------------------------------------------------------------------------
12+
13+
#include <iostream>
214

315
int foo() { return 42; }
416

517
int main() {
618
int x;
719
int &rx = x;
820
const int &l = foo();
9-
printf("%p\n", &l);
10-
printf("%p %p\n", &x, &rx);
21+
std::cout << &l << " " << &x << " " << &rx << std::endl;
1122
}

02-lrefs/newdelete.cc

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//-----------------------------------------------------------------------------
2+
//
3+
// Source code for MIPT ILab
4+
// Slides: https://sourceforge.net/projects/cpp-lects-rus/files/cpp-graduate/
5+
// Licensed after GNU GPL v3
6+
//
7+
//-----------------------------------------------------------------------------
8+
//
9+
// Example: wrong pairing of new/delete and consequences
10+
// compile with: g++ -g newdelete.cc
11+
// try x/20x P-1 in gdb
12+
//
13+
//----------------------------------------------------------------------------
14+
15+
#include <iostream>
16+
17+
struct MySmallClass {
18+
int t = 1;
19+
MySmallClass() { std::cout << "small ctor" << std::endl; }
20+
~MySmallClass() { std::cout << "small dtor" << std::endl; }
21+
};
22+
23+
struct MyBigClass {
24+
int t = 1, p = 2, q = 3;
25+
MyBigClass() { std::cout << "big ctor" << std::endl; }
26+
~MyBigClass() { std::cout << "big dtor" << std::endl; }
27+
};
28+
29+
int main() {
30+
MyBigClass *S = new MyBigClass;
31+
MySmallClass *T = new MySmallClass;
32+
MyBigClass *P = new MyBigClass[5];
33+
MySmallClass *Q = new MySmallClass[7];
34+
delete[] T; // terribly wrong
35+
}

02-lrefs/ptref.cc

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
//-----------------------------------------------------------------------------
2+
//
3+
// Source code for MIPT ILab
4+
// Slides: https://sourceforge.net/projects/cpp-lects-rus/files/cpp-graduate/
5+
// Licensed after GNU GPL v3
6+
//
7+
//-----------------------------------------------------------------------------
8+
//
9+
// Example: function signature for pointer and reference
10+
// compile with: g++ -O1 -g0 -masm=intel -S ptref.cc
11+
//
12+
//----------------------------------------------------------------------------
13+
114
int g;
215

316
void foo(int *x) { g = *x; }

02-lrefs/spacial.cc

-24
This file was deleted.

02-lrefs/triangles/lingeo.hpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
//-----------------------------------------------------------------------------
2+
//
3+
// Source code for MIPT ILab
4+
// Slides: https://sourceforge.net/projects/cpp-lects-rus/files/cpp-graduate/
5+
// Licensed after GNU GPL v3
6+
//
17
//----------------------------------------------------------------------------
28
//
39
// Example for triangle intersection
4-
// Originated from homework of D. Bushev
10+
// Originated from homework of Dmitry Bushev
511
//
612
//----------------------------------------------------------------------------
713

02-lrefs/triangles/square.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
1-
#include "lingeo.hpp"
1+
//-----------------------------------------------------------------------------
2+
//
3+
// Source code for MIPT ILab
4+
// Slides: https://sourceforge.net/projects/cpp-lects-rus/files/cpp-graduate/
5+
// Licensed after GNU GPL v3
6+
//
7+
//-----------------------------------------------------------------------------
8+
//
9+
// Example: triangle intersection, driver program
10+
//
11+
//----------------------------------------------------------------------------
12+
13+
#include <cassert>
214
#include <iostream>
315
#include <vector>
416

17+
#include "lingeo.hpp"
18+
519
polygon_t input_triangle() {
620
std::vector<point_t> verts(3);
721
point_t temp;
822
for (int i = 0; i < 3; i++) {
923
std::cin >> temp.x;
1024
std::cin >> temp.y;
1125
verts[i] = temp;
26+
assert(std::cin.good());
1227
}
1328
polygon_t ret{verts};
1429
return ret;

0 commit comments

Comments
 (0)