const类对象的用法
来源:互联网 发布:无锡算法工程师招聘 编辑:程序博客网 时间:2024/05/15 07:59
寻找了一下网上const类对象的用法,因为之前做oj题目的时候一直报错
Problem D: 平面上的点——Point类 (VI)
Time Limit: 1 Sec Memory Limit: 4 MBSubmit: 5109 Solved: 2254
[Submit][Status][Web Board]
Description
在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定。现在我们封装一个“Point类”来实现平面上的点的操作。
根据“append.cc”,完成Point类的构造方法和接口描述中的方法和函数。
接口描述:
showPoint()函数:按输出格式输出Point对象。
Point::show()方法:按输出格式输出Point对象。
Point::showSumOfPoint()方法:按格式输出程序运行至当前存在过的Point对象总数。
Point::x()方法:取x坐标。
Point::y()方法:取y坐标。
Point::x(double)方法:传参数设置x坐标并返回。
Point::y(double)方法:传参数设置y坐标并返回。
Point::setPoint(double,double)方法:设置Point对象的x坐标(第一个参数)和y坐标(第二个参数)并返回本对象。
Point::isEqual()方法:判断传入的参数与对象的坐标是否相同,相同返回true。
Point::copy()方法:传参数复制给对象。
Point::inverse()方法,有两个版本:不传参数则将对象自身的x坐标和y坐标互换;若将Point对象做参数传入,则将传入对象的坐标交换复制给对象自身,不修改参数的值。
主函数的一部分
const Point const_point(3, 3);//此处定义了一个const类对象, const_point.show(); for(int i = 0; i <= l; i++) { if(const_point.isEqual(pt[i])) { ShowPoint(const_point); ShowPoint(const_point.x(), const_point.y()); ShowPoint(Point(const_point.x(), const_point.y())); } }
在C++中,只有被声明为const的成员函数才能被一个const类对象调用。
要声明一个const类型的类成员函数,只需要在成员函数参数列表后加上关键字const
class Screen { public: char get() const; };
在类体之外定义const成员函数时,还必须加上const关键字,
因此配了一部分代码
#include <iostream>#include<iomanip>using namespace std;class Point{private: double x0,y0; int id; static int n,n2;public: Point(double xx,double yy ):x0(xx),y0(yy) { n++;n2++; id=n2; } Point (double xx):x0(xx),y0(xx) { n++;n2++; id=n2; } Point ():x0(0),y0(0) { n++;n2++; id=n2; } Point (const Point &a):x0(a.x0),y0(a.y0) { n++;n2++; id=n2; } ~Point () { n--; } void show()const //const的函数用来处理const类 { cout<<"Point["<<id<<"] : ("<<setprecision(16)<<x0<<", "<<setprecision(16)<<y0<<")"<<endl; } static void showCounter() { cout<<"Current : "<<n<<" points."<<endl; } static void showSumOfPoint() { cout<<"In total : "<<setprecision(16)<<n2<<" points."<<endl; } double x()const{return x0;} double y()const{return y0;} double x(const double xx){x0=xx;return x0;} double y(const double yy){y0=yy;return y0;} bool isEqual(Point &a)const { if(a.x0==x0&&a.x0==x0) return true; else return false; } Point& copy(Point& a) { x0=a.x0;y0=a.y0; return *this; } Point &setPoint(double xx,double yy) { x0=xx;y0=yy; return *this; } Point & inverse(){double t;t=x0;x0=y0;y0=t; return *this;} Point &inverse(Point &a){double t;t=a.x0;a.x0=a.y0;a.y0=t;return *this;} friend void ShowPoint(const Point& a); friend void ShowPoint(const double xx,const double yy);};int Point::n=0;int Point::n2=0;void ShowPoint( const Point &a){ cout<<"Point["<<a.id<<"] : ("<<setprecision(16)<<a.x0<<", "<<setprecision(16)<<a.y0<<")"<<endl;}void ShowPoint(const double xx,const double yy){ cout<<"Point : ("<<setprecision(16)<<xx<<", "<<setprecision(16)<<yy<<")"<<endl;}
const成员函数可以被具有相同参数列表的非const成员函数重载,例如,
class Screen { public: char get(int x,int y); char get(int x,int y) const; };xiaojie
1)const成员函数可以访问非const对象的非const数据成员、const数据成员,也可以访问const对象内的所有数据成员;
2)非const成员函数可以访问非const对象的非const数据成员、const数据成员,但不可以访问const对象的任意数据成员;
3)作为一种良好的编程风格,在声明一个成员函数时,若该成员函数并不对数据成员进行修改操作,应尽可能将该成员函数声明为const 成员函数。
- const类对象的用法
- C++中 const对象的用法
- const类对象和const成员函数的用法及编程技巧
- 类中const的用法
- const的普通用法及其在对象中的使用
- const对象的const函数
- 浅析类的const成员函数,类的const对象
- const对象和const函数的用法限制以及对const成员变量或引用的初始化问题
- C++中类的const成员函数和const对象
- 改类对象的const的状态
- const 修饰类函数的用法
- C++中const的用法【03】----类
- C++ const 修饰类的用法
- const 的用法
- const关键字的用法
- const的用法说明
- const限定词的用法
- const 的用法
- 【Java并发编程】之二十:并发新特性—Lock锁和条件变量(含代码)
- 开源软件怎么盈利?
- bootstrap-3-下拉菜单
- 【JDK编译】Mac OS编译OpenJDK8
- WPF 自定义窗体
- const类对象的用法
- 高次方数的尾数
- [LeetCode] 99. Recover Binary Search Tree
- 如何让孩子爱上设计模式 ——13.代理模式(Proxy Pattern)
- 阶乘尾数0的个数
- 孰优孰劣?Dubbo VS Spring Cloud性能测试大对决!
- In-cell/On-cell/OGS全贴合屏幕技术
- CentOS 7运维管理笔记(6)----Apache 基于 IP 的虚拟主机配置
- “空”的艺术-当数据为空时显示什么