小白学习
1、可重载的运算符
2、重载自增或自减++ - -运算符
operator++
编译器看到++a(前置++),它就调用operator++(a),当编译器看到a++(后置++),它就会去调用operator++(a,int).
#include <iostream>
using namespace std;
class Data
{
friend ostream& operator<<(ostream &out, Data &ob);
private:
int a;
int b;
public:
Data()
{
cout<<"无参的构造函数"<<endl;
a = 0;
b=0;
}
Data(int a,int b):a(a),b(b)
{
cout<<"有参构造"<<endl;
//this->a = a;
//this->b = b;
}
void showData(void)
{
cout<<"a = "<<a<<", b= "<<b<<endl;
}
~Data()
{
cout<<"析构函数函数"<<endl;
}
//成员函数 重载前置++ ++ob1 (先加 后使用)
//编译器 默认识别 operator++(a) //但是a可以用this代替 从而化简 operator++()
Data& operator++()//++ob1
{
//先加
a++;//this->a = this->a +1
b++;//this->b = this->b +1
//后使用
return *this;
}
//成员函数 重载后置++ ob1++ (先使用 后加)
//编译器 默认识别 operator++(a,int) //但是a可以用this代替 从而化简 operator++(int)
Data& operator++(int)//ob1++
{
//先使用(备份加之前的值)
static Data old=*this;
//后加
a++;
b++;
//返回备份值
return old;
}
//重载前置-- --ob3
//编译器 默认识别 operator++(a) //但是a可以用this代替 从而化简 operator--()
Data& operator--()
{
//先减
a--;
b--;
//后使用(返回)
return *this;
}
//重载后-- ob4--
//编译器 默认识别 operator++(a,int) //但是a可以用this代替 从而化简 operator++(int)
Data& operator--(int)
{
//先使用
static Data old=*this;
//再减
a--;
b--;
return old;
}
};
//普通全局函数 作为类的友元 重载<<运算符
ostream& operator<<(ostream &out, Data &ob)
{
out<<"a = "<<ob.a<<", b = "<<ob.b;
return out;
}
void test01()
{
Data ob1(10,20);
ob1.showData();
//重载<<直接输出自定义对象的值
//operator<<(cout,ob1);
cout<<ob1<<endl;
//成员函数 重载 ++运算符
cout<<++ob1<<endl;
Data ob2(10,20);
cout<<ob2++<<endl;
cout<<ob2<<endl;
//成员函数 重载 --运算符
Data ob3(10,20);
cout<<"ob3 "<<ob3<<endl;
cout<<--ob3<<endl;
Data ob4(10,20);
cout<<"ob4 "<<ob4<<endl;
cout<<ob4--<<endl;
cout<<"ob4 "<<ob4<<endl;
}
int main(int argc, char *argv[])
{
test01();
return 0;
}
3、指针运算符* ->
#include <iostream>
using namespace std;
class Person
{
private:
int num;
public:
Person(int num):num(num)
{
//this->num = num;
cout<<"有参构造num = "<<num<<endl;
}
void showPerson(void)
{
cout<<"num = "<<num<<endl;
}
~Person()
{
cout<<"析构函数 num = "<<num<<endl;
}
};
//设计一个智能指针 解决 Person new出的堆区空间 释放问题
class SmartPointer{
public:
Person *pPerson;
public:
SmartPointer(Person *p)
{
pPerson = p;
}
~SmartPointer()
{
if(pPerson != NULL)
{
delete pPerson;
pPerson = NULL;
}
}
//成员函数重载->运算符
Person* operator->()
{
return this->pPerson;
}
//成员函数 重载 *运算
Person& operator*()
{
return *(this->pPerson);
}
};
void test01()
{
Person *p = new Person(100);
p->showPerson();
//假如我忘了 delete p
//delete p;
//需求:自动的帮我释放 堆区空间(智能指针的概念)
SmartPointer pointer(new Person(200));
//访问Person类中的showPerson()
//pointer.pPerson->showPerson();
//保证指针的使用
//(pointer.operator ->())->showPerson();
pointer->showPerson();
(*pointer).showPerson();
}
int main(int argc, char *argv[])
{
test01();
return 0;
}
4、重载=运算符
前提1:类中 没有指针成员 不需要重载=运算符(默认的浅拷贝就可以完成)
#include <iostream>
using namespace std;
class Person
{
private:
int a;
int b;
public:
Person():a(0),b(0)
{
cout<<"无参构造"<<endl;
}
Person(int a, int b):a(a),b(b)
{
cout<<"有参构造"<<endl;
}
void showPerson(void)
{
cout<<"a = "<<a<<", b = "<<b<<endl;
}
~Person()
{
cout<<"析构函数"<<endl;
}
};
void test01()
{
Person ob1(10,20);
ob1.showPerson();
//注意 旧对象 给新对象赋值 调用的是拷贝构造(默认拷贝构造就是单纯的赋值)
Person ob2 = ob1;//这个地方 可不是调用赋值=运算符
ob2.showPerson();
Person ob3;
ob3 = ob1;//此处才是调用的赋值=运算符(默认赋值=运算是浅拷贝)
ob3.showPerson();
}
int main(int argc, char *argv[])
{
test01();
return 0;
}
前提2:类中 有指针成员 必须重载=运算符
指针作为类的成员:
1、拷贝构造函数 必须自定义(默认拷贝构造 是浅拷贝)
2、必须重载=运算符 (默认=号运算符 是浅拷贝)
#include <iostream>
#include<string.h>
using namespace std;
class Person
{
private:
char *name;//指针成员
public:
Person()
{
name = NULL;
cout<<"无参构造"<<endl;
}
Person(char *name)
{
//根据实际传入的 参数 给this->name申请空间
this->name = new char[strlen(name)+1];
//将name指向的字符串 拷贝到 this->name指向的空间中
strcpy(this->name,name);
cout<<"有参构造"<<endl;
}
Person(const Person &ob)//ob代表的就是旧对象
{
//this代表的是新对象
cout<<"拷贝构造函数"<<endl;
this->name = new char[strlen(ob.name)+1];
strcpy(this->name, ob.name);
}
~Person()
{
cout<<"析构函数"<<endl;
if(this->name != NULL)
{
delete [] this->name;
this->name = NULL;
}
}
void showPerson(void)
{
cout<<"name = "<<name<<endl;
}
//成员函数 重载=运算符
Person& operator=(Person &ob)//ob == ob1
{
//this ==>&ob3
if(this->name != NULL)//说明this->name 以前有指向(重点)
{
//释放以前指向的空间
delete [] this->name;
this->name = NULL;
}
//申请空间
this->name = new char[strlen(ob.name)+1];
//拷贝内容
strcpy(this->name,ob.name);
return *this;//重点
}
};
void test01()
{
Person ob1("lucy");
ob1.showPerson();
Person ob2 = ob1;//调用拷贝构造
Person ob3("bob");
//不重载 = 默认是浅拷贝
ob3 = ob1;
ob3.showPerson();
Person ob6,ob5,ob4;
ob6 = ob5 = ob4 = ob1;
ob6.showPerson();
}
int main(int argc, char *argv[])
{
test01();
return 0;
}
5、等于和不等于(==、!=)运算符重载
//重载== ==出现在判断语句中
bool operator==(Person &ob)
{
if(strcmp(this->name, ob.name) == 0)
{
return true;
}
return false;
}
//重载!= !=出现在判断语句中
bool operator!=(Person &ob)
{
if(strcmp(this->name, ob.name) != 0)
{
return true;
}
return false;
}
6、函数调用符()的重载
#include <iostream>
using namespace std;
class Fun
{
public:
int my_add(int x,int y)
{
return x+y;
}
//重载()
//第一个()是重载的符号 第二个()是标明要传参
int operator()(int x,int y)
{
return x+y;
}
};
void test01()
{
Fun fun;
cout<<fun.my_add(100,200)<<endl;
cout<<fun.operator ()(100,200)<<endl;
//优化 fun和()结合 就会自动寻找()运算符
cout<<fun(100,200)<<endl;
//此处 fun(100,200)不是一个真正的函数 仅仅是一个对象名和()结合 调用()重载运算符而已
//fun不是函数名 只是fun(100,200)类似一个函数调用 所以将fun(100,200)叫做仿函数
//此处的Fun是类名称
//Fun()匿名对象 Fun()(100,200) 就是匿名对象(100,200)
cout<<Fun()(100,200)<<endl;//了解
}
int main(int argc, char *argv[])
{
test01();
return 0;
}
7、不要重载&&、||
不要重载&&、|| 因为 用户无法实现 && ||的短路特性。
&& 短路特性: A && B 如果A为假 B将不会执行
|| 短路特性: A || B 如果A为真 B将不会执行