STL string class

String Bound Exceptions

Page 1 of 1

1 Replies - 4147 Views - Last Post: 19 November 2008 - 12:48 AM Rate Topic: -----

#1 SCStudent86   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 06-October 08

STL string class

Posted 18 November 2008 - 05:17 PM

I'm suppose to write a very very very simple program that checks the bounds on a string initialized by a class object. I create a char operator[](int k) function that tests to see if k is greater than or less than the size of the string. If k is outside of this range then it will declare an instance of class BoundsException. A constuctor is defined in this class that will display "Out of Range". I think my char operator function is correct. I've tested the process in main, but for some reason i'm getting an "=" symbol instead of a letter. How come?? I wouldn't be surprised if it is the damn char operator function. I'm currently having a brain fart :P

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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: STL string class

#2 muballitmitte   User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: STL string class

Posted 19 November 2008 - 12:48 AM

you forgot one thing in your constructor...

 BCheckString(string s)
			{
				numLetters = s.length();
				letter = new char[numLetters];
		strcpy(letter, s.c_str()); //you need to copy the string :D or else...
			}



there is also one more error...
 char operator[](int k)
			{
				if(k < 0 || k > numLetters)
				{
					BoundsException obj;
					//need to stop or else unexpected results! 
									   //you could use return
					 return -1; //not a very good idea though

				}
				return letter[k];
			}



or you could use exceptions ..this is more elegant but costly

 char operator[](int k)
			{
				if(k < 0 || k > numLetters)
				{
					BoundsException obj;
					throw obj;

				}
				return letter[k];
			}



and use it like this

int main()
{
	
	BCheckString object("Starting out with C++.");
	try
	  {
			  cout << object[100];
	}
	catch(...) {}
	getchar();
	return 0;
}


This post has been edited by muballitmitte: 19 November 2008 - 12:52 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1