I am working on one of the programming problems in the book “Starting with C ++ Early Objects 7th Edition,” and one of the tasks is to create a class that is derived from the STL string class. I am posting a question in order to understand what I am allowed to do and how I should implement the solution so that no one offers more advanced offers.
- The question is, as it is written in the text -
Palindrome testing
A palindrome is a line that reads the same backward as forward. For example, the words mom, dad, madame and radar are palindromes. Write a class Pstring , which is derived from the STL string class . Pstring class adds a member function
bool isPalindrome()
which determines if the string is a palindrome. Include a constructor that takes an STL string object as a parameter and passes it to the constructor of the base string class. Test your class by specifying the main program that asks the user to enter a string. The program uses the string to initialize the Pstring object, and then calls isPalindrome () to determine if the string entered is a palindrome.
You may find it useful to use the index operator [] for a string class: if str is a string object and k is an integer, then str [k] returns caracter at position k in the string.
- The end -
My main question is: how can I access the member variable that contains my string object if the class that I get Pstring is a class that I did not write, and I do not know how it implements its elements?
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... //
The reason I feel I am doing it wrong is because assignment mentions the use of index notation, however I don't understand how to use index notation if I don't know the variable name where the string is stored.
Any help would be greatly appreciated, because the author does not provide a solution unless I am a teacher who, in my opinion, is rather lame. This is probably due to the fact that this is an academic text.
c ++ string class
user1114264
source share