Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I access the member variables in the STL string class?
    primarykey
    data
    text
    <p>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.</p> <p>-- Question as it is written in the text --</p> <p>Palindrome Testing</p> <p>A Palindrome is a string that reads the same backward as forward. For example, the words mom, <em>dad</em>, <em>madam</em>, and <em>radar</em> are palindromes. Write a <code>class Pstring</code> that is derived from the <code>STL string class</code>. The <code>Pstring class</code> adds a member function</p> <pre><code>bool isPalindrome() </code></pre> <p>that determines whether the string is a palindrome. Include a constructor that takes an <code>STL string</code> 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.</p> <p>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.</p> <p>-- End --</p> <p>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?</p> <p>For example,</p> <pre><code>#include &lt;string&gt; 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 &lt; iEnd &amp;&amp; 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. } } </code></pre> <p>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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload