Several basic functions of the Simple String class "C + +"

Source: Internet
Author: User
Tags assert
Implementation of several basic functions of string # include <iostream> #include <assert.h> #include <string.h>using namespace std; Class String{public:string () {_str = new char[1];_str[0] = ' + ';} String (char *str) {assert (str! = NULL); _str = new Char[strlen (str) + 1];strcpy (_str, str);} String (const string& s) {_str = new Char[strlen (S._STR) + 1];strcpy (_str, s._str);} string& operator= (const string& s) {if (this = &s) {delete[] _str;_str = new Char[strlen (S._STR) + 1];strcpy (_s TR, s._str);} return *this;} ~string () {delete[] _str;} Public:void Getstr () {cout << _str << Endl;} Private:char *_str;}; int main () {String s;s.getstr (); String S1 ("123"); S1.getstr (); String s2 = S1;s2.getstr (); s = S1;s.getstr (); return 0;}






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Several basic functions of the Simple String class "C + +"

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.