c++拷贝构造函数的问题

来源:百度知道 编辑:UC知道 时间:2024/05/22 20:29:18
请问下面的程序怎样用拷贝构造函数实现

#include <iostream.h>
#include <math.h>
class Location
{
public:
int X,Y;

Location( int a , int b);
double distance (Location &);
~Location();
};
Location::Location(int a ,int b)
{
X=a;
Y=b; }

double Location::distance(Location & loc1)
{
double length;
length=sqrt((loc1.X-Location::X)*(loc1.X-Location::X)+
(loc1.Y-Location::Y)*(loc1.Y-Location::Y));

return length; }

Location::~Location()
{ }

class Rectangle
{
public:
int length, width;
Location upleft;
Rectangle(int a,int b, int length,int width);
~Rectangle();
};
Rectangle::Rectangle(int a,int b, int len,int wid): upleft(a, b)
{
length=len;
width=wid;
Location *upleft=new Location(a,b);

}
Rectangle::~Rectangle()
{
delete upleft;}
}
void m

你原来的程序就有些小问题,帮你也改了一下。代码在下面。

#include <iostream>
#include <cmath>
using namespace std;

class Location
{
public:
int X,Y;

Location( int a , int b); //构造函数,构造函数可以有多个
Location( const Location &); //拷贝构造函数
double distance (Location &);
~Location(); //析构函数,析构函数只能有一个
};

//构造函数1
Location::Location(int a ,int b)
{
X=a;
Y=b;
}

Location::Location(const Location &LC)
{
this->X = LC.X;
this->Y = LC.Y;
}

double Location::distance(Location & loc1)
{
double length;
length=sqrt((loc1.X-Location::X)*(loc1.X-Location::X)+
(loc1.Y-Location::Y)*(loc1.Y-Location::Y));
return length;
}

Location::~Location()
{ }

class Rectangle
{
public:
int length, width;
Location *upleft;
Rectangle(int a,int b, int length,