首页 > 编程语言 >《C++》友元

《C++》友元

时间:2023-06-16 23:34:15浏览次数:55  
标签:友元 p2 p1 int C++ Person test tmp

友元--friend

友元可以访问类的私有属性

class Person
{
	friend void test();

public:
	Person(int n)
	{
		this->n = n;
	}

private:
	int n;
};
void test()
{
	Person p(20);
	cout << p.n << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

友元可以是友类、友函数、成员函数

重载运算符--operator

#include <iostream>
using namespace std;

class Person
{
	friend void test();
	friend Person operator+(Person &p1, Person p2);

public:
	Person(){};
	Person(int a,int b)
	{
		m_a = a;
		m_b = b;
	}
private:
	int m_a;
	int m_b;
};
Person operator+(Person &p1, Person p2)
{
	Person tmp ;
	tmp.m_a = p1.m_a + p2.m_a;
	tmp.m_b = p1.m_b+ p2.m_b;
	return tmp;
}
void test()
{
	Person p1(10, 10);
	Person p2(20, 20);
	Person p3 = p1 + p2;
	cout << "p3.m_a= " << p3.m_a << "p3.m_b= " << p3.m_b << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}


标签:友元,p2,p1,int,C++,Person,test,tmp
From: https://blog.51cto.com/u_16071993/6503306

相关文章

  • 【初识C++】(缺省参数和函数重载)
    @TOC一、缺省参数1.缺省参数定义缺省参数是在函数的声明中给定参数一个指定的值。如果传参没有给定参数,那就按照声明中默认的缺省值,如果给定了参数,那就按照给定的参数值。比如:usingnamespacestd;voidFunc(inta=0){ cout<<a<<endl;}intmain(){ Func(); //......
  • c++强制类型转换的不同
    参考https://blog.csdn.net/luolaihua2018/article/details/111996610https://zhuanlan.zhihu.com/p/368267441https://zhuanlan.zhihu.com/p/151744661https://blog.csdn.net/Behold1942/article/details/111657231https://stackoverflow.com/questions/332030/when-sho......
  • c++ linux 学习第二课
    c++linux学习第二课一、GCC的工作流程:gcc编译器将C源文件到生成一个可执行程序中间一共经历四个步骤:二、gcc常用参数:-v版本-E生成预处理文件-S生成汇编文件-c只编译,生成目标文件.o文件-I(大写的i)指定头文件所在的路径-L指定库文件所在的路径-l(小写的L)......
  • C++面试八股文:在C++中,你知道哪些运算符?
    C++面试八股文:在C++中,你知道哪些运算符?某日二师兄参加XXX科技公司的C++工程师开发岗位第11面:面试官:在C++中,你都知道都哪些运算符?二师兄:啥?运算符?+-*/=这些算吗?面试官:嗯,还有其他的吗?二师兄:当然还有,+=,-=,*=,/=,==,还有逻辑运算,位运算等。面试官:好的。那你知道这些运算......
  • C++通讯录管理系统[2023-06-16]
    C++通讯录管理系统[2023-06-16]通讯录管理系统手机通讯录中的联系人的信息既可以存储在手机中,也可以存储在手机卡中,也可以同时存储在两个位置上(假设每个位置上的存储容量为1000,即手机卡中或手机上最多只能存储1000个联系人)。存储在手机中的联系人的信息只包含姓名和电话号码两项......
  • c++一些零碎记录
    c++11alignasstructalignas(8)S{}//定义结构体同时指定分配给结构体的大小为8字节alignof(与内存对齐相关)structobj{chara;intb;}alignof(obj)=4;//alignof(obj)=4表示对于obj,其内存对齐是以多少字节为单位对齐对于单个变量char其alignof(char)=1,单个字节对齐......
  • 对C++中const的说明
    对C++中const的说明在C++中,const是一个关键字,用于指定对象或变量是只读的,即不可修改。它可以应用于不同的上下文中,包括:对象和变量声明:通过在变量或对象的声明前加上const关键字,可以将其标记为只读。这意味着一旦被初始化,就不能再修改该对象或变量的值。constintx=10;//声明一......
  • C++ multi process share value via write and read data from serialized file,the b
    #include<atomic>#include<chrono>#include<cmath>#include<condition_variable>#include<cstddef>#include<forward_list>#include<fstream>#include<functional>#include<future>#include<iom......
  • vscode+cmake c++ hello world!
    1.新建一个测试目录hello及一些必要文件D:\HELLO\HELLOCPP│CMakeLists.txt└─main.cppCMakeLists.txt#工程名project(Hello)#生成目标add_executable(Hellomain.cpp)hello.cpp#include<iostream>usingnamespacestd;intmain(){cout<<"hellowo......
  • C/C++航空客运订票管理系统[2023-06-16]
    C/C++航空客运订票管理系统[2023-06-16]用c++设计一个航空客运订票管理系统。系统功能要求如下:(1)查询航线:根据旅客提出的站名输出下列信息:航班号、飞机号、飞行日期(含详细时间段),余票量、已定票乘客的信息;(2)排序功能:根据不同属性对航线进行排序;(3)订票业务:根据客户提出的要求(航班......