954,168 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Inheritance & Derived Classes

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

dallin
Newbie Poster
14 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

dallin
Newbie Poster
14 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

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.

dallin
Newbie Poster
14 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You