Constant member function in C++ in Hindi – example

Hello दोस्तों! आज हम इस पोस्ट में Constant member function in C++ in Hindi (कांस्टेंट मेम्बर फंक्शन क्या है?) के बारें में पढेंगे. और इसके example को भी देखेंगे. तो चलिए शुरू करते हैं:-

Constant member function in C++ in Hindi

C++ में, Constant member function एक फंक्शन होता है जिसे program में constant के रूप में declare किया जाता है. इस function के द्वारा call किये गये objects को change नहीं किया जा सकता है. Constant के रूप में declare करने के लिए const कीवर्ड का प्रयोग किया जाता है.

हमें ज्यादातर const कीवर्ड का प्रयोग करना चाहिए जिससे कि objects को गलती से change करने से बचा जा सके.

const member functions को किसी भी प्रकार के object के द्वारा call किया जा सकता है. जबकि non const member functions को केवल non const objects के द्वारा ही call किया जा सकता है.

Const member function का syntax नीचे दिया गया है:-

datatype function_name const();

इसका example

#include<iostream>
using namespace std;
class Demo {
int val;
public:
Demo(int x = 0) {
val = x;
}
int getValue() const {
return val;
}
};
int main() {
const Demo d(28);
Demo d1(8);
cout << "The value using object d : " << d.getValue();
cout << "\nThe value using object d1 : " << d1.getValue();
return 0;
}

इसका आउटपुट:

The value using object d : 28
The value using object d1 : 8

references:- https://www.tutorialspoint.com/const-member-functions-in-cplusplus

constant member function in c++ in hindi

निवेदन:- अगर आपके लिए यह article useful रहा हो तो इसे अपने friends और classmates के साथ अवश्य share कीजिये. जिससे कि उनकी भी help हो पायें. अगर आपका इससे related कोई सवाल हो तो उसे कमेंट करके बताइए. Thanks.

Leave a Comment