首页 > 编程语言 >使用C++语言特性中支持面向对象的语法,实现一个Point类来描述点的基础属性和操作。

使用C++语言特性中支持面向对象的语法,实现一个Point类来描述点的基础属性和操作。

时间:2022-09-28 23:24:03浏览次数:52  
标签:const Point int C++ return 类来 y0 x0

 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,int,C++,return,类来,y0,x0
From: https://www.cnblogs.com/zhouxv/p/16739928.html

相关文章

  • 【教程】VsCodeForCPP 最简单一键启动VsCode C/C++环境,无需任何配置
    整合VsCode以前的教程中,总有各种同学由于环境变量编译器的配置问题出现无法使用的情况,于是我将VsCode移植成绿色版本,直接整合C++编译器,全部配置为动态路径,保证即开即用......
  • C++内存对齐
    内存对齐:计算机中内存的地址空间是按照 byte 来划分的,从理论上讲对任何类型变量的访问可以从内存中的任意地址开始,但实际情况是:在访问特定类型变量的时候通常在特定的内......
  • C++基础
    1 标准输入 cin>>标准输出cou<<必须包含#include<iostream>usingnamespacestd; 2 命名空间:防止程序中的同名问题 const常量 const 类型  常量名......
  • C++11:noexcept修饰符、nullptr、原生字符串字面值
    noexcept修饰符voidfunc3()throw(int,char)//只能够抛出int和char类型的异常{//C++11已经弃用这个声明throw0;}voidBlockThrow()throw()//代表此函数不能抛......
  • C++11:智能指针
    C++11中有unique_ptr、shared_ptr与weak_ptr等智能指针(smartpointer),定义在memory中。可以对动态资源进行管理,保证任何情况下,已构造的对象最终会销毁,即它的析构函数最终......
  • C++11:std::bind
    std::bind是这样一种机制,它可以预先把指定可调用实体的某些参数绑定到已有的变量,产生一个新的可调用实体,这种机制在回调函数的使用过程中也颇为有用。C++98中,有两个函数bind......
  • 【C/C++】strlen和sizeof
    1chara[]={'c','+','+'};2charc[]="c++";3cout<<strlen(a)<<endl;4cout<<strlen(c)<<endl;5cout<<sizeof(a)<<endl;6......
  • postgis st_point报错st_point(geometry)不存在
     参考:https://blog.csdn.net/aliasone/article/details/80644306正确写法:SELECTst_x(the_geom)FROM"geo_dangerpoint_b"原因:st_point(float8,float8)SELECTS......
  • C++ 内存模型与顺序一致性
    目录顺序一致性什么是内存模型?什么是顺序一致性?强顺序与弱顺序顺序一致性与内存模型的强弱顺序C++内存顺序(memory_order)memory_order有哪些?如何使用memory_order?顺序一致......
  • C++11:强类型枚举
    C++11引入了一种新的枚举类型,即“枚举类”,又称“强类型枚举”。声明请类型枚举非常简单,只需要在enum后加上使用class或struct。如:enumOld{Yes,No};//oldstyl......