找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3213|回復(fù): 0
收起左側(cè)

C++語言具有靜態(tài)數(shù)據(jù)和靜態(tài)成員函數(shù)的Point類

[復(fù)制鏈接]
ID:71259 發(fā)表于 2014-12-30 01:59 | 顯示全部樓層 |閱讀模式
本帖最后由 daming 于 2014-12-30 02:15 編輯


  1. #include<iostream>
  2. using namespace std;
  3. class Point                           //point類定義
  4. {
  5. public:                               // 外部接口
  6. Point(int xx=0,int yy=0){X=xx;Y=yy;countP++;};
  7. Point(Point&);                    //拷貝構(gòu)造函數(shù)
  8. ~Point(){countP--;};
  9. int GetX(){return X;}
  10. int GetY(){return Y;}
  11. static void GetC(){cout<<"Object id="<<countP<<endl;}   // 靜態(tài)函數(shù)成員
  12. private:
  13. int X,Y;
  14. static int countP;               //靜態(tài)數(shù)據(jù)成員聲明
  15. };
  16. int Point::countP=0;                 //靜態(tài)數(shù)據(jù)成員初始化
  17. Point::Point(Point &p)
  18. {
  19. X=p.X;
  20. Y=p.Y;
  21.     countP++;
  22. }

  23. void main()                        //主函數(shù)實(shí)現(xiàn)
  24. {
  25. Point A(4,5);                  //定義對象A
  26. cout<<"Point A,"<<A.GetX()<<", "<<A.GetY();
  27. A.GetC();                     //輸出對象號,對象名引用  "."
  28. Point B(A);
  29. cout<<"Point B,"<<B.GetX()<<", "<<B.GetY();
  30. Point::GetC();                //類名引用  "::"
  31. }
復(fù)制代碼


回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表