为了尽量降低全局变量的使用并提供用户自己定义类型的功能。C++语言提供了一种新的语言机制---类(class)。并以类作为构造程序的基本单位


#include<iostream>
using namespace std;
class C
{
public:
int getAge()const
{
return age;
};
void setAge(int n)
{
age=n;
}
private:
int age;
};
 
int main()
{
C c;
c.setAge(22);
cout<<"Myage:"<<c.getAge()<<endl;
return 0;
}
posted on 2017-06-01 20:43  yutingliuyl  阅读(115)  评论(0编辑  收藏  举报