Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Anonymous
unregistriert
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
: #include <iostream.h> #include <conio.h> const int DefaultSize = 10; class Array { public: // Konstruktoren Array(int itsSize = DefaultSize); Array(const Array &rhs); ~Array() { delete [] pType;} // Operatoren Array& operator=(const Array&); int& operator[](int offSet); const int& operator[](int offSet) const; // Zugriffsfunktionen int GetitsSize() const { return itsSize; } // Friend-Funktion friend ostream& operator<< (ostream&, const Array&); // Exception-Klassen definieren class xBoundary {}; class xSize { public: xSize(int size):itsSize(size) {} ~xSize(){} int GetSize() { return itsSize; } private: int itsSize; }; class xTooBig : public xSize { public: xTooBig(int size):xSize(size){} }; class xTooSmall : public xSize { public: xTooSmall(int size):xSize(size){} }; class xZero : public xTooSmall { public: xZero(int size):xTooSmall(size){} }; class xNegative : public xSize { public: xNegative(int size):xSize(size){} }; private: int *pType; int itsSize; }; Array::Array(int size): itsSize(size) { if (size == 0) throw xZero(size); if (size > 30000) throw xTooBig(size); if (size <1) throw xNegative(size); if (size < 10) //Hier wird die ausnahme erstellt throw xTooSmall(size); pType = new int[size]; for (int i = 0; i<size; i++) pType[i] = 0; } int& Array::operator[] (int offSet) { //int size = GetitsSize(); if (offSet >= 0 && offSet < GetitsSize()) return pType[offSet]; throw xBoundary(); // return pType[0]; } const int& Array::operator[] (int offSet) const { // int size = GetitsSize(); if (offSet >= 0 && offSet < GetitsSize()) return pType[offSet]; throw xBoundary(); // return pType[0]; } int main() { try { Array intArray(8); for (int j = 0; j< 100; j++) { intArray[j] = j; cout << "intArray[" << j << "] OK..." << endl; } } catch (Array::xBoundary) { cout << "Kann Ihre Eingabe nicht verarbeiten.\n"; } catch (Array::xZero theException) { cout << "Array von null Objekten angefordert." << endl; cout << "Empfangen " << theException.GetSize() << endl; } catch (Array::xTooBig theException) { cout << "Dieses Array ist zu groß..." << endl; cout << "Empfangen " << theException.GetSize() << endl; } catch (Array::xTooSmall theException) { cout << "Dieses Array ist zu klein..." << endl; cout << "Empfangen " << theException.GetSize() << endl; } catch (...) { cout << "Etwas ist schiefgelaufen. Keine Ahnung, was!\n"; } cout << "Fertig.\n"; getch(); return 0; //geht zu return - leider keine ahnung warum - nachdem das ausnahmeobjekt erstellt wurde //und will den destruktor aufrufen. dieses führt zu einer fehlermeldung und das programm //stürzt ab } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// Konstruktor: pType wird mit NULL-Initialisiert! Array::Array(int size): itsSize(size), pType(0) { if (size == 0) throw xZero(size); if (size > 30000) throw xTooBig(size); if (size <1) throw xNegative(size); if (size < 10) //Hier wird die ausnahme erstellt throw xTooSmall(size); pType = new int[size]; for (int i = 0; i<size; i++) pType[i] = 0; } Array::~Array() { if(pType) { delete[] pType; pType = 0; // Um ganz sicher zugehen ;) } } |
C-/C++-Quelltext |
|
1 |
std::cin.get(); |
Anonymous
unregistriert
C-/C++-Quelltext |
|
1 2 |
using namespace std; //Wäre es so nicht noch perfekter??? |
Zitat von »"Der Dude"«
[klugscheiß_zum_quadrat]
C-/C++-Quelltext
1 2 using namespace std; //Wäre es so nicht noch perfekter???
[/klugscheiß_zum_quadrat]
anstatt so:
anstatt so:
std::cin.get();
nap ist das was ??
Anonymous
unregistriert
Werbeanzeige