一道C++语言的题目第一道:为了使下面的程序运行,请完成相关的类. #include"point.h" int main(){ Point p1,p2(3,3),p3(8,8),p4,p5,p6; cout << "p1:" <<p1 << endl; cout
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/04 22:45:05
![一道C++语言的题目第一道:为了使下面的程序运行,请完成相关的类. #include"point.h" int main(){ Point p1,p2(3,3),p3(8,8),p4,p5,p6; cout << "p1:" <<p1 << endl; cout](/uploads/image/z/7165461-21-1.jpg?t=%E4%B8%80%E9%81%93C%2B%2B%E8%AF%AD%E8%A8%80%E7%9A%84%E9%A2%98%E7%9B%AE%E7%AC%AC%E4%B8%80%E9%81%93%EF%BC%9A%E4%B8%BA%E4%BA%86%E4%BD%BF%E4%B8%8B%E9%9D%A2%E7%9A%84%E7%A8%8B%E5%BA%8F%E8%BF%90%E8%A1%8C%2C%E8%AF%B7%E5%AE%8C%E6%88%90%E7%9B%B8%E5%85%B3%E7%9A%84%E7%B1%BB.+%23include%26quot%3Bpoint.h%26quot%3B+int+main%28%29%7B+++++++++Point+p1%2Cp2%283%2C3%29%2Cp3%288%2C8%29%2Cp4%2Cp5%2Cp6%3B++++++++++cout+%26lt%3B%26lt%3B+%26quot%3Bp1%3A%26quot%3B+%26lt%3B%26lt%3Bp1+%26lt%3B%26lt%3B+endl%3B+++++++++cout)
一道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;
};