1. Code
1#include <iostream>2#include <stdio.h>3 using namespacestd;4 class Point5 {6 Public:7Point (intx=0,inty=0): X (x), Y (y)8 { 9count++;Ten } OnePoint (ConstPoint &p): X (p.x), Y (p.y) A { -count++; - } the~point () {count--;} - intGetx ()Const{returnx;} - intGety ()Const{returny;} - Static intCount//Note to define the following to public, you have access rights + Private: - intx, y; + }; A intPoint::count=0;//static data member definition and initialization, using Class name qualification at intMain () - { - int*ptr=&point::count;//defines an int pointer that points to a static member of a class -Point A (4,5); -cout<<"Point A:"<<a.getx () <<","<<a.gety () <<Endl; -cout<<"Object count="<<*ptr<<Endl; inPoint B (a);//use one of the objects of the class to do another argument to the object, call the copy constructor -cout<<"Point B:"<<b.getx () <<","<<b.gety () <<Endl; tocout<<"Object count="<<*ptr<<Endl; +System"Pause"); - return 0; the}
2. Running Results
Same as the last one.
The Note:count type is static and access rights are public.
Pointer to static data member of Class