C + + Primer learning note _27_ operator overloading and conversion (2)--++/--operator overloading,! operator overloading, assignment operator overloading, String Class ([], +, + = operator overloading), >> and << operator overloading
One, ++/--operator overloading
1, front + + operator overloading
The way the member function is overloaded, the prototype is:
Function type & operator ++ ();
The friend function is overloaded. The prototype is:
friend function type & operator ++ (class type &);
2.Post ++ operator overloading
The member function is overloaded. The prototype is:
Function type operator ++ (int);
The friend function is overloaded. The prototype is:
friend function type operator ++ (class type &, int);
3.Prefix--operator overloading
The member function is overloaded. The prototype is:
Function type & operator-();
The friend function is overloaded. The prototype is:
friend function type & operator-(class type &);
4.Post --- operator overloading
The member function is overloaded. The prototype is:
Function type operator-(int);
The friend function is overloaded. The prototype is:
friend function type operator-(class type &, int);
#include <iostream>
using namespace std;
class Integer
{
public:
Integer (int n);
~ Integer ();
Integer & operator ++ ();
// friend Integer & operator ++ (Integer & i);
Integer operator ++ (int n);
// friend Integer operator ++ (Integer & i, int n);
Integer & operator-();
// friend Integer & operator-(Integer & i);
Integer operator-(int n);
// friend Integer operator-(Integer & i, int n);
void Display () const;
private:
int n_;
};
Integer :: Integer (int n): n_ (n) {}
Integer :: ~ Integer () {}
Integer & Integer :: operator ++ ()
{
++ n_;
return * this;
}
// Integer & operator ++ (Integer & i)
// {
// ++ i.n_;
// return i;
//}
Integer Integer :: operator ++ (int n)
{
// n _ ++;
Integer tmp (n_);
n _ ++;
return tmp;
}
// Integer operator ++ (Integer & i, int n)
// {
// Integer tmp (i.n_);
// i.n _ ++;
// return tmp;
//}
Integer & Integer :: operator-()
{
--n_;
return * this;
}
// Integer & operator-(Integer & i)
// {
// --i.n_;
// return i;
//}
Integer Integer :: operator-(int n)
{
// n _--;
Integer tmp (n_);
n _--;
return tmp;
}
// Integer operator-(Integer & i, int n)
// {
// Integer tmp (i.n_);
// i.n _--;
// return tmp;
//}
void Integer :: Display () const
{
cout << n_ << endl;
}
int main (void)
{
Integer n (100);
n.Display ();
cout << "++ stage" << endl;
Integer n2 = ++ n;
n.Display ();
n2.Display ();
Integer n3 = n ++;
n.Display ();
n3.Display ();
cout << "--stage" << endl;
Integer m (100);
Integer m2 = --m;
m.Display ();
m2.Display ();
Integer m3 = m--;
m.Display ();
m3.Display ();
return 0;
}
operation result:
100
++ stage
101
101
102
101
--stage
99
99
98
99
It should be noted that in order to distinguish it from the front ++, the rear ++ has an int parameter, but it has no effect. When you set a breakpoint to debug, you can find that the default assignment is 0. And member functions cannot coexist with friend functions at this time, because the ++ operator is not clear.
Second, assignment operator overloading,! Operator overloading
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
class CMyString
{
public:
CMyString (const char * pData = NULL);
CMyString (const CMyString & other);
~ CMyString ();
CMyString & operator = (const CMyString & str);
bool operator! () const;
void display () const;
private:
char * m_pData;
};
CMyString :: CMyString (const char * pData)
{
if (pData == NULL)
{
m_pData = new char [1];
* m_pData = '\ 0';
}
else
{
int length = strlen (pData);
m_pData = new char [length + 1];
strcpy (m_pData, pData);
}
}
CMyString :: CMyString (const CMyString & other)
{
int Len = strlen (other.m_pData);
m_pData = new char [Len + 1];
strcpy (m_pData, other.m_pData);
}
CMyString :: ~ CMyString ()
{
delete [] m_pData;
}
CMyString & CMyString :: operator = (const CMyString & str)
{
if (this == & str)
return * this;
delete [] m_pData;
m_pData = NULL;
m_pData = new char [strlen (str.m_pData) + 1];
strcpy (m_pData, str.m_pData);
return * this;
}
bool CMyString :: operator! () const
{
return strlen (m_pData)! = 0;
}
void CMyString :: display () const
{
cout << m_pData << endl;
}
int main ()
{
CMyString s1 ("abc");
CMyString s2;
s2 = s1;
s2.display ();
s2 = "def";
s2.display ();
CMyString s3;
bool noempty;
noempty =! s3;
cout << noempty << endl;
s3 = "abc";
noempty =! s3;
cout << noempty << endl;
return 0;
}
operation result:
abc
def
0
1
Explanation: Suppose there are three objects, str1, str2, str3. In the program, the statement str1 = str2 = str3 will fail to compile. The! Operator here means true when the string is not empty.
Third, the String class ([], +, + = operator overloading), >> and << operator overloading
In the following code, operator + calls the implementation of operator + =; the stream operator can only be overloaded as a friend function, because the first parameter is a stream reference, not a String class.
#include <iostream>
#include <string>
#include <string.h>
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_;
};
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 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)
{
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;
}
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.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;
}
operation result:
c
abAdefg
xyzabc
xxxyyy
aaaxxxsdfadfaxxxx
xxxyyy
xxxyyy
asdfasdf
asdfasdf
8
reference:
C ++ primer fourth edition
Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.
C ++ Primer study notes _27_ operator overloading and conversion (2)-++ /-operator overloading ,! Operator overloading, assignment operator overloading, String ([], +, + = operator overloading), >> and << operator overloading