c++程序编写:声明point类,对其重载运算符“+”、“—”。
3个回答
展开全部
重载“-”和“+”一样。我这里就只写重载“+”的代码了
一个参数的,即把重载函数声明为成员函数
#include<iostream>
using namespace std;
class point
{
public:
point(int a,int b):x(a),y(b){}
point operator +(point &p2);
void show()
{
cout<<x<<","<<y<<endl;
}
private:
int x;
int y;
};
point point:: operator+( point &p2)
{
return point(x + p2.x, y + p2.y);
}
两个参数时,即把成员函数声明为友元函数
#include<iostream.h>
class point
{
public:
point(int a,int b):x(a),y(b){}
friend point operator +(point p1 , point p2);
void show()
{
cout<<x<<","<<y<<endl;
}
private:
int x;
int y;
};
point operator+(point p1 , point p2)
{
return point(p1.x + p2.x, p1.y + p2.y);
}
int main()
{
point c1(2,3),c2(5,6);
c1=c1+c2;
c1.show ();
return 0;
}
一个参数的,即把重载函数声明为成员函数
#include<iostream>
using namespace std;
class point
{
public:
point(int a,int b):x(a),y(b){}
point operator +(point &p2);
void show()
{
cout<<x<<","<<y<<endl;
}
private:
int x;
int y;
};
point point:: operator+( point &p2)
{
return point(x + p2.x, y + p2.y);
}
两个参数时,即把成员函数声明为友元函数
#include<iostream.h>
class point
{
public:
point(int a,int b):x(a),y(b){}
friend point operator +(point p1 , point p2);
void show()
{
cout<<x<<","<<y<<endl;
}
private:
int x;
int y;
};
point operator+(point p1 , point p2)
{
return point(p1.x + p2.x, p1.y + p2.y);
}
int main()
{
point c1(2,3),c2(5,6);
c1=c1+c2;
c1.show ();
return 0;
}
展开全部
重载“-”和“+”一样。我这里就只写重载“+”的代码了
一个参数的,即把重载函数声明为成员函数
#include<iostream.h>
class point
{
private:
int x;
int y;
public:
point(int a,int b):x(a),y(b)
{}
point operator +(point &p2);
void show()
{
cout<<x<<","<<y<<endl;
}
};
point point:: operator+( point &p2)
{
return point(x + p2.x, y + p2.y);
}
两个参数时,即把成员函数声明为友元函数
#include<iostream.h>
class point
{
private:
int x;
int y;
public:
point(int a,int b):x(a),y(b)
{}
friend point operator +(point p1 , point p2);
void show()
{
cout<<x<<","<<y<<endl;
}
};
point operator+(point p1 , point p2)
{
return point(p1.x + p2.x, p1.y + p2.y);
}
int main()
{
point c1(2,3),c2(5,6);
c1=c1+c2;
c1.show ();
return 0;
一个参数的,即把重载函数声明为成员函数
#include<iostream.h>
class point
{
private:
int x;
int y;
public:
point(int a,int b):x(a),y(b)
{}
point operator +(point &p2);
void show()
{
cout<<x<<","<<y<<endl;
}
};
point point:: operator+( point &p2)
{
return point(x + p2.x, y + p2.y);
}
两个参数时,即把成员函数声明为友元函数
#include<iostream.h>
class point
{
private:
int x;
int y;
public:
point(int a,int b):x(a),y(b)
{}
friend point operator +(point p1 , point p2);
void show()
{
cout<<x<<","<<y<<endl;
}
};
point operator+(point p1 , point p2)
{
return point(p1.x + p2.x, p1.y + p2.y);
}
int main()
{
point c1(2,3),c2(5,6);
c1=c1+c2;
c1.show ();
return 0;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询