Föreläsningsanteckningar OOP-föreläsning 13, måndag 16 oktober 2006 =================================================================== Idag ==== 1. Forts: Vektorklassen "Vektor", med felkontroll 2. Kort: Standardbibliotekets vektorklass, "vector" 3. Uppgifter från en gammal tenta 1. Forts: Vektorklassen "Vektor", med felkontroll ================================================= Utdrag ur vektor-3.cpp ---------------------- // ... template class Vektor { private: T* lagring; int storlek; public: Vektor(int storlek); Vektor(const Vektor& original); ~Vektor(); const Vektor& operator=(const Vektor& original); T& Vektor::operator[](int i); }; // ... template T& Vektor::operator[](int i) { return lagring[i]; } Utdrag ur vektor-4.cpp ---------------------- // ... class FelaktigtIndexUndantag { }; template class Vektor { private: T* lagring; int storlek; public: Vektor(int storlek); Vektor(const Vektor& original); ~Vektor(); const Vektor& operator=(const Vektor& original); T& Vektor::operator[](int i); }; // ... template T& Vektor::operator[](int i) { if (lagring < 0 || i >= storlek) throw FelaktigtIndexUndantag(); return lagring[i]; } // ... int main() { Vektor v(3); v[0] = 7.1; v[2] = 5.9; cout << "v[0] == " << v[0] << endl; cout << "v[1] == " << v[1] << endl; v[7] = 1.17; } Utmatning --------- v[0] == 7.1 v[1] == 0 terminate called after throwing an instance of 'FelaktigtIndexUndantag' Abort (core dumped) Utdrag ur vektor-5.cpp ---------------------- int main() { try { Vektor v(3); v[0] = 7.1; v[2] = 5.9; cout << "v[0] == " << v[0] << endl; cout << "v[1] == " << v[1] << endl; v[7] = 1.17; } catch (FelaktigtIndexUndantag e) { cout << "Det uppstod ett fel: Felaktigt index\n"; } } Utmatning --------- v[0] == 7.1 v[1] == 0 Det uppstod ett fel: Felaktigt index vektor-6.cpp ------------ #include #include #include using namespace std; class FelaktigtIndexUndantag : public exception { private: int i, min, max; public: FelaktigtIndexUndantag(int i, int min, int max); virtual const char* what() const throw(); }; FelaktigtIndexUndantag::FelaktigtIndexUndantag(int i, int min, int max) { this->i = i; this->min = min; this->max = max; } const char* FelaktigtIndexUndantag::what() const throw() { ostringstream oss; oss << "Felaktigt index i en vektoroperation: " << i << " (ska vara " << min << ".." << max << ")"; return oss.str().c_str(); // Fel. Varför? } template class Vektor { private: T* lagring; int storlek; public: Vektor(int storlek); Vektor(const Vektor& original); ~Vektor(); const Vektor& operator=(const Vektor& original); T& Vektor::operator[](int i) throw (FelaktigtIndexUndantag); }; template Vektor::Vektor(int storlek) { this->storlek = storlek; lagring = new T[storlek]; } template Vektor::Vektor(const Vektor& original) { this->storlek = original.storlek; lagring = new T[original.storlek]; for (int i = 0; i < storlek; ++i) lagring[i] = original.lagring[i]; } template Vektor::~Vektor() { delete [] lagring; } template const Vektor& Vektor::operator=(const Vektor& original) { if (this != &original) { delete [] lagring; this->storlek = original.storlek; lagring = new T[original.storlek]; for (int i = 0; i < storlek; ++i) lagring[i] = original.lagring[i]; } return *this; } template T& Vektor::operator[](int i) throw (FelaktigtIndexUndantag) { if (lagring < 0 || i >= storlek) throw FelaktigtIndexUndantag(i, 0, storlek - 1); return lagring[i]; } int main() { try { Vektor v(3); v[0] = 7.1; v[2] = 5.9; cout << "v[0] == " << v[0] << endl; cout << "v[1] == " << v[1] << endl; v[7] = 1.17; } catch (FelaktigtIndexUndantag& e) { cout << "Det uppstod ett indexfel: " << e.what() << endl; throw; } catch (exception& e) { cout << "Det uppstod ett annat fel: " << e.what() << endl; throw; } catch (...) { cout << "Det uppstod ett annat fel." << endl; throw; } } Utmatning --------- v[0] == 7.1 v[1] == 0 Det uppstod ett indexfel: Felaktigt index i en vektoroperation: 7 (ska vara 0..2) terminate called after throwing an instance of 'FelaktigtIndexUndantag' what(): Felaktigt index i en vektoroperation: 7 (ska vara 0..2) Abort (core dumped) Utdrag ur vektor-7.cpp ---------------------- const char* FelaktigtIndexUndantag::what() const throw() { ostringstream oss; oss << "Felaktigt index i en vektoroperation: " << i << " (ska vara " << min << ".." << max << ")"; string s = oss.str(); const char* cs1 = s.c_str(); char* cs2 = new char[strlen(cs1) + 1]; strcpy(cs2, cs1); return cs2; } 2. Kort: Standardbibliotekets vektorklass, "vector" =================================================== vektor-8.cpp ------------ #include #include using namespace std; int main() { try { vector v(3); // operator[]() -- Subscripting access without bounds checking v[0] = 7.1; v[2] = 5.9; cout << "v[0] == " << v[0] << endl; cout << "v[1] == " << v[1] << endl; v[7] = 1.17; cout << "v[19] == " << v[19] << endl; // at() -- Subscripting access with bounds checking v.at(1) = 77.3; cout << "v.at(0) == " << v.at(0) << endl; cout << "v.at(1) == " << v.at(1) << endl; cout << "v.at(19) == " << v.at(19) << endl; } catch (exception& e) { cout << "Det uppstod ett annat fel: " << e.what() << endl; // throw; } } Utmatning --------- v[0] == 7.1 v[1] == 0 v[19] == 0 v.at(0) == 7.1 v.at(1) == 77.3 Det uppstod ett annat fel: vector::_M_range_check 3. Uppgifter från en gammal tenta =================================