编程:Point类有成员变量x,y,运算符重载++、--实现对坐标改变 30
具体要求如下:定义Point类,有坐标x,y两个整形的私有成员变量;定义成员函数Point&operator++();Pointoperator++(int);以实现对P...
具体要求如下:定义Point类,有坐标x,y两个整形的私有成员变量;定义成员函数Point& operator++();Point operator++(int);以实现对Point类重载“++”运算符,定义成函数Point& operator –-();Point operator - -(int);以实现对Point类重载“- -”运算符,实现对坐标值的改变。
展开
3个回答
展开全部
/*
定义Point类
有坐标x,y两个成员变量,
对Point类重载 “++” (自增),”--”(自减)运算符,实现对坐标值的改变
包含前置与后置
*/
#include <iostream>
using namespace std;
class Point{
public:
Point(){ }
Point(int x,int y);
~Point(){ }
Point operator++(int);//对应于a++
Point operator--(int);//对应于a--
friend ostream& operator<<(ostream& out, const Point& a);//友元函数cout<<a
friend void operator>>(istream&in, Point& a);//友元函数cin>>a
public:
int x;
int y;
};
Point::Point(int x,int y){
this->x=x;
this->y=y;
}
Point& Point::operator++(){//++a
this->x++;
this->y++;
return *this;
}
Point Point::operator++(int){//a++
Point tmp(this->x,this->y);
this->x++;
this->y++;
return tmp;
}
Point& Point::operator--(){//--a
this->x--;
this->y--;
return *this;
}
Point Point::operator--(int){//a--
Point tmp(this->x,this->y);
this->x--;
this->y--;
return tmp;
}
ostream& operator<<(ostream& out, const Point& a)
{
out<<"点为:("<<a.x<<","<<a.y<<")"<<endl;
return out;
}
void operator>>(istream&in, Point& a)
{
int px,py;
cout<<"输入x:"; cin>>px;
cout<<"输入y:"; cin>>py;
a.x=px;
a.y=py;
}
void main()
{
cout<<"第一次运行速度有点慢^_^"<<endl;
Point a;
cin>>a;
cout<<a;
cout<<"++a:"<<++a;
}
定义Point类
有坐标x,y两个成员变量,
对Point类重载 “++” (自增),”--”(自减)运算符,实现对坐标值的改变
包含前置与后置
*/
#include <iostream>
using namespace std;
class Point{
public:
Point(){ }
Point(int x,int y);
~Point(){ }
Point operator++(int);//对应于a++
Point operator--(int);//对应于a--
friend ostream& operator<<(ostream& out, const Point& a);//友元函数cout<<a
friend void operator>>(istream&in, Point& a);//友元函数cin>>a
public:
int x;
int y;
};
Point::Point(int x,int y){
this->x=x;
this->y=y;
}
Point& Point::operator++(){//++a
this->x++;
this->y++;
return *this;
}
Point Point::operator++(int){//a++
Point tmp(this->x,this->y);
this->x++;
this->y++;
return tmp;
}
Point& Point::operator--(){//--a
this->x--;
this->y--;
return *this;
}
Point Point::operator--(int){//a--
Point tmp(this->x,this->y);
this->x--;
this->y--;
return tmp;
}
ostream& operator<<(ostream& out, const Point& a)
{
out<<"点为:("<<a.x<<","<<a.y<<")"<<endl;
return out;
}
void operator>>(istream&in, Point& a)
{
int px,py;
cout<<"输入x:"; cin>>px;
cout<<"输入y:"; cin>>py;
a.x=px;
a.y=py;
}
void main()
{
cout<<"第一次运行速度有点慢^_^"<<endl;
Point a;
cin>>a;
cout<<a;
cout<<"++a:"<<++a;
}
展开全部
大概给你一个我的参考吧
/// <summary>
/// 怪物移动的方法
/// </summary>
/// <param name="hero">移动到玩家</param>
public void Move(Hero hero)
{
//移动到万家计算移动后的位置
this.CurrentLocation = new Point(hero.OriginalLocation.X, hero.OriginalLocation.Y - hero.Size.Height);
}
/// <summary>
/// 回到原位置方法
/// </summary>
public void Move()
{
//返回是将原始位置设为当前位置
this.CurrentLocation = this.OriginalLocation;
}
/// <summary>
/// 怪物移动的方法
/// </summary>
/// <param name="hero">移动到玩家</param>
public void Move(Hero hero)
{
//移动到万家计算移动后的位置
this.CurrentLocation = new Point(hero.OriginalLocation.X, hero.OriginalLocation.Y - hero.Size.Height);
}
/// <summary>
/// 回到原位置方法
/// </summary>
public void Move()
{
//返回是将原始位置设为当前位置
this.CurrentLocation = this.OriginalLocation;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/*
定义Point类
有坐标x,y两个成员变量,
对Point类重载
“++”
(自增),”--”(自减)运算符,实现对坐标值的改变
包含前置与后置
*/
#include
<iostream>
using
namespace
std;
class
Point{
public:
Point(){
}
Point(int
x,int
y);
~Point(){
}
Point
operator++(int);//对应于a++
Point
operator--(int);//对应于a--
friend
ostream&
operator<<(ostream&
out,
const
Point&
a);//友元函数cout<<a
friend
void
operator>>(istream&in,
Point&
a);//友元函数cin>>a
public:
int
x;
int
y;
};
Point::Point(int
x,int
y){
this->x=x;
this->y=y;
}
Point&
Point::operator++(){//++a
this->x++;
this->y++;
return
*this;
}
Point
Point::operator++(int){//a++
Point
tmp(this->x,this->y);
this->x++;
this->y++;
return
tmp;
}
Point&
Point::operator--(){//--a
this->x--;
this->y--;
return
*this;
}
Point
Point::operator--(int){//a--
Point
tmp(this->x,this->y);
this->x--;
this->y--;
return
tmp;
}
ostream&
operator<<(ostream&
out,
const
Point&
a)
{
out<<"点为:("<<a.x<<","<<a.y<<")"<<endl;
return
out;
}
void
operator>>(istream&in,
Point&
a)
{
int
px,py;
cout<<"输入x:";
cin>>px;
cout<<"输入y:";
cin>>py;
a.x=px;
a.y=py;
}
void
main()
{
cout<<"第一次运行速度有点慢^_^"<<endl;
Point
a;
cin>>a;
cout<<a;
cout<<"++a:"<<++a;
}
定义Point类
有坐标x,y两个成员变量,
对Point类重载
“++”
(自增),”--”(自减)运算符,实现对坐标值的改变
包含前置与后置
*/
#include
<iostream>
using
namespace
std;
class
Point{
public:
Point(){
}
Point(int
x,int
y);
~Point(){
}
Point
operator++(int);//对应于a++
Point
operator--(int);//对应于a--
friend
ostream&
operator<<(ostream&
out,
const
Point&
a);//友元函数cout<<a
friend
void
operator>>(istream&in,
Point&
a);//友元函数cin>>a
public:
int
x;
int
y;
};
Point::Point(int
x,int
y){
this->x=x;
this->y=y;
}
Point&
Point::operator++(){//++a
this->x++;
this->y++;
return
*this;
}
Point
Point::operator++(int){//a++
Point
tmp(this->x,this->y);
this->x++;
this->y++;
return
tmp;
}
Point&
Point::operator--(){//--a
this->x--;
this->y--;
return
*this;
}
Point
Point::operator--(int){//a--
Point
tmp(this->x,this->y);
this->x--;
this->y--;
return
tmp;
}
ostream&
operator<<(ostream&
out,
const
Point&
a)
{
out<<"点为:("<<a.x<<","<<a.y<<")"<<endl;
return
out;
}
void
operator>>(istream&in,
Point&
a)
{
int
px,py;
cout<<"输入x:";
cin>>px;
cout<<"输入y:";
cin>>py;
a.x=px;
a.y=py;
}
void
main()
{
cout<<"第一次运行速度有点慢^_^"<<endl;
Point
a;
cin>>a;
cout<<a;
cout<<"++a:"<<++a;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询