How to access member variables in an STL string class?

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... // #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 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.

+8
c ++ string class
source share
4 answers

You should not inherit from std :: string, since it was not intended for this, and you do not need to find the palindrome.

See the following: Inheriting and overriding std :: string?

Palindrome solution (from this question: Check if the string is a palindrome . Related to this: Search engine optimization Palindrome <+ a>)

 #include <algorithm> bool isPal(const string& testing) { return std::equal(testing.begin(), testing.begin() + testing.size() / 2, testing.rbegin()); } 

The quality of this book seems dubious. Free functions (depending on who you ask) are almost always preferable to member functions and are especially preferable to inheritance.


If you must use inheritance:

 class Pstring : public string { //... bool isPalindrome() { return std::equal(begin(), begin() + size() / 2, rbegin()); // as a side-note, 'iterator' will refer to the inherited return type of begin() // Also, 'operator[](x)' will call the subscript operator } }; 
+3
source share

There was no auto in the book because this keyword was recently added to the language. If your compiler is older than a year or is not one of the big names, it probably does not support it.

For this problem, you do not need to refer to any member variables for the correct solution, so there is no need to worry about having them, or they are available. Well, because none of this is specified by the standard - all the implementation details defined by your particular compiler, and if you find yourself deeply versed in this, you should ask yourself what you are doing wrong.

Of course, the member functions of the parent class are available exactly like the member functions of the child class - you just call them.

Operator-operator overloads are a bit more complicated, but still not so bad. You need to provide an instance for the call that *this is against. You can also call them the operator keyword, but in my opinion a little awkward.

 if ((*this)[i] == (*this)[j]) if (operator[](i) == operator[](j)) 
+1
source share

If you do not want to use auto, then you can just use std::string::iterator , which means that auto is allowed anyway.

Thus, task No. 1 is fulfilled.


When you call begin() and end() , you call the members begin() and end() in the superclass std :: string.

Thus, task No. 2 is performed.

0
source share

Try the following:

 #include <string> class Pstring : public std::string { public: Pstring(const std::string &text) : std::string(text) { } bool isPalindrome() { std::string::size_type len = length(); std::string::size_type half = len / 2; for (std::string::size_type idx = 0; idx < half; ++idx) { if ((*this)[idx] != (*this)[len-idx-1]) return false; } return true; } }; 
0
source share

All Articles