静态_友元_const.pptVIP

  • 4
  • 0
  • 约7.68千字
  • 约 36页
  • 2017-01-01 发布于河南
  • 举报
静态_友元_const

static 类成员 static类变量 表示的是同类的所有对象共享的信息 尽管像是全局变量, 但作用域是类范围 只被同一个类的多个对象访问 只在文件范围中初始化一次 即使类没有一个对象,其static成员依然存在 可以是public、 private或protected Static类数据成员和成员函数独立于类对象而存在 static 类成员 访问static 类变量 public static变量 可以用二元作用域运算符通过类名访问(::) Student::count private static 变量 当不存在类成员对象时 只能通过 public static成员函数访问 通过类名、二元作用域运算符(::)和函数名, 来调用public static成员函数 Student::getCount() static 类成员 static 成员函数 不能访问非static数据或函数 static函数没有this指针 3 #include iostream 5 using std::cout; 6 using std::endl; 8 #include new // C++ standard new operator 9 #include cstring // strcpy and strlen prototypes 11 #include Student2.h // Student class definition 12 14 int Student::count = 0; 15 18 int Student::getCount() 19 { 20 return count; 21 22 } 27 Student::Student( const char *first, const char *last ) 28 { 29 firstName = new char[ strlen( first ) + 1 ]; 30 strcpy( firstName, first ); 31 32 lastName = new char[ strlen( last ) + 1 ]; 33 strcpy( lastName, last ); 34 35 ++count; // increment static count of Students 36 37 cout Student constructor for firstName 38 lastName called. endl; 39 40 } // end Student constructor 41 43 Student::~Student() 44 { 45 cout ~Student() called for firstName 46 lastName endl; 48 delete [ ] firstName; // recapture memory 49 delete [ ] lastName; // recapture memory 50 51 --count; // decrement static count of Students 52 53 } 3 #include iostream 5 using std::cout; 6 using std::endl; 8 #include new // C++ standard new operator 10 #include Student2.h // Student cla

文档评论(0)

1亿VIP精品文档

相关文档