To learn the operator overloading of C ++ from scratch (3): Improve the string class ([], +, + = Operator overloading),> and

Source: Internet
Author: User

In the previous article, I used the string class for several times. Now I need to reload several operators to improve it and to reload the stream operators.

[] Operator Overloading

+ Operator Overloading

+ = Operator overload

<Operator overload
> Operator Overloading


String. h:

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Ifndef _ string_h _
# DEFINE _ string_h _
# Include <iostream>
Using namespace STD;

Class string
{
Public:
String (const char * STR = "");
String (const string & other );
String & operator = (const string & other );
String & operator = (const char * Str );

Bool Operator! () Const;
Char & operator [] (unsigned int index );
Const char & operator [] (unsigned int index) const;

Friend string operator + (const string & S1, const string & S2 );
String & operator ++ = (const string & other );

Friend ostream & operator <(ostream & OS, const string & Str );
Friend istream & operator> (istream & is, string & Str );
~ String (void );

Void display () const;
Int length () const;
Bool isempty () const;

PRIVATE:
String & assign (const char * Str );
Char * allocandcpy (const char * Str );
Char * STR _;
};

# Endif // _ string_h _

String. cpp:

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Pragma warning (Disable: 4996)
# Include "string. H"
# Include <string. h>
// # Include <iostream>
// Using namespace STD;

String: string (const char * Str)
{
STR _ = allocandcpy (STR );
}

String: string (const string & other)
{
STR _ = allocandcpy (other. Str _);
}

String & string: Operator = (const string & other)
{
If (this = & other)
Return * this;

Return assign (other. Str _);
}

String & string: Operator = (const char * Str)
{
Return assign (STR );
}

String & string: Assign (const char * Str)
{
Delete [] STR _;
STR _ = allocandcpy (STR );
Return * this;
}

Bool string: Operator! () Const
{
Return strlen (STR _)! = 0;
}

Char & string: operator [] (unsigned int index)
{
// Return STR _ [Index];
// The non-const version calls the const version.

Return const_cast <char &> (static_cast <const string &> (* This) [Index]);
}

Const char & string: operator [] (unsigned int index) const
{
Return STR _ [Index];
}

String ::~ String ()
{
Delete [] STR _;
}

Char * string: allocandcpy (const char * Str)
{
Int Len = strlen (STR) + 1;
Char * newstr = new char [Len];
Memset (newstr, 0, Len );
Strcpy (newstr, STR );

Return newstr;
}

Void string: Display () const
{
Cout <STR _ <Endl;
}

Int string: length () const
{
Return strlen (STR _);
}

Bool string: isempty () const
{
Return length () = 0;
}

String operator + (const string & S1, const string & S2)
{
// Int Len = strlen (s1.str _) + strlen (s2.str _) + 1;
// Char * newstr = new char [Len];
// Memset (newstr, 0, Len );
// Strcpy (newstr, s1.str _);
// Strcat (newstr, s2.str _);
//
// String TMP (newstr );
// Delete newstr;
String STR = S1;
STR + = S2;
Return STR;
}

String & string: Operator + = (const string & other)
{
Int Len = strlen (STR _) + strlen (other. Str _) + 1;
Char * newstr = new char [Len];
Memset (newstr, 0, Len );
Strcpy (newstr, STR _);
Strcat (newstr, other. Str _);

Delete [] STR _;

STR _ = newstr;
Return * this;
}

Ostream & operator <(ostream & OS, const string & Str)
{
OS <Str. Str _;
Return OS;
}

Istream & operator> (istream & is, string & Str)
{
Char TMP [1024];
Cin> TMP;
STR = TMP;
Return is;
}

Main. cpp:

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Include "string. H"
# Include <iostream>
Using namespace STD;

Int main (void)
{
String S1 ("abcdefg ");

Char CH = S1 [2];
Cout <ch <Endl;

S1 [2] = 'a ';
S1.display ();

Const string S2 ("xyzabc ");
Ch = S2 [2];
// S2 [2] = 'M'; Error
S2.display ();

String S3 = "XXX ";
String S4 = "yyy ";

String S5 = S3 + S4;
S5.display ();

String S6 = "AAA" + S3 + "sdfadfa" + "XXXX ";
S6.display ();

S3 + = S4;
S3.display ();

Cout <S3 <Endl;

String S7;
Cin> S7;
Cout <S7 <Endl; If (! S7.isempty ())
Cout <s7.length () <Endl;

Return 0;
}



Note that the constructor of the string class cannot be declared as explicit; otherwise, string S3 = "XXX ";
Compilation error. The non const version of operator [] calls the const version implementation. The static_cast and const_cast types of conversion operators are used. For details, refer to here; operator + calls the implementation of operator + =. You can only reload stream class operators as user functions, because the first parameter is a stream class reference, not a string class.

By implementing such a string class, we can be familiar with basic memory management and copy control.


Refer:

C ++ primer version 4
Valid tive C ++ 3rd
C ++ programming specifications

Related Article

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.