6-13/6-14/6-15
13886 ワード
//6-13
#include<iostream>
using namespace std;
class Point
{
public:
Point(int x=0,int y=0):x(x),y(y){}
int getX() const {return x;}
int getY() const {return y;}
private:
int x,y;
};
int main()
{
Point a(4,5);
Point * p1=&a;
int (Point::*funcPtr)() const=&Point::getX;
cout<<(a.*funcPtr)()<<endl;
cout<<(p1->*funcPtr)()<<endl;
cout<<a.getX()<<endl;
cout<<p1->getX()<<endl;
return 0;
}
1 #include <iostream>
2 //6-14
3 using namespace std;
4
5 class Point
6 {
7 public:
8 Point(int x=0,int y=0):x(x),y(y)
9 {
10 count++;
11 }
12 Point(const Point &p):x(p.x),y(p.y)
13 {
14 count++;
15 }
16 ~Point()
17 {
18 count--;
19 }
20 int getX() const
21 {
22 return x;
23 }
24 int getY() const
25 {
26 return y;
27 }
28 static int count;
29 private:
30 int x,y;
31 };
32 int Point::count=0;
33 int main()
34 {
35 int *ptr=&Point::count;
36 Point a(4,5);
37 cout<<"Point A:"<<a.getX()<<","<<a.getY();
38 cout<<"Object count= "<<Point::count<<endl;
39 Point::count=100;
40 Point b(a);
41 cout<<"Point B: "<<b.getX()<<", "<<b.getY();
42 cout<<"Object count="<<*ptr<<endl;
43
44 return 0;
45 }
1 #include <iostream>
2 //6-15
3 using namespace std;
4
5 class Point
6 {
7 public:
8 Point(int x=0,int y=0):x(x),y(y)
9 {
10 count++;
11 }
12 Point(const Point &p):x(p.x),y(p.y)
13 {
14 count++;
15 }
16 ~Point(){count--;}
17 int getX() const {return x;}
18 int getY() const {return y;}
19
20 static void showCount()
21 {
22 cout<<" Object count="<<count<<endl;
23 }
24 private:
25 int x,y;
26 static int count;
27 };
28
29 int Point::count=0;
30
31 int main()
32 {
33 void (*funcPtr)()=Point::showCount;
34
35 Point a(4,5);
36 cout<<"Point A:"<<a.getX()<<", "<<a.getY();
37
38 funcPtr();
39
40 Point b(a);
41 cout<<"Point B:"<<b.getX()<<","<<a.getY();
42 funcPtr();
43
44 return 0;
45 }