Eighth Week item four construction of the-string class

Source: Internet
Author: User

Please construct the add and subtract operations of the string class. where S1 + S2 connects two strings, S1-S2 is the connection that removes the trailing spaces of S1 and the leading spaces of the S2.
Tip: There are pointer members, which should be noted at design time. This, you know.

#include <iostream> #include <cstring>using namespace Std;class string{public:string ();    Default constructor String (const char *s);               String (string &str);    Constructor ~string ();    void display ();    Friend string operator + (string &s1,string &s2);    Friend string operator-(string &s1,string &s2);p Rivate:char *p;                                       int Len; A character pointer used to point to a string};    String::string () {len = 0; p = NULL;}    string::string (const char *s) {len = strlen (s);    p = new Char[len+1]; strcpy (p,s);}    String::string (String &str) {len = Str.len;   if (p!=null) delete []p;    When the assignment occurs, the original object may already exist, and the original space must be freed P = new char[len+1]; strcpy (P,STR.P);} String::~string () {if (!p) delete []p;} void string::d isplay ()//The string that the output p points to {Cout<<p<<endl;}    String operator + (string &s1, String &s2) {string S;    S.len = S1.len+s2.len;   S.P = new Char[s.len+1]; Original mis-write char (s.len+1)   strcpy (S.P,S1.P);    strcat (S.P,S2.P); return s;}    String operator-(string &s1, String &s2) {string S;    C1 string Char *c1=new char[s1.len+1] for the truncated trailing space;    strcpy (C1,S1.P);    int i=s1.len-1;    while (i>=0&&c1[i]== ")--I.;    c1[i+1]= ' + ';    C2 string char *c2=new char[s2.len+1] to remove leading whitespace;    strcpy (C2,S2.P);    i=0;    while (i<s2.len&&c2[i]== ') ++i;    int j=0;        while (i<s2.len&&c2[i]!= ') {c2[j]=c2[i];        ++i;    ++j;    } c2[j]= ' + ';    Connect the two parts S.len = strlen (c1) +strlen (C2);     S.P = new Char[s.len+1];    Original error Write char (s.len+1) strcpy (S.P,C1);    strcat (S.P,C2);    Delete C1;    Delete C2; return s;}    int main () {String string1 ("Hello"), string2 ("World");    String1.display ();    String2.display ();    String String3;    String3 = string1 + string2;    String3.display ();    String3 = string1-string2;    String3.display (); return 0;}


Operation Result:


Summary of Knowledge points:

To manipulate a string with a pointer

Please construct the add and subtract operations of the string class. where S1 + S2 connects two strings, S1-S2 is the connection that removes the trailing spaces of S1 and the leading spaces of the S2.
Tip: There are pointer members, which should be noted at design time. This, you know.
Learning experience:

Good study Day Day up

Eighth Week item four construction of the-string 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.