2013年5月14日 星期二

C++ 使用物件的方法--五種


#include <iostream>
using namespace std;

class Count{

public:
  void setX(int a);    
  void show();    
private:
  int x;                          
};

void Count::setX(int a)      
{
     x =a;    
}

void Count::show()
{
 cout << x <<endl;  
}

  
int main(void)
{
   
   Count a;
   a.setX(2);
   a.show();
//////////////////////////////  
   Count b;
   Count *ptr = &b; //pointer
   b.setX(12);
   b.show();
//////////////////////////////
   Count c;
   Count &ref = c;
   ref.setX(45);
   ref.show();  
////////////////////////////////////////  
   Count *test = new Count;
   test->setX(44);
   test->show();
///////////////////////////////////////
   (*test).setX(443);
   (*test).show();
/////////////////////////////////////////////    
   
   
    system("PAUSE");
    return 0;
 
}

沒有留言:

張貼留言