当前位置:编程学习 > VC++ >>

VC++编程求救!!!急急急

设计一个平面点类Location,要求

1.两个私有数据成员:int X,Y分别存放横,纵坐标

2.公有函数;构造函数Location(int xx=0,int yy=0)为当前对象赋初值,Location(Location &p)用已知对象p为当前对象赋值,析构函数 Location()完成

3.函数Location g()定义对象A(1,2)并返回该对象

4.函数中定义对象Location B,并且函数g() 返回的对象为其赋值

答案:
class Location
{
private:
int x;
int y;

public:
Location();
Location(int xx,int yy);
Location(Location &p);
~Location();
Location g();
int getX();
int getY();
};


#include <iostream>
#include "location.h"

using namespace std;

Location::Location()
{
x=0;
y=0;
}

Location::Location(int xx,int yy)
{
x=xx;
y=yy;
}

Location::Location(Location &p)
{
this->x=p.x;
this->y=p.y;
}

Location::~Location()
{
}

Location Location::g()
{
Location A(1,2);
return A;
}


int Location::getX()
{
return x;
}

int Location::getY()
{
return y;
}



int main()
{
Location B;
Location temp(3,4);
Location temp2(temp); //使用拷贝构造函数

B=temp.g();
cout<<"B: x="<<B.getX()<<",y= "<<B.getY()<<endl;
cout<<"temp: x="<<temp.getX()<<",y= "<<temp.getY()<<endl;
cout<<"temp2: x="<<temp2.getX()<<",y= "<<temp2.getY()<<endl;

return 0;
}
class Location
{
int X,Y;
public Location(int xx,int yy)
{
xx=0;
yy=0;
}
public Location(int x,int y)
{
this.X=x;
this.Y=y;
}
public void g()
{
this.X=1;
this.Y=2;
}
}
void main()
{
Location m;
m.g();
}
没有看懂第3个...第四个不明白.
class CLocation
{
public:
CLocation(int nx, int ny);
CLocation(CLocation &p);
~CLocation();

CLocation *g();

private:
int m_nx;
int m_ny;
};

CLocation::CLocation(CLocation &p)
{
this->m_nx = p.m_nx;
this->m_ny = p.m_ny;
}

CLocation::CLocation(int nx=0, int ny=0)
{
this->m_nx = nx;
this->m_ny = ny;
}
CLocation::~CLocation(void)
{
}

CLocation *CLocation::g()
{
return this;
}




很高兴为您 解答





class Location{private:int x;int y;public:Location();Location(int xx,int yy);Location(Location &p);~Location();Location g();int getX();int getY();};#include <iostream>#include "location.h"using namespace std;Location::Location(){x=0;y=0;}Location::Location(int xx,int yy){x=xx;y=yy;}Location::Location(Location &p){this->x=p.x;this->y=p.y;}Location::~Location(){}Location Location::g(){Location A(1,2);return A;}int Location::getX(){return x;}int Location::getY(){return y;}int main(){Location B; Location temp(3,4);Location temp2(temp); //使用拷贝构造函数B=temp.g();cout<<"B: x="<<B.getX()<<",y= "<<B.getY()<<endl;cout<<"temp: x="<<temp.getX()<<",y= "<<temp.getY()<<endl;cout<<"temp2: x="<<temp2.getX()<<",y= "<<temp2.getY()<<endl;return 0;}

上一个:VC++编程练习问题
下一个:VC++编程6

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,