实验任务2
使用C++语言特性中支持面向对象的语法,实现一个Point类来描述点的基础属性和操作。
1 #include<iostream> 2 using namespace std; 3 class Point { 4 public: 5 Point(int x0 = 1, int y0 = 2); 6 Point(const Point& p); 7 ~Point() = default; 8 int get_x()const { return x; } 9 int get_y()const { return y; } 10 void show()const; 11 private: 12 int x, y; 13 }; 14 Point::Point(int x0, int y0) :x{ x0 }, y{ y0 }{ 15 cout << "constrctor called" << endl; 16 } 17 Point::Point(const Point& p) : x{ p.x }, y{ p.y }{ 18 cout << "copy constrctor called" << endl; 19 } 20 void Point::show()const { 21 cout << "(" << x << "," << y << ")" << endl; 22 } 23 int main() { 24 Point p1; 25 p1.show(); 26 Point p2(4, 5); 27 p2.show(); 28 Point p3 = p2; 29 p3.show(); 30 Point p4{ p3 }; 31 p4.show(); 32 cout << p4.get_x() << endl; 33 }
更改数据后
标签:const,Point,对象,return,int,实验,y0,x0 From: https://www.cnblogs.com/zhouxv/p/16740301.html