目录
++运算符重载
前置++运算符重载
1.成员函数的方式重载,原型为:
函数原型 & operator ++();
2.友元函数的方式重载,原型为:
friend 函数原型 & operator ++(类类型 &);
后置自增和后置自减的重载
1.成员函数的方式重载,原型为:
函数原型 operator ++(int);
2.友元函数的方式重载,原型为:
friend 函数原型 operator ++(类类型 &, int);
Interger.h
#pragma once
class Interger
{
public:
Interger(int n);
Interger(const Interger& other);
~Interger();
void Display() const;
Interger& operator++();
Interger operator++(int);
private:
int n_;
};
Interger.cpp
#include "Interger.h"
#include <iostream>
using namespace std;
Interger::Interger(int n) : n_(n)
{
cout << "Interger..." << endl;
}
Interger::Interger(const Interger& other)
{
n_ = other.n_;
cout << "copy Interger..." << endl;
}
Interger::~Interger()
{
cout << "~Interger..." << endl;
}
void Interger::Display() const
{
cout << n_ << endl;
}
Interger& Interger::operator++() // 前置++
{
++n_;
return *this;
}
Interger Interger::operator++(int) // 后置++
{
Interger tmp(n_);
n_++;
return tmp;
}
main
#include "Interger.h"
#include <iostream>
using namespace std;
int main()
{
Interger n(100);
n.Display();
Interger n2 = ++n;
n.Display();
n2.Display();
Interger n3 = n++;
n.Display();
n3.Display();
return 0;
}
赋值运算符重载
String.h
#pragma once
class String
{
public:
explicit String(const char* str = "");
String(const String& other);
String& operator=(const String& other);
String& operator=(const char* str);
~String();
void Display() const;
private:
char* str_;
char* AllocAndCopy(const char* str);
};
String.cpp
#include "String.h"
#include <iostream>
#include <string.h>
using namespace std;
String::String(const char* str)
{
str_ = AllocAndCopy(str);
}
String::String(const String& other)
{
str_ = AllocAndCopy(other.str_);
}
String& String::operator=(const String& other)
{
if (this == &other) return *this;
delete[] str_;
str_ = AllocAndCopy(other.str_);
return *this;
}
String& String::operator=(const char* str)
{
delete[] str_;
str_ = AllocAndCopy(str);
return *this;
}
String::~String()
{
delete[] str_;
}
char* String::AllocAndCopy(const char* str)
{
int len = strlen(str) + 1;
char* newstr = new char[len];
memset(newstr, 0, sizeof(newstr));
strncpy(newstr, str, len);
return newstr;
}
void String::Display() const
{
cout << str_ << endl;
}
main
#include "String.h"
#include <iostream>
using namespace std;
int main()
{
String s1("AAA");
s1.Display();
String s2("BBB");
s2.Display();
String s3 = s2;
s3.Display();
String s4(s1);
s4.Display();
String s5;
s5.Display();
s5 = "xxxxxx";
s5.Display();
return 0;
}
运行结果:
!运算符重载
字符串为空,返回false;字符串不为空,返回true。
String.h
#pragma once
class String
{
public:
explicit String(const char* str = "");
String(const String& other);
String& operator=(const String& other);
String& operator=(const char* str); // xxxxxx
bool operator!() const;
~String();
void Display() const;
private:
char* str_;
char* AllocAndCopy(const char* str);
};
String.cpp
bool String::operator!() const
{
return (strlen(str_) != 0);
}
test.cpp
String s6;
bool notempty;
notempty = !s6;
cout << notempty << endl;
String s7("aaaaaa");
notempty = !s7;
cout << notempty << endl;
[]运算符重载
+运算符重载
+=运算符重载
流运算符
1.C++的I/O流库的一个重要特性就是能够支持新的数据类型的输出和输入。
2.用户可以通过对插入符(<<)和提取符(>>)进行重载来支持新的数据类型。
3.流运算符的重载只能使用友元函数进行重载(二元操作运算符)。
4. friend istream& operator >>(istream&, 类类型&);
5. friend ostream& operator <<(ostream&, const 类类型&);
<<运算符重载
>>运算符重载
String.h
#pragma once
#include <iostream>
using namespace std;
class String
{
public:
/*explicit*/ 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 istream& operator>>(istream& is, String& str);
friend ostream& operator<<(ostream& os, const String& str);
~String();
void Display() const;
private:
char* str_;
char* AllocAndCopy(const char* str);
};
String.cpp
#include "String.h"
#include <string.h>
String::String(const char* str)
{
str_ = AllocAndCopy(str);
}
String::String(const String& other)
{
str_ = AllocAndCopy(other.str_);
}
String& String::operator=(const String& other)
{
if (this == &other) return *this;
delete[] str_;
str_ = AllocAndCopy(other.str_);
return *this;
}
String& String::operator=(const char* str)
{
delete[] str_;
str_ = AllocAndCopy(str);
return *this;
}
bool String::operator!() const
{
return (strlen(str_) != 0);
}
char& String::operator[](unsigned int index)
{
//return str_[index];
//non const version 调用const version
return const_cast<char &>(static_cast<const String&>(*this)[index]);
}
const char& String::operator[](unsigned int index) const
{
return str_[index];
}
String operator+(const String& s1, const String& s2)
{
/*
int len = strlen(s1.str_) + strlen(s1.str_) + 1;
char* newstr = new char[len];
memset(newstr, 0, sizeof(newstr));
strncpy(newstr, s1.str_, strlen(s1.str_));
strcat(newstr, s2.str_);
String tmp(newstr);
delete newstr;
return tmp;
*/
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, sizeof(newstr));
strncpy(newstr, str_, strlen(str_));
strcat(newstr, other.str_);
delete [] str_;
str_ = (newstr);
return *this;
}
String::~String()
{
delete[] str_;
}
char* String::AllocAndCopy(const char* str)
{
int len = strlen(str) + 1;
char* newstr = new char[len];
memset(newstr, 0, sizeof(newstr));
strncpy(newstr, str, len);
return newstr;
}
void String::Display() const
{
cout << str_ << endl;
}
istream& operator>>(istream& is, String& str)
{
char tmp[1024];
cin >> tmp;
str = tmp;
return is;
}
ostream& operator<<(ostream& os, const String& str)
{
os << str.str_;
return os;
}
main
#include "String.h"
int main()
{
String s1("AAA");
s1.Display();
String s2("BBB");
s2.Display();
String s3 = s2;
s3.Display();
String s4(s1);
s4.Display();
String s5;
s5.Display();
s5 = "xxxxxx";
s5.Display();
String s6;
bool notempty;
notempty = !s6;
cout << notempty << endl;
String s7("abcdefg");
notempty = !s7;
cout << notempty << endl;
char ch = s7[2];
cout << ch << endl;
s7[2] = 'A';
s7.Display();
const String s8("uvwxyz");
ch = s8[2];
//s8[2] = 'M'; // 可以更改对象,存在隐患,所以返回const引用,使得编译不过
s8.Display();
s7.Display();
s8.Display();
String s9 = s7 + s8 + "23333" + "55556666";
s9.Display();
String s10 = "99999999"; //不声明为explicit可以使用
s10.Display();
s9 += s10;
s9.Display();
cout << s9 << endl;
String s11;
cin >> s11;
cout << s11 << endl;
return 0;
}
运行结果: