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 >>

Recommended Answers

All 3 Replies

Is there any reason you're using inheritance when a few non-member functions for String would work just as well with less work?

It's a class assignment that requires me to use derived classes.

No need to reply to my question. I have been working on it feverishly and finally got it!! It works!! -- that is a wonderful feeling.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.