博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C ++ | 用setter和getter方法创建一个类
阅读量:2535 次
发布时间:2019-05-11

本文共 1279 字,大约阅读时间需要 4 分钟。

In the below program, we are creating a C++ program to create a class with setter and getter methods.

在下面的程序中,我们正在创建一个C ++程序,以使用setter和getter方法创建一个类

#include 
using namespace std;// class definitionclass Pofloat {
  private: // private data members float x, y;  public: // public member functions void set_xy(float x, float y); float get_x(); float get_y();};// member functions definitionsvoid Pofloat::set_xy(float x, float y) {
this -> x = x; this -> y = y;}float Pofloat::get_x() {
return x;}float Pofloat::get_y() {
return y;}// main functionint main() {
// creating objects Pofloat objP; //setting the values objP.set_xy(36.0f, 24.0f); //getting the values cout << "objP is " << objP.get_x(); cout << ", " << objP.get_y() << endl; //setting the values again objP.set_xy(1.2f, 3.4f); //getting the values again cout << "objP is " << objP.get_x(); cout << ", " << objP.get_y() << endl; return 0;}

Output

输出量

objP is 36, 24objP is 1.2, 3.4

See the program – here method set_xy() which is a setter method that is using to set the value of x and y and get_x(), get_y() are the getter methods that are using to get the value of x and y.

请参见程序–这里的set_xy()方法是一个setter方法,用于设置x和y的值,以及get_x() , get_y()是用于获取x和y值的getter方法。

翻译自:

转载地址:http://hcozd.baihongyu.com/

你可能感兴趣的文章
第一次作业
查看>>
“==”运算符与equals()
查看>>
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>