一道C++语言的题目第一道:为了使下面的程序运行,请完成相关的类. #include"point.h

一道C++语言的题目
第一道:
为了使下面的程序运行,请完成相关的类.

#include"point.h"

int main()
{
Point p1,p2(3,3),p3(8,8),p4,p5,p6;

cout << "p1:" < cout << "p2:" < cout << "p3:" <
p1.setX(10);
cout << "change p1:"<< p1 << endl << endl;

cout<< "Input x and y :";
cin >> p4;
cout << "p4:" <
p5 = 5 + p2;
cout << "p5 = 5 + p2"<< endl;
cout << "p5:" < cout << "p2:" <
if(p5 == p3)
cout <<"p5==p3" << endl << endl;
else
cout <<"p5!=p3" << endl << endl;


p6 = p3++;
cout << "p6 = p3++"<< endl;
cout << "p3:" < cout << "p6:" <
p6 = ++p3;
cout << "p6 = ++p3"<< endl;
cout << "p3:" < cout << "p6:" <
cout << endl;


system("pause");

return 0;
}


Stella_x 1年前 已收到1个回答 举报

凌小帅000003 幼苗

共回答了20个问题采纳率:85% 举报

#include
#include
using namespace std;

class Point{
public:
explicit Point(int x = 0) : m_x(x), m_y(x) {}
Point(int x , int y) : m_x(x),m_y(y) {}
Point( const Pointrhs) : m_x(rhs.m_x),m_y(rhs.m_y) {}
~Point() {}

friend std::ostreamoperator<<( std::ostreamost, const Pointrhs)
{
ost << "( " << rhs.m_x << " , " << rhs.m_y << " )";
return ost;
}
friend std::istreamoperator>>( std::istreamist, Pointrhs)
{
ist >> rhs.m_x;
ist >> rhs.m_y;
return ist;
}
Pointoperator++()
{
++m_x;
++m_y;
return *this;
}
// for point++
const Point operator++(int)
{
Point tmp(m_x,m_y);
++m_x;
++m_y;
return tmp;
}
bool operator==(const Pointrhs)
{
return rhs.m_x == m_x rhs.m_y == m_y;
}
Pointoperator=( const Pointrhs )
{
m_x = rhs.m_x;
m_y = rhs.m_y;
return *this;
}
const Pointoperator+(const Pointrhs)
{
Point tmp;
tmp.m_x = m_x + rhs.m_x;
tmp.m_y = m_y + rhs.m_y;
return tmp;
}
friend Pointoperator+(int lhs, const Pointrhs)
{
Point tmp;
tmp.m_x = lhs + rhs.m_x;
tmp.m_y = lhs + rhs.m_y;
return tmp;
}
Pointoperator+=(const Pointrhs)
{
m_x += rhs.m_x;
m_y += rhs.m_y;
return *this;
}

void setX(int x)
{
m_x = x;
}
void setY(int y)
{
m_y = y;
}
void set(int x, int y)
{
m_x = x;
m_y = y;
}
private:
int m_x;
int m_y;
};

1年前

3
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 16 q. 0.013 s. - webmaster@yulucn.com