5-8

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class A
 6 {
 7 public:
 8     A(int i);
 9     void print();
10 private:
11     const int a;
12     static const int b=10;
13 };
14 
15 
16 A::A(int i):a(i){}
17 
18 void A::print()
19 {
20     cout<<a<<":"<<b<<endl;
21 }
22 //常成员函数只能通过初始化来获得初值
23 
24 int main()
25 {
26     A a1(100),a2(0);
27     a1.print();
28     a2.print();
29 
30     return 0;
31 }

 

posted @ 2013-11-02 15:09  退之  阅读(176)  评论(0编辑  收藏  举报