c++成员函数const_C ++中的const成员函数

news/2024/7/20 2:13:39 标签: c++, java, python, 小程序, ios

c++成员函数const

C ++常量成员函数 (C++ Const Member Functions)

A constant (const) member function can be declared by using const keyword, it is used when we want a function that should not be used to change the value of the data members i.e. any type of modification is not allowed with the constant member function.

常量(const)成员函数可以使用const关键字声明,当我们想要一个不应用于更改数据成员值的函数时,即使用常量 成员函数时,不能使用任何类型的修改。

Example:

例:

Here, we have a class named "Numbers" with two private data members a and b and 4 member functions two are used as setter functions to set the value of a and b and 2 are constant members functions which are using as getter functions to get the value of a and b.

在这里,我们有一个名为“ Numbers”的类,具有两个私有数据成员a和b和4个成员函数,两个用作设置函数来设置a和b的值,而2是常量成员函数,用作getter函数来获取a和b的值。

Program:

程序:

#include <iostream>
using namespace std;

class Numbers
{
    private:
        int a;
        int b;
    public:
        Numbers() {a = 0; b = 0;}
        
        //setter functions to set a and b
        void set_a(int num1)
        {
            a = num1;
        }
        void set_b(int num2)
        {
            b = num2;
        }
        
        //getter functions to get value of a and b
        int get_a(void) const
        {
            return a;
        }
        int get_b(void) const
        {
            return b;
        }
};

//Main function
int main()
{
    //declaring object to the class
    Numbers Num;
    //set values
    Num.set_a(100);
    Num.set_b(100);
    
    //printing values
    cout<<"Value of a: "<<Num.get_a()<<endl;
    cout<<"Value of b: "<<Num.get_b()<<endl;
    
    return 0;
}

Output

输出量

Value of a: 100
Value of b: 200


翻译自: https://www.includehelp.com/cpp-programs/const-member-functions.aspx

c++成员函数const


http://www.niftyadmin.cn/n/1254099.html

相关文章

phalcon使用Redis发布订阅(pub/sub)命令介绍

Redis 发布订阅(pub/sub)是一种消息通信模式&#xff1a;发送者(pub)发送消息&#xff0c;订阅者(sub)接收消息。 一&#xff1a;命令介绍 PSUBSCRIBE pattern [pattern1 ....] 说明&#xff1a;订阅一个或多个符合给定模式的频道&#xff0c;每个模式以*作为匹配符参数&#x…

php多图上传 预览删除,layui+django 多组图片上传、预览、删除、打标签

{% csrf_token %}{{text}}选择图片layui.use([upload, jquery], function(){var upload layui.upload;var $ layui.jquery;//执行实例var uploadInst upload.render({elem: #test1, //绑定元素multiple: true, //多图上传method: POST,auto: false, //关闭自动上传data:…

java bitset_Java BitSet xor()方法与示例

java bitsetBitSet类xor()方法 (BitSet Class xor() method) xor() method is available in java.util package. xor()方法在java.util包中可用。 xor() method is used to perform logical XOR between this BitSet and the given BitSet. xor()方法用于在此BitSet和给定的Bit…

IPhone微信H5用Video标签播放不了视频

H5用Video标签播放视频 视频在安卓上可以正常播放&#xff0c;在苹果上却不能播放。 因为用了文件服务站点&#xff0c;而且不支持断点下载 把文件服务改成支持断点下载即可 断点下载参考&#xff08;C#&#xff09;转载于:https://www.cnblogs.com/chinshin/p/10057740.html

matlab生成主对角占优,matlab实现判断是否能否生成严格对角占优矩阵

如题&#xff1a;function X IsStrictDiagMatrix(A)% input: A matrix% output: The matrix after transformation% if the matrix is not a square matrix, return errorif size(A, 1) ~ size(A, 2)error(It is not a square matrix);end% get the size of A and set the siz…

数据库 嵌套查询_联接操作与嵌套查询 数据库管理系统

数据库 嵌套查询加盟业务 (Join operations) Join operations and nested queries both works to combine the data, which is allocated in different tables to make a single result. Both the join and nested query operations are similar but there are some difference…

php bt种子转换电驴地址,bt转换ed2k_BT文件转磁力链接工具 BT种子文件转换成ed2k链接...

bt文件转磁力链接工具其实就是一个bt转换ed2k工具&#xff0c;可以协助你将BT种子转换成为ed2k磁力链接。。BT种子文件是个文件&#xff0c;它不是个链接。。而ed2k是个链接&#xff0c;一般我们习惯的叫它磁力链接。。其实磁力链接就是BT种子的一种样式。。其实本质上还是BT下…

如何优雅地链式取值

开发中&#xff0c;更多地方用到链式操作是我写这边文章的初衷。如&#xff1a; res.data.goods.list[0].price 但是&#xff0c;使用链式操作之后遇到类似于Uncaught TypeError: Cannot read property goods of undefined 这种错误也是再正常不过了&#xff0c;如果说是res数据…