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.
/********************************************************************
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>
usingnamespace 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)
{
returntrue;
}
else
{
returnfalse;
}
}
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.
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
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.
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?
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.
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: