1、友元函数
#include<iostream>
using namespace std;
class MyClass
{
public://访问修饰符
int a;
protected:
void fun()
{
cout <<"fun"<<" "<<a<< endl;
}
//friend void fun2();//友元函数 类内成员对于这个函数相当于public
private:
void fun1()
{
cout << "fun1" <<" "<< a << endl;
}
friend void fun2(); //这个友元的范围在整个类里都可以。即对应函数的友元出现一次就可访问类全部的函数,没有位置要求。
friend int main();
private:
void fun3()
{
cout << "fun3" << " " << a << endl;
}
};
void fun2()
{
MyClass db;
db.a = 12;
db.fun1();
db.fun();
db.fun3();
}
int main()
{
fun2();
system("pause");
return 0;
}
2、接口函数
#include<iostream>
using namespace std;
class MyClass
{
public://访问修饰符
int a;
private:
int age;
void fun3()
{
age = 22;
cout << "fun3" << " " << age<< endl;
}
public:
int Get()// 接口函数
{
return age;
}
void Set()
{
age = 1234;
}
};
int main()
{
MyClass db1;
db1.Set();
int a = db1.Get();
cout << a << endl;
system("pause");
return 0;
}