Declaration of class derived from STL string class....
/**********************************************************
*
*
*
**********************************************************/
#include<iostream>
#include<string>
#include "BoundsException.h"
using std::string;
using std::cout;
using std::endl;
using std::operator <;
using std::operator >;
namespace MyName_StringBoundExceptions
{
#ifndef STRINGBOUNDEXCEPTIONS_H
#define STRINGBOUNDEXCEPTIONS_H
class BCheckString : string
{
private:
int numLetters;
char *letter;
public:
BCheckString(string s)
{
numLetters = s.length();
letter = new char[numLetters];
}
~BCheckString()
{
delete [] letter;
letter = NULL;
}
char operator[](int k)
{
if(k < 0 || k > numLetters)
{
BoundsException obj;
}
return letter[k];
}
};
#endif
}
Declaration of class BoundsException....
/**********************************************************
*
*
*
**********************************************************/
#include<iostream>
using std::cout;
using std::endl;
namespace MyName_StringBoundExceptions
{
#ifndef BOUNDSEXCEPTION_H
#define BOUNDSEXCEPTION_H
class BoundsException
{
public:
BoundsException()
{
cout << "Out of range." << endl << endl;
}
};
#endif
}
Main.cpp
/**********************************************************
*
*
*
**********************************************************/
#include<iostream>
#include<string>
#include "BCheckString.h"
using std::string;
using std::endl;
using std::cout;
using MyName_StringBoundExceptions::BCheckString;
int main()
{
BCheckString object("Starting out with C++.");
cout << object.operator [](5);
return 0;
}

New Topic/Question
Reply



MultiQuote



|