Menu

Friday, 15 May 2015

Chapter 9, Problem 6: OOP By Robert Lafore

Problem Statement:

Start with the ARROVER3 program in Chapter 8. Keep the safearay class the same as in
that program, and, using inheritance, derive the capability for the user to specify both the
upper and lower bounds of the array in a constructor. This is similar to Exercise 9 in
Chapter 8, except that inheritance is used to derive a new class (you can call it safehilo)
instead of modifying the original class.

Solution:

//Ahmad Furqan
//P9.6
#include <iostream>
#include <conio.h>
using namespace std;
#include <process.h> //for exit()
const int LIMIT = 100; //array size
////////////////////////////////////////////////////////////////
class safearay
{
private:
int arr[LIMIT];
public:
int& operator [](int n) //note: return by reference
{
if (n< 0 || n >= LIMIT)
{
cout << "\nIndex out of bounds"; exit(1);
}
return arr[n];
}
};
////////////////////////////////////////////////////////////////
class safehilo :public safearay
{
private:
int llimit, ulimit;
public:
safehilo(int a, int b) :llimit(a), ulimit(b)
{
if ((b - a) > LIMIT)
{
cout << "Array limits exceed maximum permissible range."; exit(1);
}
}
int& operator [](int n)
{
if (n < llimit || n >= ulimit)
{
cout << "\nIndex out of bounds"; _getch(); exit(1);
}
safearay::operator[](n - llimit);
}
};
////////////////////////////////////////////////////////////////
void main(void)
{
safehilo sa1(100,175);
for (int j = 100; j<175; j++) //insert elements
sa1[j] = j * 10; //*left* side of equal sign
for (int j = 100; j<175; j++) //display elements
{
int temp = sa1[j]; //*right* side of equal sign
cout << "Element " << j << " is " << temp << endl;
}
_getch();
}

2 comments: