友元函数与操作符重载入门+const修饰this指针

先对比一下成员函数与全局函数的区别

先对比下全局函数与普通的成员函数
#include<iostream>

class Test
{
public:
	//理解this指针的关键,站在测试程序的角度去思考
	//什么对象调用这个函数,this指针就是谁
	Test(int a, int b)//有参构造函数
	{
		this->a = a;
		this->b = b;
	}
	int getA()
	{
		return a;
	}
	int getB()
	{
		return b;
	}
public:
	Test add(Test&t2)//this指针指向一个对象
	{   
		this->a += t2.getA();
		this->b += t2.getB();

		return *this;   //*操作等于取指针地址,也就是
	}
	Test add2(Test&t2)//this指针指向一个对象
	{     //t3是局部变量,add调用完了就会销毁,所以需要拷贝一个对象,匿名对象
		Test t3(this->a+ t2.getA(),this->b+ t2.getA());
		return t3;//使用匿名对象返回
	}public:
	void printAB()
	{
		std::cout << "a=" << a << " " << "b=" << b << std::endl;
	}
protected:
private:
	int a;
	int b;
};
//全局函数
//结论如果把全局函数转成成员函数,少了一个操作数,通过this指针被隐藏
Test add(Test &t1, Test&t2)
{
	Test t3(t1.getA() + t2.getA(), t1.getB() + t2.getA());
	return t3;
}
//把成员函数转为全局函数,需要多一个参数
void printAB(Test *pthis)
{
	std::cout << "a=" << pthis->getA() << " " << "b=" << pthis->getB()<< std::endl;
}
void main()
{
	Test t1(1, 2);
	Test t2(3, 4);
	Test t3 = add(t1, t2);
	t1.add(t2);//相当于add(&t1,t2);
	t1.printAB();
	system("pause");
}


由之前我们可以看出成员函数有一个this指针,这个指针指向自己本身,一般编译器将this指针给隐藏起来





友元函数:关键字为friend

//friend 破坏了类的封装性。。。。
//friend关键字是一个关系户
//why 无奈之际 java class
//java---》1.class==》class==>java类
//java类的反射 sun jdk api


//cc++ 1预编译gcc -E 2汇编 gcc -i 3编译gcc -c 3、链接ld ===》汇编代码
//friend 
//应用场景。。。。。
//const关键字 冒牌货c
//register
//typedef 混号王 


友元函数是指某些虽然不是类成员却能够访问类的所有成员的函数。类授予它的友元特别的访问权

#include "iostream"
using namespace std;

class A1
{
public:
	A1()
	{
		a1 = 100;
		a2 = 200;
	}
	int getA1()
	{
		return this->a1;
	}
	//声明一个友元函数
	friend void setA1(A1 *p, int a1); //什么这个函数是这个类的好朋友
	
protected:
private:
	int a1;
	int a2;
};

void setA1(A1 *p, int a1)
{
	p->a1 = a1;
}
//friend 破坏了类的封装性。。。。
//friend关键字是一个关系户
//why 无奈之际 java class
//java---》1.class==》class==>java类
//java类的反射 sun jdk api

//cc++ 1预编译gcc -E 2汇编 gcc -i 3编译gcc -c 3、链接ld ===》汇编代码
//friend 
//应用场景。。。。。
//const关键字 冒牌货c
//register
//typedef 混号王 

void main11()
{
	A1 mya1;
	cout<<mya1.getA1()<<endl;
	setA1(&mya1, 300);
	cout<<mya1.getA1()<<endl;

	system("pause");
}

class A
{
	//b是a的好朋友	
	friend class B;
public:
	void display()
	{
		cout<<x<<endl;
	}

protected:
private:
	int x;
};

class B
{
public:
	void setA(int x)
	{
		Aobj.x = x;
	}
	void printA()
	{
		cout<<Aobj.x<<endl;
	}
protected:
private:
	A Aobj;

};

void main()
{
	B b1;
	b1.setA(100);
	b1.printA();
	system("pause");
}



