#include<iostream>
class Object{
public:
int score;
float height ;
void Test(){
printf("test function,age=%d\n",age);
printf("test function,num=%d",num);
}
private:
int age=10;
void Testprivate(){
printf("testprivate function\n");
}
protected:
int num=91;
void Testprotected(){
printf("testprotected function\n");
}
};
int main(){
Object student1;
student1.score=90;
student1.height=170.1;
student1.Test();
//TestPrivate(); 不能在此处使用
//Testprotected(); 不能在此处使用
return 0;
}
private定义的变量和函数只能在类内部使用;
public定义的变量和函数可以从任何地方访问
#include<iostream>
using namespace std;
class Object{
public:
int score;
float height ;
void Test_public( );
private:
int age=10;
void Test_private(int score);
protected:
int num=91;
void Test_protected();
};
/*函数定义到class外面*/
void Object:: Test_public(){
printf("test function,age=%d\n",age);
printf("test function,num=%d",num);
}
void Object:: Test_private(int score){
cout<<"score="<<score<<endl;
}
void Object:: Test_protected(){
printf("testprotected function\n");
}
int main(){
Object student1;
student1.score=90;
student1.height=170.1;
student1.Test_public();
//TestPrivate(); 不能在此处使用
//Test_protected(); 不能在此处使用
return 0;
}