c++编程题目 在线等答案

给出下面的类定义
#include<stdio.h>
#include<iostream.h>
class three_d
{
int x,y,z;
public
three_d(int i,int j,int k)
{ x=i;y=j;z=k;}
three_d()
{x=0;y==;z=0;}
void get(int &i,int &j,int &k)
{i=x;j=y;k=z;}
void show();
};
针对three_d类,重载“+”运算符,以便使“+”运算符可以接受一面的运算类型:
ob+int
int+ob
其中ob为three_d类的对象
详细点 谢谢了

第1个回答  2010-03-05
#include<stdio.h>
#include<iostream.h>

class three_d
{
int x,y,z;
public:
friend three_d& operator+(three_d& a,int b);
friend three_d& operator+(int b,three_d& a);

three_d(int i,int j,int k)
{
x=i;
y=j;
z=k;
}
three_d()
{
x=0;
y=0;
z=0;
}
void get(int &i,int &j,int &k)
{
i=x;
j=y;
k=z;
}
void show()
{
cout<<x<<endl<<y<<endl<<z<<endl;
}
};
three_d& operator+(three_d& a,int b)
{
a.x+=b;
a.y+=b;
a.z+=b;
return a;
}
three_d& operator+(int b,three_d& a)
{
a.x+=b;
a.y+=b;
a.z+=b;
return a;
}

void main()
{
three_d temp; //测试下,没问题
temp.show();
temp=temp+1;
temp.show();
}
希望能帮到你本回答被提问者采纳
第2个回答  2010-03-05
class three_d;
three_d operator+(int,three_d); //这个函数是全局函数
three_d operator+(three_d,int);
class three_d
{
public:
friend three_d operator+(int,three_d);
friend three_d operator+(three_d,int);
protected:
private:
};
three_d operator+(int,three_d)
{

return three_d();
}
第3个回答  2010-03-05
friend three_d *operator(int i)
{
this->x+=i;
this->y+=i;
this->z+=i;
return this;
}
相似回答