943,969 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2875
  • C++ RSS
Mar 12th, 2005
0

Inheritance & Derived Classes

Expand Post »
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.



C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. #include <cstring> //for strcpy(), etc.
  4. #include <conio.h> //For getch()
  5. ////////////////////////////////////////////////////////////////
  6. class String //user-defined string type
  7. {
  8. protected:
  9. enum { SZ = 80 }; //size of all String objects
  10. char str[SZ]; //holds a C-string
  11. public:
  12. String() //no-arg constructor
  13. { str[0] = '\0'; }
  14. String( char s[] ) //1-arg constructor
  15. { strcpy(str,s); } //convert C-string to String
  16. void display() //display the String
  17. { cout << str; }
  18. operator char*()
  19. { return str;}
  20. };
  21.  
  22. class Pstring : public String
  23. {
  24. public:
  25. Pstring(char s[]): String(s)
  26. { if(strlen(s)>(SZ-1)){
  27. strncpy(str,s,79);
  28. str[79] = '\0';
  29. }
  30. else
  31. String(s);
  32. }
  33. };
  34.  
  35. //class Pstring2 : public Pstring
  36. //{
  37. // public:
  38. // Pstring2() : Pstring
  39.  
  40.  
  41.  
  42. //};
  43. ////////////////////////////////////////////////////////////////
  44. int main()
  45. {
  46. cout << "THIS SENTENCE IS SHORTER THAN SZ:" << endl;
  47. Pstring2 s1 = "Never read the instructions.";
  48. s1.display();
  49. cout << endl <<"THIS SENTENCE IS LONGER THAN SZ:" << endl;
  50. Pstring2 s2 = "I would have to say that the greatest single achievement "
  51. "of the American medica";
  52. s2.display();
  53. cout << endl <<"THIS SENTENCE DEMONSTRATES THE LEFT, MID AND RIGHT FUNCTIONS:" << endl;
  54. Pstring2 s3 = "A long time ago in a galaxy far, far away.";
  55. s3.display();
  56.  
  57.  
  58.  
  59. getch();
  60. return 0;
  61. }


<< moderator edit: added [code][/code] tags >>
Last edited by Dave Sinkula; Mar 12th, 2005 at 6:43 pm. Reason: Added code tags.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
dallin is offline Offline
14 posts
since Jan 2005
Mar 12th, 2005
0

Re: Inheritance & Derived Classes

Is there any reason you're using inheritance when a few non-member functions for String would work just as well with less work?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 12th, 2005
0

Re: Inheritance & Derived Classes

It's a class assignment that requires me to use derived classes.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
dallin is offline Offline
14 posts
since Jan 2005
Mar 12th, 2005
0

Re: Inheritance & 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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
dallin is offline Offline
14 posts
since Jan 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: prime numbers homework trouble
Next Thread in C++ Forum Timeline: Question about comparing a string with a character





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC