自考新教材-p175_2

源程序:

#include <iostream>
using namespace std;

class pointer
{
public:
int a;
int *p;
pointer()
{
a = 100;
p = new int(10);
}
pointer(const pointer &tempp)
{
if (this != &tempp)
{
a = tempp.a;
p = new int();
*p = *tempp.p;
}
}
~pointer()
{
if (p != NULL)
delete p;
}
pointer &operator=(const pointer &c)
{
if(this==&c)
return *this;
delete this->p;
this->p = new int(*c.p);
return *this;
}
};
int main()
{
pointer p1;
pointer p2(p1);
cout << p1.a << ",\t" << *p1.p << ",\t" << (p1.p == p2.p) << endl;
*p1.p = 20;
p2.a = 300;
cout << (p1.a == p2.a) << ",\t" << *p1.p << ",\t" << (p1.p == p2.p) << endl;
system("pause");
return 1;
}

运行结果:

 

posted @ 2020-02-08 09:31  bobo哥  阅读(159)  评论(0编辑  收藏  举报