Commit ceaa48ec by Patryk Czarnik

Klasa wyjątku IndexOutOfBounds

parent 01002383
......@@ -40,8 +40,8 @@ int main() {
try {
w[50] = 5050;
std::cout << "udało się ustawić element 50\n";
} catch(const char *exn) {
std::cout << "wyjątek " << exn << '\n';
} catch(IndexOutOfBounds &exn) {
std::cout << "wyjątek " << exn.what() << '\n';
}
std::cout << "\nsuma wynosi: " << suma(w) << "\n\n";
......
#include <iostream>
#include <sstream>
#include "wektor.h"
// Implementacja klasy IndexOutOfBounds
IndexOutOfBounds::IndexOutOfBounds(TypIndeksu requested_size, TypIndeksu actual_size)
: requested_size{requested_size}, actual_size{actual_size} { }
std::string IndexOutOfBounds::what() {
std::stringstream s;
s << "Index " << requested_size << " out of bounds (" << actual_size << ")";
return s.str();
}
// Implementacja klasy Wektor
// Dla klas, które same zarządzają pamięcią, istotne jest zdefiniowanie
// w spójny sposób takich elementów klasy, jak:
// - konstruktor kopiujący
......@@ -74,14 +89,14 @@ void Wektor::push_back(TypWartosci e) {
TypWartosci Wektor::operator[](TypIndeksu idx) const {
if(idx < 0 || idx >= liczba_elementow) {
throw "indeks poza zakresem";
throw IndexOutOfBounds{idx, liczba_elementow};
}
return t[idx];
}
TypWartosci& Wektor::operator[](TypIndeksu idx) {
if(idx < 0 || idx >= liczba_elementow) {
throw "indeks poza zakresem";
throw IndexOutOfBounds{idx, liczba_elementow};
}
return t[idx];
}
......
......@@ -2,10 +2,21 @@
#define _WEKTOR_H_
#include <iostream>
#include <exception>
typedef int TypWartosci;
typedef unsigned int TypIndeksu;
class IndexOutOfBounds : public std::exception {
public:
const TypIndeksu requested_size, actual_size;
IndexOutOfBounds(TypIndeksu requested_size, TypIndeksu actual_size);
std::string what();
};
/** Tablica zmiennej długości zawierająca wartości int. */
class Wektor {
TypWartosci *t;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment