I'm stuck, I have a base class String, derived class Pstring, and trying to develop another derived class Pstring2. Pstring2 needs a function called left. Main will call the function by Text.left(Text1,N) // Text2 is assigned the leftmost N characters from Text1. I'm having a hard time understanding how to connect Pstring2 to Pstring and String base class. Left member function of Pstring2 will carve out the left most 10 characters assigned to an object. The code I have so far is noted below. Can you give me some help? Thanks.
#include <iostream>
using namespace std;
#include <cstring> //for strcpy(), etc.
#include <conio.h> //For getch()
////////////////////////////////////////////////////////////////
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'; }
String( char s[] ) //1-arg constructor
{ strcpy(str,s); } //convert C-string to String
void display() //display the String
{ cout << str; }
operator char*()
{ return str;}
};
class Pstring : public String
{
public:
Pstring(char s[]): String(s)
{ if(strlen(s)>(SZ-1)){
strncpy(str,s,79);
str[79] = '\0';
}
else
String(s);
}
};
//class Pstring2 : public Pstring
//{
// public:
// Pstring2() : Pstring
//};
////////////////////////////////////////////////////////////////
int main()
{
cout << "THIS SENTENCE IS SHORTER THAN SZ:" << endl;
Pstring2 s1 = "Never read the instructions.";
s1.display();
cout << endl <<"THIS SENTENCE IS LONGER THAN SZ:" << endl;
Pstring2 s2 = "I would have to say that the greatest single achievement "
"of the American medica";
s2.display();
cout << endl <<"THIS SENTENCE DEMONSTRATES THE LEFT, MID AND RIGHT FUNCTIONS:" << endl;
Pstring2 s3 = "A long time ago in a galaxy far, far away.";
s3.display();
getch();
return 0;
}
<< moderator edit: added [code][/code] tags >>