一道C++语言的题目第一道:为了使下面的程序运行,请完成相关的类. #include"point.h" int main(){ Point p1,p2(3,3),p3(8,8),p4,p5,p6; cout << "p1:" <<p1 << endl; cout
来源:学生作业帮助网 编辑:作业帮 时间:2024/04/29 14:37:28
一道C++语言的题目第一道:为了使下面的程序运行,请完成相关的类. #include"point.h" int main(){ Point p1,p2(3,3),p3(8,8),p4,p5,p6; cout << "p1:" <<p1 << endl; cout
一道C++语言的题目
第一道:
为了使下面的程序运行,请完成相关的类.
#include"point.h"
int main()
{
Point p1,p2(3,3),p3(8,8),p4,p5,p6;
cout << "p1:" <<p1 << endl;
cout << "p2:" <<p2 << endl;
cout << "p3:" <<p3 << endl << endl;
p1.setX(10);
cout << "change p1:"<< p1 << endl << endl;
cout<< "Input x and y :";
cin >> p4;
cout << "p4:" <<p4 << endl << endl;
p5 = 5 + p2;
cout << "p5 = 5 + p2"<< endl;
cout << "p5:" <<p5 << endl ;
cout << "p2:" <<p2 << endl << endl;
if(p5 == p3)
cout <<"p5==p3" << endl << endl;
else
cout <<"p5!=p3" << endl << endl;
p6 = p3++;
cout << "p6 = p3++"<< endl;
cout << "p3:" <<p3 << endl ;
cout << "p6:" <<p6 << endl << endl;
p6 = ++p3;
cout << "p6 = ++p3"<< endl;
cout << "p3:" <<p3 << endl ;
cout << "p6:" <<p6 << endl << endl;
cout << endl;
system("pause");
return 0;
}
一道C++语言的题目第一道:为了使下面的程序运行,请完成相关的类. #include"point.h" int main(){ Point p1,p2(3,3),p3(8,8),p4,p5,p6; cout << "p1:" <<p1 << endl; cout
#include <iostream>
#include <cstdlib>
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 Point& rhs) : m_x(rhs.m_x),m_y(rhs.m_y) {}
~Point() {}
friend std::ostream& operator<<( std::ostream& ost, const Point& rhs)
{
ost << "( " << rhs.m_x << " , " << rhs.m_y << " )";
return ost;
}
friend std::istream& operator>>( std::istream& ist, Point& rhs)
{
ist >> rhs.m_x;
ist >> rhs.m_y;
return ist;
}
Point& operator++()
{
++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 Point& rhs)
{
return rhs.m_x == m_x && rhs.m_y == m_y;
}
Point& operator=( const Point& rhs )
{
m_x = rhs.m_x;
m_y = rhs.m_y;
return *this;
}
const Point& operator+(const Point& rhs)
{
Point tmp;
tmp.m_x = m_x + rhs.m_x;
tmp.m_y = m_y + rhs.m_y;
return tmp;
}
friend Point& operator+(int lhs, const Point& rhs)
{
Point tmp;
tmp.m_x = lhs + rhs.m_x;
tmp.m_y = lhs + rhs.m_y;
return tmp;
}
Point& operator+=(const Point& rhs)
{
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;
};