| | |
Inheritance & Derived Classes
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2005
Posts: 14
Reputation:
Solved Threads: 0
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.
<< moderator edit: added [code][/code] tags >>
C++ Syntax (Toggle Plain Text)
#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 >>
Last edited by Dave Sinkula; Mar 12th, 2005 at 6:43 pm. Reason: Added code tags.
![]() |
Similar Threads
- what do i really need to do ? (C++)
- C ++ Class members (C++)
- Base and derived classes (C++)
- Header organization (base and derived classes) (C++)
- Need example of how to use INHERITANCE! (C++)
Other Threads in the C++ Forum
- Previous Thread: prime numbers homework trouble
- Next Thread: Question about comparing a string with a character
Views: 2383 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






