Or is it my code? Can someone compile this and let me know if it works for them?
I'm getting errors saying it can't access the protected members in the String class.
/* String.cpp
Program: convert between ordinary strings to special protected strings class Pstring2 with substring functions to fetch a part of the string.
*/
#include <iostream>
#include <cstring>
usingnamespace std;
////////////////////////////////////////////////////////////////////////////////
class String //user-defined string type
{
protected:
enum {SZ = 80}; //size of all String objects
char str[SZ]; //holds a C-string
public:
String() //no-arg constructor
{
str[0] = '\0'; //initialize String with a NULL character
}
String(char s[]) //1-arg constructor
{
strcpy(str, s); //convert C-string to String
}
void display() const //display the String
{
cout<< str;
}
operatorchar*() //conversion operator
{
return str; //convert String to C-string
}
}; //End of class String
////////////////////////////////////////////////////////////////////////////////
class Pstring : public String //user-defined derived protected string type
{
public:
Pstring():String() //no-arg constructor
//with call to String class constructor in intializer list
{}
Pstring(char s[]) //one-arg constructor
{
int i;
for(i=0;(i<(SZ-1))&&(s[i]!='\0');i++) //copy first SZ-1 characters into str
{ // or all characters upto NULL character if the string is smaller than SZ.
str[i]=s[i];
}
str[i]='\0'; // Append NULL character to the end of string.
}
};
////////////////////////////////////////////////////////////////////////////////
class Pstring2 : public Pstring //user-defined derived protected string type with substring functions
{
public:
Pstring2():Pstring() //no-arg constructor
//with call to Pstring class constructor in initializer list
{}
Pstring2(char s[]):Pstring(s) //one-arg constructor
//with call to Pstring class one-arg constructor in initializer list
{}
protected:
void left(Pstring2 text, int n) //Fetch the n leftmost characters from text
{
int i;
for(i=0;i<n;i++)
str[i]=text.str[i]; //copy first n characters into str
str[i]='\0'; //append NULL character at the end of str
}
void mid(Pstring2 text,int s, int n) //Fetch n characters from text starting from s position
{
int i;
for(i=0;i<n;i++)
str[i]=text.str[s+i]; //copy n characters into str starting from s position
str[i]='\0'; //append NULL character at the end of str
}
void right(Pstring2 text, int n) //fetch the n rightmost characters from text
{
int j;
for(j=0;text.str[j]!='\0';j++); //count total characters in Pstring2 text
for(int i=j-n;i<j;i++) //loop from j-n position till j position
str[i-j+n]=text.str[i]; //copy last n characters into str
str[n]='\0'; //append NULL character at the end of str
}
};
////////////////////////////////////////////////////////////////////////////////
int main()
{
cout<<"TEST 1 : "<<"THIS SENTENCE IS SHORTER THAN SZ:"<<endl;
Pstring2 s1; //use no-arg constructor
//create and initialize C-string
char xstr[] = "Hello world!";
s1 = xstr; //use 1-arg constructor to convert C-string to String
s1.display(); //display String
cout<<endl << endl;
cout<<"TEST 2: "<<"THIS SENTENCE IS LONGER THAN SZ:"<<endl;
Pstring2 s2 = "Lets see if this larger than SZ characters string would be copied to a string of SZ characters."; //uses 1-arg constructor to initialize String
cout << static_cast<char*>(s2); //use conversaion operator
cout << endl << endl; //to convert String to C-string before sending to << op
cout<<"TEST 3: "<<"THIS SENTENCE DEMONSTRATES THE LEFT, MID AND RIGHT FUNCTIONS:"<<endl;
Pstring2 s3 = "A long time ago in a galaxy far, far away."; //uses 1-arg constructor to initialize String
cout << static_cast<char*>(s3); //use conversaion operator
cout << endl << endl; //to convert String to C-string before sending to << op
cout<<"TEST 3 (i): "<<"THE LEFTMOST 10 CHARACTERS ARE:"<<endl;
s1.left(s3,10); //fetch first 10 characters from Pstring2 s3
cout << static_cast<char*>(s1); //use conversaion operator
cout<<endl << endl; //to convert String to C-string before sending to << op
cout<<"TEST 3 (ii): "<<"THE SUBSTRING BEGINNING IN POSITION 16 FOR A LENGTH OF 10 IS:"<<endl;
s1.mid(s3,16,10); //fetch 10 characters from Pstring2 s3 starting from position 16
cout << static_cast<char*>(s1); //use conversaion operator
cout<<endl << endl; //to convert String to C-string before sending to << op
cout<<"TEST 3 (iii): "<<"THE RIGHTMOST 14 CHARACTERS ARE:"<<endl;
s1.right(s3,14); //fetch last 14 characters from Pstring2 s3
cout << static_cast<char*>(s1); //use conversaion operator
cout<<endl << endl; //to convert String to C-string before sending to << op
system("pause");
return 0;
}