sign up I login
 advanced
refer a friend - earn nickels!!

Ask & Discuss Questions with Community & Experts

Moderation Team
Ask iit jee aieee pet cbse icse state board community Discussion Response Post to: C++......
Forum Index -> Games, Puzzles and Quizzes -> View Full Question like the article? email it to a friend.  
Author Message
antonyajay21 (509)

Blazing goIITian

Olaaa!! Perrrfect answer. 93  [115 rates]

antonyajay21's Avatar

total posts: 334    
offline Offline
#include <iostream.h>
#include <string.h>

class String {
   char *char_ptr;   // pointer to string contents
   int length;       // length of string in characters
public:
   // three different constructors
   String(char *text);           // constructor using existing string
   String(int size = 80);        // creates default empty string
   String(String& Other_String); // for assignment from another
                                 // object of this class
   ~String() {delete char_ptr;};
   int Get_len (void);
   void Show (void);
};

String::String (char *text)
{
   length = strlen(text);  // get length of text
   char_ptr = new char[length + 1];
   strcpy(char_ptr, text);
};

String::String (int size)
{
   length = size;
   char_ptr = new char[length+1];
   *char_ptr = '\0';
};

String::String (String& Other_String)
{
   length = Other_String.length;       // length of other string
   char_ptr = new char [length + 1];   // allocate the memory
   strcpy (char_ptr, Other_String.char_ptr); // copy the text
};

int String::Get_len(void)
{
   return (length);
};

void String::Show(void)
{
   cout << char_ptr << "\n";
};

main ()                                // test the functions
{
   String AString ("Allocated from a constant string.");
   AString.Show();

   String BString;             // uses default length
   cout << "\n" << BString.Get_len() << "\n" ; //display length
   BString = "This is BString";

   String CString(BString);    // invokes the third constructor
   CString.Show();             // note its contents
}


Be not afraid of growing slowly.
Be afraid only of standing still.

 this reply: 5 points  (with Olaaa!! Perrrfect answer.   in 1 votes )   [?]
 
You have to be logged on to rate
  
 

Top Offers for goIITians
Correspondence Courses
Brilliant Tutorials
Narayana Institute
Aakash Institute
Classroom/Crash Courses
Narayana - Kota , Delhi , Others
Brilliant Tutorials - Class , Crash
Aakash Institute - Medical , Engg
Online Test Series
Brilliant Tutorials
Narayana Institute
Aakash Institute
Mahesh Tutorials
AMITY      Sri Chaitanya