#include<iostream>
using namespace std;
class Try
{
public:
Try()
{
cout << "测试友元函数以及构造、析构函数使用" << endl;
length = 0;
height = 0;
weight = 0;
}
~Try()
{
cout << "测试结束" << endl;
}
friend void print(Try try2);
void print2()
{
length += 5;
weight += 3;
height += 6;
cout << "长宽高之积:" << length * height * weight << endl;
}
private:
void print3()
{
length += 5;
weight += 3;
height += 6;
cout << "长宽高之积:" << length * height * weight << endl;
}
protected:
void print4()
{
length += 5;
weight += 3;
height += 6;
cout << "长宽高之积:" << length * height * weight << endl;
}
int length;
int height;
int weight;
};
void print(Try try2)
{
try2.length += 5;
try2.weight += 3;
try2.height += 6;
cout << "长宽高之积:" << try2.length * try2.height * try2.weight << endl;
}
class Stry:public Try
{
public:
int print5()
{
cout<<"长宽高之和:"<<length+weight+height<<endl;
}
};
int main()
{
Stry abc;
}
【1】当成员函数为保护类型,成员变量为保护类型时,子类访问成员函数失败
【2】当成员函数为公共类型,成员变量为保护类型时,子类访问成员函数成功
原文:https://www.cnblogs.com/trainking-star/p/12271387.html