| by Arround The Web | No comments

C++ Const Function Examples

This article focuses on the “const” keyword that is used with the function in C++. The “const” keyword is referred to as the constant value that we cannot modify during the execution of the program. The “const” functions in C++ are those functions whose data members are restricted to be changed in their class. The “cont” functions are efficiently used as they can avoid unexpected modifications to the object’s value.

Example 1: Program of a Const Member Function in C++

Here, we have a simple program of the const member function:

#include<iostream>
using namespace std;
class MyClass {
    int num;
public:
    MyClass(int n = 0) {
        num = n;
    }
    int getValue() const {
        return num;
    }
};
int main() {
    const MyClass c(30);
    MyClass c1(5);
        cout << "Number using object c : " << c.getValue();
        cout << "\nNumber using object c1 : " << c1.getValue();
    return 0;
}

Initially, we define the header file which enables the program input/output streams. Then, we set the “MyClass” class where we declare a “num” variable and the member functions of the MyClass() function and the getVal() function. Inside the MyClass() function, we set the “n” parameter with the value of “0”. Then, the “n” argument is assigned to the “num” variable. Next, we call the getVal() function with the “const” keyword, indicating that the object’s present state can’t be modified on a function call. The getVal() function returns the “num” member variable.

Finally, we reach out to the main() function. Here, we define the “c” and “c1” objects of the MyClass() function and also pass the values to these objects. The “c” object is set with the “const” keyword, indicating that the value that is assigned to this object cannot be modified.

The output of the const member function that is called by the object is shown in the following. This way, we can use the “const” keyword with the member function and the object:

Example 2: Program of a Const Member Function Outside the Class in C++

Now, we have another implementation where the const member function is defined outside of a class definition and set and retrieve a private member variable value using a const member function.

#include <iostream>
using namespace std;

class NewClass {
    int i;

public:
    void set_record(int);

    int get_record() const;
};

void NewClass::set_record(int x) { i = x; }
int NewClass::get_record() const { return i; }

int main()
{
    NewClass c;
    c.set_record(10);
    cout << c.get_record();

    return 0;
}

Here, we first establish a class, “NewClass”, where we initialize the “i” variable which is kept private. After that, we have a public keyword where the set_record() and get_record() member functions are defined. The set_record() member function is used to set the value of the “i” variable and the get_record() function is used here to return the value of the “i” variable. Note that we use the “const” keyword with the get_record() member function which represents it as a const member function, and the object state cannot be modified.  After that, we have a set_record() function definition to set the “i” variable value.

Similarly, we have the get_record() function definition to retrieve the “i” variable value. Then, we set the main() function where we have a declaration of the “c” object of the “NewClass” class. Then, the “set_record”, a non-const member function, is called to assign a value to the “z” variable. Moving on, we call the get_record() const member function to print the value of “i”.

The value of the private member variable is called by the const member function and is shown in the following prompt:

Example 3: Program of a Const Member Function Defined Outside the Class as an Independent Function

However, we implement another case of the const member function where the member function with the “const” keyword is called outside the class as a standalone function.

#include<bits/stdc++.h>
using namespace std;
class Equation{
    int n1,n2;
    public:
    void set_equation(int x, int y)
    {
        n1 = x;
        n2 = y;
    }
    void show_equation()
    {
        cout<<"The Equation is: "<<n1<<" + "<<n2<<"b"<<endl;
    }
    friend void funIs(const Equation);
};
void funIs(const Equation obj)
{
    cout<<"The Equation using function is: "<<obj.n1<<" + "<<obj.n2<<"b"<<endl;
}
int main()
{
    Equation obj;
    obj.set_equation(5,8);
    obj.show_equation();
    funIs(obj);
}

Here, we create the “Equation()” class and then declare the “n1” and “n2” variables. Here, we also add the set_Equation() member function to set the values of the “n1” and “n2” variables. The show_Equation() function shows the equation that is generated using these variables.

After this, we have a function declaration of funIs() which is defined using the “friend” keyword. This “friend” keyword allows us to access the private member of the “Equation” class. Next, we call the “funIs()” friend function outside the class and input the “obj” parameter of the “Equation” class as const. In the end, we have a main() function where we declare the object of the “Equation” class. Then, we set the values of the “n1” and “n2” variables using the set_Equation() function. The “n1” and “n2” variables are used to display their values using the show_Equation() function. Lastly, we call the “funIs” friend function of the “Equation” class to display the equation.

The equation and the equation using the const friend function are displayed on the prompt:

Example 4: Program of a Const Member Function to Update the Value in C++ (Worst Case)

The program demonstrates the worst scenario of the const member function where we try to modify the value of the variable called “inside the const member function”.

#include <iostream>
using namespace std;

class Data {
    int v;

public:
    void setValue(int i) { v = i; }
    int getValue() const
    {

        ++v;
        return v;
    }

};

int main()
{
    Data d;
    d.setValue(20);
    cout << endl << d.getValue();

    return 0;
}

Here, we first construct the “Data” class and declare the “v” variable within the class. After this, we set the “public” keyword and then declare the class member functions of “setValue()” which represents the constructor of the class and the getValue() which indicates the getter member function of the class. The setValue() function takes the “i” variable as a parameter. This “i” variable is assigned to the “v” member variable.

After this, we have the getValue() function definition where we retrieve the value of the “v” variable. Since the getValue() function is declared with the “const” keyword, representing that the value of the “v” variable cannot be updated in any case. However, we intentionally attempt to increment the “v” variable to change its value. When the program reaches this stage, an error is thrown. Lastly, we have the main() function call where we define the “d” object of the “Data” class and set the value of “20” for this “d” object. After that, we call the getValue() function to get the object’s “d” value.

The prompt generates the results of the previous implementation where it gives an error on “++v” because we are not allowed to modify the value of the const member function objects:

Conclusion

In conclusion, we dive into the const function of C++ which is used to avoid accidental changes in the program’s value. The const member functions in C++ are read-only functions whose modification of the objects on which it is called is not allowed. We also implemented various scenarios of the const function to demonstrate its functionality in C++.

Share Button

Source: linuxhint.com

Leave a Reply