C++——友元、函数重载、操作符重载

1.友元的用法

(1)在类中以friend关键字声明友元

(2)类的友元可以是其它类或者具体函数

(3)友元不是类的一部分

(4)友元不受类中访问级别的限制

(5)友元可以直接访问具体类的所有成员

复制代码
#include <stdio.h>
#include<math.h>
class Point {
	double x;
	double y;
	friend double func(Point& p1,Point& p2);
public:
	Point(int x, int y) {
		this->x=x;
		this->y=y;
	}
};
double func(Point& p1, Point& p2) {
	double ret = 0;
	ret = (p2.y - p1.y) * (p2.y - p1.y) + (p2.x - p1.x) * (p2.x - p1.x);
	ret = sqrt(ret);
	return ret;
}
int main() {
	Point p1(1,2);
	Point p2(10,20);
	printf("|(p1,p2)|=%f\n", func(p1, p2));
	return 0;
}

友元函数可以访问类中的私有属性,否则就要调用get函数去访问x和y,增加性能消耗

类的友元可以是其它类的成员函数,也可以是某个完整的类,但是不具备传递性

2.函数重载

(1)重载函数的本质为多个不同的函数

(2)函数名和参数列表是唯一的标识

(3)函数重载必须发生在同一个作用域中

复制代码
#include <stdio.h>
#include<string.h>
int main() {
	const char* s = "niuhuicong";
	char buf[15] = { 0 };
	strcpy(buf, s);
	strncpy(buf, s, sizeof(buf) - 1); //将s拷贝给buf,长度最多为sizeof(buf) - 1)
	printf("%s\n", buf);
	return 0;
}

但是strncpy这个函数很难记,所以我们将改为

复制代码
#include <stdio.h>
#include<string.h>
char* strcpy(char* buf, const char* str, unsigned int n) {
	return strncpy(buf, str, n);
}
int main() {
	const char* s = "niuhuicong";
	char buf[15] = { 0 };
	strcpy(buf, s, sizeof(buf) - 1);
	printf("%s\n", buf);
	return 0;
}

3.操作符重载

(1)复数相加

复制代码
#include <stdio.h>
#include<string.h>
class Complex {
	int a;
	int b;
public:
	Complex(int a = 0, int b = 0) {
		this->a = a;
		this->b = b;
	}
	int getA() {
		return a;
	}
	int getB() {
		return b;
	}
	friend Complex Add(const Complex& p1, const Complex& p2);
};
Complex Add(const Complex& p1, const Complex& p2) {
	Complex ret;
	ret.a = p1.a + p2.a;
	ret.b = p1.b + p2.b;
	return ret;
}
int main() {
	Complex c1 = { 1,2 };
	Complex c2 = { 3,4 };
	Complex c3 = Add(c1, c2);
	printf("c3.a=%d,c3.b=%d\n",c3.getA(), c3.getB());
	return 0;
}

C++中的重载能够扩展操作符的功能,操作符的重载以函数的方式进行,本质是特殊形式的函数扩展操作符的功能

通过operator关键字可以定义特殊的函数,本质是通过函数重载操作符

所以复数相加使用operator关键字的写法如下:

复制代码
#include <stdio.h>
#include<string.h>
class Complex {
	int a;
	int b;
public:
	Complex(int a = 0, int b = 0) {
		this->a = a;
		this->b = b;
	}
	int getA() {
		return a;
	}
	int getB() {
		return b;
	}
	friend Complex operator +(const Complex& p1, const Complex& p2);
};
Complex operator +(const Complex& p1, const Complex& p2) {
	Complex ret; 
	ret.a = p1.a + p2.a;
	ret.b = p1.b + p2.b;
	return ret;
}
int main() {
	Complex c1 = { 1,2 };
	Complex c2 = { 3,4 };
	Complex c3 = c1+c2;
	printf("c3.a=%d,c3.b=%d\n",c3.getA(), c3.getB());
	return 0;
}

可以将操作符重载函数定义为类的成员函数,比全局操作符重载函数少一个参数(左操作数),不需要依赖友元就可以完成操作符重载,当成员函数和全局函数中都有该操作符重载函数,则优先调用成员函数的实现版本

复制代码
#include <stdio.h>
#include<string.h>
class Complex {
	int a;
	int b;
public:
	Complex(int a = 0, int b = 0) {
		this->a = a;
		this->b = b;
	}
	int getA() {
		return a;
	}
	int getB() {
		return b;
	}
	Complex operator +(const Complex& p1) {
		Complex ret;
		ret.a = this->a + p1.a;
		ret.b = this->b + p1.b;
		return ret;
	}
};

int main() {
	Complex c1 = { 1,2 };
	Complex c2 = { 3,4 };
	Complex c3 = c1+c2;
	printf("c3.a=%d,c3.b=%d\n",c3.getA(), c3.getB());
	return 0;
}
相关推荐
IT 行者24 分钟前
Web逆向工程AI工具:JSHook MCP,80+专业工具让Claude变JS逆向大师
开发语言·javascript·ecmascript·逆向
程序员 沐阳2 小时前
JavaScript 内存与引用:深究深浅拷贝、垃圾回收与 WeakMap/WeakSet
开发语言·javascript·ecmascript
Mr_Xuhhh3 小时前
Java泛型进阶:从基础到高级特性完全指南
开发语言·windows·python
He1955013 小时前
wordpress搭建块
开发语言·wordpress·古腾堡·wordpress块
老天文学家了3 小时前
蓝桥杯备战Python
开发语言·python
赫瑞3 小时前
数据结构中的排列组合 —— Java实现
java·开发语言·数据结构
初夏睡觉4 小时前
c++1.3(变量与常量,简单数学运算详解),草稿公放
开发语言·c++
升职佳兴4 小时前
C盘爆满自救:3步无损迁移应用数据到E盘(含回滚)
c语言·开发语言
ID_180079054734 小时前
除了 Python,还有哪些语言可以解析 JSON 数据?
开发语言·python·json
阿拉斯攀登4 小时前
从入门到实战:CMake 与 Android JNI/NDK 开发全解析
android·linux·c++·yolo·cmake