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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 为成员变量提供存取函数,可以使用Google C++风格命名。具体实现方式如下: 1. 对于私有成员变量,提供get和set函数函数名应该以小写字母开头,单词之间用下划线分隔。例如: ```c++ class MyClass { private: int my_var_; public: int get_my_var() const { return my_var_; } void set_my_var(int value) { my_var_ = value; } }; ``` 2. 对于公有成员变量,可以直接访问,不需要提供存取函数。如果需要提供存取函数,可以使用与私有成员变量相同的方式。例如: ```c++ class MyClass { public: int my_var; private: int my_private_var_; public: int get_my_private_var() const { return my_private_var_; } void set_my_private_var(int value) { my_private_var_ = value; } }; ``` 使用Google C++风格命名可以提高代码的可读性和可维护性,建议在编写代码时遵循该命名规范。 ### 回答2: 成员变量是类的重要部分,在许多情况下需要提供存取函数来访问和修改它们。Googlec 风格的命名规范建议将存取函数分别命名为 get_成员变量名() 和 set_成员变量名(),以明确函数的作用。 对于成员变量的存取函数,需要注意以下几点: 1. 成员变量应该声明为 private,以避免直接修改成员变量导致对象状态不一致。 2. 存取函数应该声明为 public,以便从外部访问类成员变量。 3. get_函数应该返回成员变量的值,而set_函数应该修改成员变量的值。 4. get_函数的参数为空,而set_函数应该有一个参数用于指定成员变量的新值。 以一个具体的例子为说明:假设有一个类 Student,其包含成员变量 name 和 age,那么存取函数的定义应该如下: ``` class Student { private: string name; int age; public: // get 函数 string get_name() const { return name; } int get_age() const { return age; } // set 函数 void set_name(const string &new_name) { name = new_name; } void set_age(int new_age) { age = new_age; } }; ``` 通过这样规范的存取函数,可以保护对象的状态,同时也方便外部读取和修改成员变量的值。同时使用 Googlec 风格的命名规范,也可以使函数命名清晰明了,减少命名冲突的风险。 ### 回答3: 成员变量的存取函数是用于访问和修改成员变量的方法。通过提供存取函数,可以保护成员变量,防止直接访问和修改,保证代码的封装性和安全性。Googlec风格命名是一种代码风格,提供了一套标准的、易于阅读和理解的命名规则。 为成员变量提供存取函数,通常需要提供两个函数:一个是获取成员变量的值的函数,另一个是设置成员变量的值的函数。例如,对于一个名为age的int类型成员变量: ``` class Person { public: int GetAge() const { return age_; } void SetAge(int age) { age_ = age; } private: int age_; }; ``` 上述代码,GetAge函数用于获取age成员变量的值,SetAge函数用于设置age成员变量的值。注意,在函数应该使用尽量简单和清晰的词语,以便于其他开发人员的理解。 在Googlec风格命名函数名应该使用小写字母开头,采用单词小写加下划线的形式命名,例如: ``` class Person { public: int get_age() const { return age_; } void set_age(int age) { age_ = age; } private: int age_; }; ``` 通过遵循Googlec风格命名,代码的可读性和可维护性可以得到大大提高,也为其他开发人员提供了更好的使用体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值