15_2_1.cpp
来自「本文档是(作者:钱能)《C++程序设计教程》课后习题答案。 选题编辑:张朝阳 」· C++ 代码 · 共 35 行
CPP
35 行
//15_2_1
#include <iostream.h>
class Animal;
void SetValue(Animal&, int);
void SetValue(Animal&, int, int);
class Animal{
public:
friend void SetValue(Animal&, int);
protected:
int itsWeight;
int itsAge;
};
void SetValue(Animal& ta, int tw)
{
ta.itsWeight =tw;
}
void SetValue(Animal& ta, int tw, int tn)
{
ta.itsWeight =tw; //非成员函数或友员不能访问类的私有数据
ta.itsAge =tn; //非成员函数或友员不能访问类的私有数据
}
void main()
{
Animal peppy;
SetValue(peppy, 5);
SetValue(peppy,7,9);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?