100% found this document useful (2 votes)
264 views

Const. Member Function

Constant member functions can be declared using the const keyword and are used to get data member values without allowing modification. The example class Numbers contains private data members a and b, setter functions to modify them, and constant getter functions to return values without changing a or b. The main function declares a Numbers object, sets a and b to 100, and prints their values using the constant getter functions.

Uploaded by

Ipsha Guha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
264 views

Const. Member Function

Constant member functions can be declared using the const keyword and are used to get data member values without allowing modification. The example class Numbers contains private data members a and b, setter functions to modify them, and constant getter functions to return values without changing a or b. The main function declares a Numbers object, sets a and b to 100, and prints their values using the constant getter functions.

Uploaded by

Ipsha Guha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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.

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.

#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: 100

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy