#include <iostream>
using namespace std;
/*类的声明*/
class Test //类的名称
//命名规则为:首字母大写
{
public: //public关键字后的声明是类与外部的接口,
//外部可访问public后声明的 成员 和 方法(公有成员和方法)
void initXYZ(int x, int y, int z); //类中方法(c++中的函数称为“方法”)
//命名规则是驼峰式命名(前面小写,后面首字母大写)
void displayXYZ() //在类内实现的方法
//默认为“内联函数”
{
cout << x_ << endl;
cout << y_ << endl;
cout << z_ << endl;
}
private: //private下的成员为 “私有成员”
//只允许类中的方法访问,不允许外部访问
//若类中无任何关键字声明,则默认为“私有”
int x_;//私有成员
//命名规则为:成员名+‘_’(name_)
int y_;
protected: //protected下的成员为“保护成员”
//与private类似,差别在于继承与派生时,
//对派生类的影响不同
int z_;//保护成员
};
void Test::initXYZ(int x, int y, int z) //类外实现方法
{
x_ = x;
y_ = y;
z_ = z;
}
int main()
{
Test t;
t.initXYZ(1, 2, 3);
t.displayXYZ();
return 0;
}