Accessing an STL base class member variable

Hello, I am having some issues understanding the steps necessary to access the member variable from a class I can not access. I understand one can make member protected in the base class, which will allow for the derived class to access those members. For this practice program I am task with having the string as the base class, but pretty confused with how to access the data given the assignment constraints.

Here is the code and assignment to better communicate my issue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  /********************************************************************
	Palindrome Testing

	A palindrome is a string that reads the same backward as forward. For example, the words mom,  dad, 
	 madam  and  radar are all 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 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 character at position  k in the string.
*********************************************************************/
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Pstring: public string
{
	public:
		Pstring(string);
		bool isPalindrom();
};


Pstring::Pstring(string word)
{
	string pass(word);
}
bool Pstring::isPalindrom()
{
	string pass;
	string compare;
	int size = pass.size();
	int index = size - 1;
	vector <char> temp;

	while (index >= 0)
	{
		temp.push_back(pass[index]);
		index--;
	}

	for (int x = 0; x < size; x++)
	{
		compare += temp[x];
	}

	if (compare == pass)
	{
		return true;
	}
	else
	{
		return false;
	}
}


int main()
{
	string test;

	do
	{
		cout << "Enter a string/word: (Enter -1 to exit) ";
		cin >> test;

		Pstring check(test);
		
		cout << check.isPalindrom() << endl << endl;
	} while (test != "-1");

	return 0;
}
When you inherit from a class publically, you may use all members of that class that are either protected or public. For your situation, you only need to access public members of the class.

1
2
3
4
5
bool Pstring::isPalindrom()
{
    string reversed(rbegin(), rend());
    return reversed == *this ;
}
Inheriting from string is generally considered a bad idea. Does your assignment explicitly tell you to inherit from string?
@cire - Very elegant, although I doubt that is what the author of the problem had in mind.

@Moschops - From line 5 of the OP.
Write a class Pstring that is derived from the STL string class.
Unfortunately, it does; the comment at the top of the code says:
Write a class Pstring that is derived from the STL string class.
still no luck. I utilized you idea cire, but the value is not being passed.
The goal is to ask the user for a string value, pass that to the Pstring constructotr, which will then pass it to the string construct. The issue happens, when trying to access, that value and use it for some constraints in the isPalidrom function. I know that there are many different and easier ways of doing something like this, but just following the assignment to further grasp some concepts.

By the way here is the output from using your idea:


Enter a string/word: (Enter -1 to exit) test

1

Enter a string/word: (Enter -1 to exit) testing

1

Enter a string/word: (Enter -1 to exit)



as you can nothing is being passed, which in return is saying is true
I don't see anywhere in your class that you set the actual value of the string. The actual input word.
The goal is to ask the user for a string value, pass that to the Pstring constructotr, which will then pass it to the string construct.

The constructor you supplied passes nothing to the std::string constructor as it should. It does, however, create a variable local to the constructor which stops existing when the constructor exits.

1
2
3
Pstring::Pstring(string word) : string(word)
{
}
I did a test run with doing a seperate member for the Pstring class, but from following the assignment details they just ask for the constructor and isPalidrom functions.

To my understanding, since Pstring is derived from the string STL it is using a member variable from string. But how do I access which ever value that is?
But how do I access which ever value that is?

The instructions give you a strong hint:
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 character at position k in the string.

http://www.cplusplus.com/reference/string/string/operator[]/

You do not need direct access to the internal representation of std::string.
Last edited on
Read cire's response. That's the biggest problem with your code.

Your isPalindrome() method is more complicated than it needs to be. Just compare the characters of the string directly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <iomanip>

bool palindrome( const std::string& str ) // do we need a class for this?
{

    std::string pal ; // ignore punctuation, white space and case
    for( char c : str ) if( std::isalnum(c) ) pal += std::tolower(c) ;

    // http://en.cppreference.com/w/cpp/algorithm/equal
    return std::equal( pal.begin(), pal.begin() + pal.size()/2, pal.rbegin() ) ;
}

struct pstring : std::string
{
    using std::string::string ; // http://www.stroustrup.com/C++11FAQ.html#inheriting
    bool is_palindrome() const { return ::palindrome(*this) ; } // *** make it const-correct
};

int main ()
{
    const pstring pstr = "A man, a plan, a canal - Panama!" ;
    std::cout << std::quoted(pstr) << "\npalindrome? " << std::boolalpha << pstr.is_palindrome() << '\n' ;
}

http://coliru.stacked-crooked.com/a/3b8b87dfc1e5fc8c
Sorry for the late response, but thank you for the help everyone. The program is now working as intended. Here is where the issue was.


The constructor you supplied passes nothing to the std::string constructor as it should. It does, however, create a variable local to the constructor which stops existing when the constructor exits.


Need to further understand the member initializer list.

My function works correct, but was curious to know what you mean by this
Just compare the characters of the string directly.


I assume you mean:
if last element of string == first element of string continue comparing else return false
else if second to last element of string == second element of string continue comparing, etc
etc
etc

Is that correct, or are you relating to something else?
Here is what I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool Pstring::isPalindrom()
{
	int size = this->size();
	string compare;
	int index = size - 1;
	vector <char> temp;

	while (index >= 0)
	{
		temp.push_back((*this)[index]);
		index--;
	}

	for (int x = 0; x < size; x++)
	{
		compare += temp[x];
	}

	return compare == *this;
}

Last edited on
My function works correct, but was curious to know what you mean by this
Just compare the characters of the string directly.
1
2
3
4
5
6
7
size_t mid= size()/2;
for (size_t i = 0; i < mid; ++i) {
    if ((*this)[i] != (*this)[size()-1]) {
        return false;
    }
}
return true;
ok thanks , will keep that in mind.
Topic archived. No new replies allowed.