------------------------------------------------------------------------------
MC logo
Safe Array Demo
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Template Functions] [Template Generalized Stack] [Template Generalized Stack Driver] [Array Template Stack] [Array Template Test Driver] [Generalized Safe Array] [Safe Array Demo] [Vector Iterator Demo]
gensafedemo.cpp
#include <iostream>
#include <string>
#include "gensafearr.h"

using namespace std;

main()
{
        SafeArr<int> a1(6);

        for(int i = 0; i < a1.size(); ++i)
                a1.store(i, 3*i + 2);
        cout << "p1: " << a1 << endl;

        int tot = 0;
        for(int i = 0; i < a1.size(); ++i) tot += a1.fetch(i);
        cout << "Total is " << tot << endl;

        for(int i = -3; i < 10; i += 2) {
                try {
                        cout << "Fetch at " << i << ": ";
                        cout << a1.fetch(i) << endl;
                } catch(const range_error &e) {
                        cout << "Ooops! " << e.what() << endl;
                }
                try {
                        int val = 10*i+5;
                        cout << "Store " << val << " at " << i;
                        a1.store(i, val);
                } catch(const range_error &e) {
                        cout << ": Ooops! " << e.what();
                }
                cout << endl;
        }

        cout << "p2: " << a1 << endl;

        SafeArr<string> stra(5);

        stra.store(0, "This");
        stra.store(1, "is");
        stra.store(2, "the");
        stra.store(3, "last");
        stra.store(4, "thing");

        string pref = "";
        for(int i = 0; i < stra.size(); ++i) {
                cout << pref << stra.fetch(i);
                pref = " ";
        }
        cout << "." << endl;
}