Inheritance & Derived Classes

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2005
Posts: 14
Reputation: dallin is an unknown quantity at this point 
Solved Threads: 0
dallin dallin is offline Offline
Newbie Poster

Inheritance & Derived Classes

 
0
  #1
Mar 12th, 2005
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.



  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Inheritance & Derived Classes

 
0
  #2
Mar 12th, 2005
Is there any reason you're using inheritance when a few non-member functions for String would work just as well with less work?
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 14
Reputation: dallin is an unknown quantity at this point 
Solved Threads: 0
dallin dallin is offline Offline
Newbie Poster

Re: Inheritance & Derived Classes

 
0
  #3
Mar 12th, 2005
It's a class assignment that requires me to use derived classes.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 14
Reputation: dallin is an unknown quantity at this point 
Solved Threads: 0
dallin dallin is offline Offline
Newbie Poster

Re: Inheritance & Derived Classes

 
0
  #4
Mar 12th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2383 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC