C++运行结果分析?
#include<iostream>usingnamespacestd;classCoordinate{public:Coordinate(intx1,inty1){x=...
#include<iostream>
using namespace std;
class Coordinate
{
public:
Coordinate(int x1,int y1)
{ x=x1;
y=y1;
cout<<"constructor is called.\n";
}
Coordinate(Coordinate &p);
~Coordinate()
{ cout<<"Destructor is called\n";}
int getx()
{ return x;}
int gety()
{ return y;}
private:
int x,y;
};
Coordinate::Coordinate(Coordinate &p)
{
x=p.x;
y=p.y;
cout<<"Copy-initialization Constructour is called\n";
}
int main()
{
Coordinate p1(3,4);
Coordinate p2(p1);
Coordinate p3=p2;
cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n";
return 0;
}
请大神帮我分析下运行结果啊,,不知道怎么分析 展开
using namespace std;
class Coordinate
{
public:
Coordinate(int x1,int y1)
{ x=x1;
y=y1;
cout<<"constructor is called.\n";
}
Coordinate(Coordinate &p);
~Coordinate()
{ cout<<"Destructor is called\n";}
int getx()
{ return x;}
int gety()
{ return y;}
private:
int x,y;
};
Coordinate::Coordinate(Coordinate &p)
{
x=p.x;
y=p.y;
cout<<"Copy-initialization Constructour is called\n";
}
int main()
{
Coordinate p1(3,4);
Coordinate p2(p1);
Coordinate p3=p2;
cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n";
return 0;
}
请大神帮我分析下运行结果啊,,不知道怎么分析 展开
展开全部
运行结果:
constructor is called.
Copy-initialization Constructour is called
Copy-initialization Constructour is called
p3=(3,4)
Destructor is called
Destructor is called
Destructor is called
Coordinate p1(3,4); // Coordinate类的实例p1,x=3,y=4
Coordinate p2(p1); // Coordinate类的实例p2,调用了拷贝构造函数,通过p1初始化了p2
Coordinate p3=p2; // Coordinate类的实例p3,调用了拷贝构造函数,通过p2初始化了p3
cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n"; // 输出p3的值。
最后,程序自动释放栈上的内存,调用了p1,p2,p3的析构函数
constructor is called.
Copy-initialization Constructour is called
Copy-initialization Constructour is called
p3=(3,4)
Destructor is called
Destructor is called
Destructor is called
Coordinate p1(3,4); // Coordinate类的实例p1,x=3,y=4
Coordinate p2(p1); // Coordinate类的实例p2,调用了拷贝构造函数,通过p1初始化了p2
Coordinate p3=p2; // Coordinate类的实例p3,调用了拷贝构造函数,通过p2初始化了p3
cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n"; // 输出p3的值。
最后,程序自动释放栈上的内存,调用了p1,p2,p3的析构函数
展开全部
目测应该输出
“constructour is called”
“Copy-initialization Constructour is called”
“Copy-initialization Constructour is called”
“p3=(3,4)”
"Destructor is called“
"Destructor is called“
"Destructor is called“
p1(3,4)调用构造函数
p2(p1)调用拷贝函数
p3=p2又调用一次拷贝
然后echo
最后destructor,希望能帮到你
“constructour is called”
“Copy-initialization Constructour is called”
“Copy-initialization Constructour is called”
“p3=(3,4)”
"Destructor is called“
"Destructor is called“
"Destructor is called“
p1(3,4)调用构造函数
p2(p1)调用拷贝函数
p3=p2又调用一次拷贝
然后echo
最后destructor,希望能帮到你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
首先在创建对象时是调用构造函数。当用类的一个对象去初始化该类的另一个对象时、如果函数的形参是类的对象,调用函数时,进行形参和实参结合时、如果函数的返回值是类的对象,函数执行完成返回调用者时都是调用拷贝构造函数。当对象的生存期即将结束的时候,虚构函数被依次调用。最后就是类内函数的调用。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询