0% found this document useful (0 votes)
32 views2 pages

OOP C++ Zadaci 2

The document contains the solutions to an object-oriented programming exam with 3 questions. 1) Defines rect and size classes to represent rectangles and offsets, and overloads operators for rectangle addition and intersection. 2) Defines an array class template with constructor and begin/end iterators to print initialized elements. 3) Reads people's names and heights from a file into a vector, filters out invalid heights, calculates the average, sorts by height then name, and prints results.

Uploaded by

tvoj Sam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views2 pages

OOP C++ Zadaci 2

The document contains the solutions to an object-oriented programming exam with 3 questions. 1) Defines rect and size classes to represent rectangles and offsets, and overloads operators for rectangle addition and intersection. 2) Defines an array class template with constructor and begin/end iterators to print initialized elements. 3) Reads people's names and heights from a file into a vector, filters out invalid heights, calculates the average, sorts by height then name, and prints results.

Uploaded by

tvoj Sam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Objektno orijentirano programiranje

ispit
4. travanj 2005.

1. (30 bodova)
Napišite klase rect i size tako da sljedeći program:

#include <iostream>

int main(){
oop::rect r1(0, 0, 50, 50), r2(50, 50, 100, 100);
r1 += oop::size(10, 10); // offset rect
std::cout << (r1 & r2); // print intersection
}

ispisuje presjek dva pravokutnika:


(50,50,60,60)

2. (30 bodova)
Napišite klasu array (u konstruktoru se svi elementi postavljaju na zadanu vrijednost) tako da sljedeći program:
#include <iostream>
#include <algorithm>

int main(){
array<double, 8> a(1.);
a[0] = a[2] = 2;

std::ostream_iterator<double> os(std::cout, " ");


std::copy(a.begin(), a.end(), os);
}

ispisuje sve elemente polja:


2 1 2 1 1 1 1 1

3. (40 bodova)
U datoteci osobe.txt se u svakom redu nalazi ime osobe i njezina visina (u cm). Napišite program koji koristeći standardne algoritme:
a) učita sve osobe u vektor
b) izbaci sve sa visinom manjom od 100 i većom od 200 cm
c) ispiše prosjek visina
d) sortira silazno po visini (u slučaju iste visine, uzlazno po abecedi)
e) ispiše sve osobe u obliku "ime: visina"
Objektno orijentirano programiranje
rješenja ispita
4. travanj 2005.

1. 2.
namespace oop { template <typename T, int N>
class array {
class size { T a[N];
public: public:
int x, y; array(const T& v){
size(int x, int y) : x(x), y(y) {} for(int i=0; i<N; ++i)
}; a[i] = v;
}
class rect { T& operator[](int i) { return a[i]; }
public: T* begin() { return a; }
int left, top, right, bottom; T* end() { return a+N; }
rect(int l, int t, int r, int b) : };
left(l), top(t), right(r), bottom(b) {}

void operator+=(const size& s) {


left += s.x; right += s.x;
top += s.y; bottom += s.y;
}

rect operator&(const rect& r) const {


return rect(max(left, r.left), max(top, r.top),
min(right, r.right), min(bottom, r.bottom));
}
};

std::ostream& operator<<(std::ostream& os, const rect& r){


return os << '(' << r.left << ',' << r.top << ','
<< r.right << ',' << r.bottom << ')';
}

} // namespace
3.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric> // accumulate
using namespace std;

struct person {
string name;
unsigned int height; // cm
operator unsigned int() const { return height; } // for accumulate
};

istream& operator>>(istream& is, person& p){


return is >> p.name >> p.height;
}

ostream& operator<<(ostream& os, const person& p){


return os << p.name << ": "<< p.height;
}

bool rem(const person& p){


return p.height < 100 || p.height > 200;
}

bool comp(const person& p1, const person& p2){


return p1.height != p2.height ? p1.height > p2.height : p1.name < p2.name;
}

int main(){
ifstream f("osobe.txt");
istream_iterator<person> is(f), es;
ostream_iterator<person> os(cout, "\n");

vector<person> c;
copy(is, es, back_inserter(c));
c.erase(remove_if(c.begin(), c.end(), rem), c.end());
if(c.size())
cout << "prosjek: " << accumulate(c.begin(), c.end(), 0.)/c.size() << endl;

sort(c.begin(), c.end(), comp);


copy(c.begin(), c.end(), os);
}

You might also like