I'm working on one of the programming challenges in the book Starting Out With C++ Early Objects 7th Edition and one of the assignments asks to create a class which is derived from the STL string class. I'm posting the question for the purpose of understanding what I am allowed to do and how I am supposed to implement the solution so that no one offers more advanced suggestions.
-- Question as it is written in the text --
Palindrome Testing
A Palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam, and radar are palindromes. Write a class Pstring
that is derived from the STL string class
. The Pstring class
adds a member function
bool isPalindrome()
that determines whether the string is a palindrome. Include a constructor that takes an STL string
object as a parameter and passes it to the string base class constructor. Test your class by having a main program that asks the user to enter a string. The program uses the string to initialize a Pstring object and then calls isPalindrome() to determine whether the string entered is a palindrome.
You may find it useful to use the subscript operator [] of the string class: if str is a string object and k is an integer, then str[k] returns the caracter at the position k in the string.
-- End --
My main question is how do I access the member variable which holds my string object if the class I am deriving Pstring from is a class I did not write and I do not know how it implements its members?
For example,
#include <string>
using namespace std;
class Pstring : public string
{
public:
Pstring(std::string text)
: string(text) { }
bool isPalindrome()
{
// How do I access the string if I am passing it to the base class?
// What I think I should do is...
bool is_palindrome = true;
auto iBegin = begin();
auto iEnd = end() - 1;
while (iBegin < iEnd && is_palindrome)
{
if (*iBegin++ != *iEnd--)
is_palindrome = false;
}
return is_palindrome;
// But I think this is wrong because...
// #1 The book did not discuss the keyword auto yet
// #2 The book discussed when a class is derived from another class,
// how the members from super class will be accessible to the sub class.
// However, with this assignment, I don't see how to access the members.
}
}
The reason I feel like I am doing this incorrectly is because the assignment mentions using subscript notation, however, I don't understand how to use the subscript notation if I don't know the name of the variable where the string is stored.
Any help would be greatly appreciated because the author does not provide the solutions unless I am an instructor which is pretty lame in my opinion. It probably has to do with the fact that this is an academic text.