如下(重要和非凡的地方都有具体的注解):#include <iostream> using namespace std; class test { private://私有成员类外不能够直接访问 int number; public://共有成员类外能够直接访问 float socre; public: int rp() { return number; } void setnum(int a) { number=a; } };
下面我们来看一个例子,利用这个例子中我们要说明两个重要问题:#include <iostream> using namespace std; int pp=0; class test { private: int number; public: float socre; int pp; public: void rp(); }; void test::rp()//在外部利用域区分符定义test类的成员函数 { ::pp=11;//变量名前加域区分符给全局变量pp赋值 pp=100;//设置结构体变量 }
void main() { test a; test b; a.rp(); cout<<pp<<endl; cout<<a.pp<<endl;
代码如下: #include <iostream> using namespace std; class test { private: int number; public: float socre; int pp; public: int rp(int); }; int test::rp(int a)//在外部利用域区分符定义test类的成员函数 { number=100; return a + number; }
所以在main中如假如要定义ballscore类的对象就要在类名称前加上class要害字 class ballscore jeff; 3.类型名称隐藏了非类型名称,看对上面代码的分析int test; void main() { class test { float a; float b; }; test test; ::test=1; class ballscore jeff;
1.一个名字不能同时设置为两种不同的类型class test { //... }; typedef int test; 这个就是错误的! 2.非类型名(变量名,常量名,函数名,对象名,枚举成员)不能重名. test a; void a(); 就是错误的,因为a是一个test类的对象,它和函数a名称重名了!
3.类型与非类型不在同一个名字空间上,可以重名,即使在同一作用域内,但两者同时出现时定义类对象的时候要加上前缀class以区分类型和非类型名! class test { //..... }
int test
class test a;//利用class前坠区分,定义了一个test类的对象a 好了,到这里关于类的知识点我们已经学习完,希望大家多多练习 更多内容请看C/C++技术学堂 C/C++技术专题 Java编程开发手册专题,或