运算符重载入门

1.为什么需要运算符重载

答:比如定义一个复数类,类的对象相加,编译器不知道如何进行加减,编译器给你提供了一种机制,让用于自己去完成,自定义类的加减操作

//这个机制就是运算符重载

#include "iostream"
using namespace std;

//a + bi //复数
class Complex
{
public:
	friend Complex operator+(Complex &c1, Complex &c2);//声明一个友元函数,
	Complex(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	void printCom()
	{
		cout<<a<<" + "<<b<<"i "<<endl;
	}

private:
	int a;
	int b;
};

/*
//通过全局函数完成对象加
Complex add(Complex &c1, Complex &c2)
{
	Complex c3(c1.a + c2.a, c1.b+c2.b);
	return c3;
}
*/
Complex operator+(Complex &c1, Complex &c2)//全局函数重载 这里operator+是一个关键字
{
	Complex c3(c1.a + c2.a, c1.b+c2.b);
	return c3;
}
void main()
{
	{
		int a = 0, b = 0;
		int c = a + b;
		//对于基础类型,编译已经知道如何进行加减
	}
	Complex c1(1, 2), c2(3, 4);
	//为什么要有运算符重载
	//原因 Complex是用户自定义类型。。编译器根本不知道如何进行加减。。。
	//编译器给你提供了一种机制,让用户自己去完成,自定义类型的加减操作。。。。。
	//这个机制就是运算符重载机制

	//Complex c3 = c1 + c2;

	//Complex c3 = add(c1, c2);
	//Complex c3 = operator+(c1, c2);


	Complex c3 = c1 + c2;
	c3.printCom();


	system("pause");
}



用2种方法实现运算符重载

//全局函数方法去实现操作符重载
//1 承认操作符重载是一个函数
//2 根据操作数,1个操作数 2个操作数,完成函数的参数
//3 根据函数原型,实现也需要业务


用类成员函数实现-运算符重载

#include "iostream"
using namespace std;

//a + bi //复数
class Complex
{
public:
	friend Complex operator+(Complex &c1, Complex &c2);
	friend Complex operator*(Complex &c1, Complex &c2);
	friend Complex& operator++(Complex &c1); //前置++
	Complex(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	void printCom()
	{
		cout<<a<<" + "<<b<<"i "<<endl;
	}
	//通过类成员函数完成-操作符重载
	Complex operator-(Complex &c2)
	{
		Complex tmp(a - c2.a, this->b - c2.b);
		return tmp;
	}
	//通过成员函数完成前置--
	Complex& operator--()
	{
		this->a--;
		this->b--;
		return *this;
	}

private:
	int a;
	int b;
};

Complex operator+(Complex &c1, Complex &c2)
{
	Complex tmp(c1.a + c2.a, c1.b + c2.b);
	return tmp;
}

Complex operator*(Complex &c1, Complex &c2)
{
	Complex tmp(c1.a*c2.a, c1.b*c2.b);
	return tmp;
}

//前置++ 
Complex& operator++(Complex &c1)
{
	c1.a ++;
	c1.b ++;
	return c1;
}

void main()
{

	Complex c1(1, 2), c2(3, 4);

	//全局函数方法去实现操作符重载
	//1 承认操作符重载是一个函数
	//2 根据操作数,1个操作数 2个操作数,完成函数的参数
	//3 根据函数原型,实现也需要业务
	//Complex c31 = operator+(c1, c2);
	Complex c3 = c1 + c2;

	//用类成员函数实现-运算符重载
	Complex c4 = c1 - c2;
	c4.printCom();
	//c1.operator-(c2);

	{
		//问题1 根据类型
		int a = 10, b = 1;
		int c = a + b;
	}

	//前置++ 全局函数
	++c1; 
	c1.printCom();

	//前置-- 成员函数
	--c1;
	c1.printCom();
	//c1.operator--()



	system("pause");
}




正规重载写法

#include "iostream"
using namespace std;

//a + bi //复数
class Complex
{
public:
	friend ostream& operator<<(ostream &out, Complex &c1);
	Complex(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	void printCom()
	{
		cout<<a<<" + "<<b<<"i "<<endl;
	}
	//通过类成员函数完成-操作符重载
	Complex operator-(Complex &c2)
	{
		Complex tmp(a - c2.a, this->b - c2.b);
		return tmp;
	}
	Complex operator+(Complex &c2)
	{
		Complex tmp(a + c2.a, this->b + c2.b);
		return tmp;
	}
	//通过成员函数完成前置--
	Complex& operator--()
	{
		this->a--;
		this->b--;
		return *this;
	}
	//通过成员函数完成前置--
	Complex& operator++()
	{
		this->a++;
		this->b++;
		return *this;
	}

	//通过类成员函数完成后置--
	Complex operator--(int)
	{
		Complex tmp = *this;
		this->a--;
		this->b--;
		return tmp;
	}
	//通过类成员函数完成后置--
	Complex operator++(int)
	{
		Complex tmp = *this;
		this->a++;
		this->b++;
		return tmp;
	}

private:
	int a;
	int b;
};

void main61()
{

	Complex c1(1, 2), c2(3, 4);

	//全局函数方法去实现操作符重载
	//1 承认操作符重载是一个函数
	//2 根据操作数,1个操作数 2个操作数,完成函数的参数
	//3 根据函数原型,实现需要业务
	//Complex c31 = operator+(c1, c2);
	Complex c3 = c1 + c2;

	//用类成员函数实现-运算符重载
	Complex c4 = c1 - c2;
	c4.printCom();
	//c1.operator-(c2);

	{
		//问题1 根据类型
		int a = 10, b = 1;
		int c = a + b;
	}

	//前置++ 全局函数
	++c1; 
	c1.printCom();

	//前置-- 成员函数
	--c1;
	c1.printCom();
	//c1.operator--()

	//后置++ 全局函数
	c1++; //operator++(c1);
	c1.printCom();

	//后置-- 类成员函数
	c1--;  //c1.operator--()
	c1.printCom();

	system("pause");
}

ostream& operator<<(ostream &out, Complex &c1)
{
	//out<<"12345,生活真是苦"<<endl;
	out<<c1.a<<" + "<<c1.b<<"i "<<endl;
	return out;
}
void main()
{
	Complex c1(1, 2), c2(3, 4);
	int a = 10;
	char *p = "addddd";
	cout<<"a"<<a<<endl;
	cout<<"p"<<p<<endl;

	//Complex自定义类型 。a + bi ab ba
	cout<<c1;

	//全局函数 
	//cout<<c1;
	//operator<<(cout, c1);

	//1
	//类成员函数
	//因为你没有方法拿到cout这个类的源码。。。。AOP C++
	//cout.operator<<(c1);

	//2 支持链式编程
	cout<<c1<<"abcc";

	//函数返回值当左值,要求返回一个引用。。。。。
	
	//cout.operator<<(c1).operator<<("abcd");

	//s<<"abcd"

	//s cout.operator<<(c1);

	//s.operator<<("abcd");



	system("pause");

}





const修饰this指针

#include "iostream"
using namespace std;

//C 
class Test
{
public:
protected:
private:
	int a;
	const int A() //const // ====>int A(const this) 
	{ 
		//观点1:const是修饰a,但是通过测试,我们发现,b++也不能编译通过
		//这说明:const把a 和 b都修饰了。。。。

		//剖析:因为this作为函数的第一个参数,被隐藏。。。。const没有地方放。。。。
		//
		//const是修饰this

		a ++;
		//b++;
		return a; 
	}
	void A(int val) 
	{ 
		a = val; 
	}

	int BBB()
	{
		
		return a;
	}

	int b;
};


 int Aaaaa(const Test *pthis) 
{ 
	return 10; 
}
void main()
{
	Test t1;
	system("pause");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值