A bug fix in C + + write class

Source: Internet
Author: User

  #include "wz.h"   #include "sts.h" Class _string{    friend std::istream & operator>> (Std::istream& is, _string& a);//bug 1 2     friend std::ostream& operator<< (std::ostream& os,_string&  a);      public:    _string ()      / /default Constructors     {        length = 0;         b=new char[1];        b [0]= ';   }                 _string (char *a);    //Constructor     _string (Int n, Char a);     ~_string ();  //destructor     _string (_string & a);   //copy Constructor      int size () {return length;}   //gets the string length     _string operator+ (const _string& a);    //overloaded ' + ' operator     _string& operator+= (const _string& a);    //overloaded ' + = ' operator     _string& operator= (const _string&  a);    //overloaded assignment operator      int operator< (const _string  &a);    int operator> (const _string &a);     Char& operator[] ( int n);    //overloaded subscript operator     _string   substr (int pos,int n);    //returns a substring    _string  substr ( Int pos);    //return substring    private:    char *b;     int length;};  _string::_string (char *a) &NBSP;&NBsp;   //Constructor {    length = strlen (a);       b = new char[length+1];    for (int  i= 0;i< length;i++)     {        b[i] = a[i];     }    b[length] =  ' + ';}  _string::_string (Int n,char a) {    b=new char[n+1];     for (int  i= 0;i<n;i++)     {         b[i] =a;    }    b[n] =  ' + ';}  _string::~_string ()      //destructor {    delete []b;     length=0;}  _string::_string (_string &a)       //copy constructor {     length=a.size ();     b=New char [length+1];   for (int  i = 0;i<length;i++)      {        b[i] = a.b[i];     }    b[length] =  ' + ';}  _string  _string::operator+ (const _string& a)     //heavy +{     int newLen = length+a.length;    char *str;     str = new char[newlen+1];    int count =  0;    for (int i = 0;i<length;i++)     {         str[i] = this->b[i];         count++;    }         for ( int i =0;count<newlen;count++,i++)     {        str[i] = a.b[i];    }     str[newLen] =  '     _string temp ' (str);          return temp;}  _string&  _string:: operator+= (const _string& a)    //Reload + ={    int newlen = length+a.length;    char *str;     str = new char[newlen+1];    int count =  0;    for (int i = 0;i<length;i++)     {         str[i] = this->b[i];         count++;    }         for ( int i =0;count<newlen;count++,i++)     {        str[i] = a.b[i];    }    str[ newlen] =  ' + ';     _string temp (str);    *this=temp;     return *this;    }_string& _string:: operator= ( Const _string &a)     //overload =  {       if (this==&a)           return *this;        delete []b;      length = a.length;       b = new char[length+1];       for (int  i = 0;i<length;i++)     {         b[i] = a.b[i];    }    b[length] =   '   &nbs ';p;       return *this; }int _string:: operator< ( Const _string &a)       {     while  ( *THIS-&GT;B&NBSP;==&NBSP;*A.B) {if  (*this->b ==  '? ') {return 0;} this->b++;b++;} if  (*this->b - *a.b < 0) {return 1;}  else return 0;;     }int _string:: operator> (Const _string &a)        {     while  (*THIS-&GT;B&NBSP;==&NBSP;*A.B) {if  (* this->b ==  '? ') {return 0;} this->b++;b++;} if  (*this->b - *a.b >0) {return 1;}  else return 0;;   }   char& _string:: operator[] ( int n)   //overloaded subscript operator   {      if (n>length)           return b[length-1];      else           return b[n];  }   ostream&  operator<< (ostream& os, _string& a)   //overloaded output characters   {       os<<a.b<<endl;      return os;   }/*  istream& operator>> (Std::istream& is, _string& a)   //overloaded input   {  // 1 while (*a.b!= ')        // {is>>a.b ;}             //2  a.b=new char[256];       is>>a.b ;      a.length=strlen ( A.B);       return is;  } *///  5 istReam& operator>> (ISTREAM&NBSP;&AMP;IS,&NBSP;_STRING&NBSP;&AMP;STR)//input  {char tem[1000 ];  //simple application for a piece of memory  is.get (tem,1000, ' \ n '); Str.length = strlen (TEM); str.b = new  char[str.length + 1];strcpy (Str.b, tem); return is;}    _string  _string::substr (int pos, int n)     // Two substr functions that accept different parameters, return substrings   {      char *p = new  Char[n+1];      for (int i=0;i<n;i++)        {          p[i]=b[pos];           pos++;      }      p[n]= ' \ 0 ';       _string k (P);      k.length=n;       return  k;  }      _string  _string::substr (Int pos)  {      int len=length;     char *p=new char[len-pos+1 ];     int t=pos;     for (int i=0;t<len;t++,i++ )      {         p[i]=b[t];      }     p[t]= ' + ';       _string k ( P);      k.length=len-pos;      return   k; }int main  () {;                      _string a;cin>>a;cout<<a;return 0;  return 0;}

For the input to be able to take a space but there may be a bug that loses the first character

A bug fix in C + + write class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.