User Tools

Site Tools


cpp:exception

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cpp:exception [2023/05/15 13:58] gthanoscpp:exception [2023/05/15 14:01] (current) gthanos
Line 11: Line 11:
 class Vector { class Vector {
   int *array;   int *array;
-  int size;+  long size;
      
 public: public:
-  Vector(int length=0);+  Vector(long length=0);
   ~Vector();   ~Vector();
   int &valueAt(int pos) const;  // returns a reference to element at position pos   int &valueAt(int pos) const;  // returns a reference to element at position pos
Line 22: Line 22:
 <code cpp Vector.cpp> <code cpp Vector.cpp>
 #include "Vector.hpp" #include "Vector.hpp"
-Vector::Vector(int length) {+Vector::Vector(long length) {
   size = length;   size = length;
   array = new (nothrow) int[size];   array = new (nothrow) int[size];
Line 52: Line 52:
  
 int main() { int main() {
-  int size;+  long size;
   cout << "Enter verctor size: ";   cout << "Enter verctor size: ";
   cin >> size;   cin >> size;
Line 71: Line 71:
 class Vector { class Vector {
   int *array;   int *array;
-  int size;+  long size;
      
 public: public:
-  Vector(int length=0);+  Vector(long length=0);
   ~Vector();   ~Vector();
   int &valueAt(int pos) const;  // returns a reference to element at position pos   int &valueAt(int pos) const;  // returns a reference to element at position pos
Line 84: Line 84:
 #include "Vector.hpp" #include "Vector.hpp"
  
-Vector::Vector(int length) {+Vector::Vector(long length) {
   size = length;   size = length;
   array = new int[size];   array = new int[size];
Line 108: Line 108:
  
 int main() { int main() {
-  int size;+  long size;
      
   cout << "Enter verctor size: ";   cout << "Enter verctor size: ";
Line 115: Line 115:
     Vector v(size);     Vector v(size);
   } catch(std::bad_alloc ex) {   } catch(std::bad_alloc ex) {
-    if(size<=0) { +    std::cout << "Allocation failure!\n";
-      cout << "Vector size should be a positive integerRetry...\n"; +
-      continue; +
-    }+
     exit(-1);     exit(-1);
   }   }
cpp/exception.1684159114.txt.gz · Last modified: 2023/05/15 13:58 by gthanos