您的位置:首页 > 其它

6-13/6-14/6-15

2013-11-02 15:18 239 查看
//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;
}


#include <iostream>
//6-14
using namespace std;

class Point
{
public:
Point(int x=0,int y=0):x(x),y(y)
{
count++;
}
Point(const Point &p):x(p.x),y(p.y)
{
count++;
}
~Point()
{
count--;
}
int getX() const
{
return x;
}
int getY() const
{
return y;
}
static int count;
private:
int x,y;
};
int Point::count=0;
int main()
{
int *ptr=&Point::count;
Point a(4,5);
cout<<"Point A:"<<a.getX()<<","<<a.getY();
cout<<"Object count= "<<Point::count<<endl;
Point::count=100;
Point b(a);
cout<<"Point B: "<<b.getX()<<", "<<b.getY();
cout<<"Object count="<<*ptr<<endl;

return 0;
}


#include <iostream>
//6-15
using namespace std;

class Point
{
public:
Point(int x=0,int y=0):x(x),y(y)
{
count++;
}
Point(const Point &p):x(p.x),y(p.y)
{
count++;
}
~Point(){count--;}
int getX() const {return x;}
int getY() const {return y;}

static void showCount()
{
cout<<" Object count="<<count<<endl;
}
private:
int x,y;
static int count;
};

int Point::count=0;

int main()
{
void (*funcPtr)()=Point::showCount;

Point a(4,5);
cout<<"Point A:"<<a.getX()<<", "<<a.getY();

funcPtr();

Point b(a);
cout<<"Point B:"<<b.getX()<<","<<a.getY();
funcPtr();

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