C++ Non-Static Members In Hindi

📔 : C++ 🔗

Class में बिना static keyword के define किये गए सभी Properties / Methods Non Static members होते हैं , By default ये Properties / Methods Non Static ही होते हैं, और इन्हे class object के साथ ही access किया जा सकता है।

C++ Difference Between Static And Non Static Members

Static और Non Static में सबसे बड़ा difference यही होता है कि , Static Members (Property / Methods) Access करने के लिए हमें उस class का Object नहीं बनाना पड़ता है , simply ClassName और Double Colon (::) का use करके हम directly Class की Static Property / Methods Access कर सकते हैं।


जबकि Non Static Members को access करने के लिए Class का Object बनाना ही पड़ेगा , बिना Object बनाये Non Static Members (Property / Methods) को access नहीं कर पाएंगे।

C++ Non Static Member Example

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define class.
class MyCalss{
  // define variables.
  string fname = "Rahul";
  string lname = "Verma";
  // define public method so that we can access it inside main() function.
  public :
  string get_name(){
    return fname +" "+ lname;
  }
};

int main() {
  // here create class Object to access class members.
  MyCalss myobj;
  cout << myobj.get_name();
  return 0;
}
Output
Rahul Verma

Update value

हालाँकि Object initialization के बाद आप Class property को modify / update भी कर सकते हो। लेकिन उससे पहले उन properties को आपको as a public property define करना पड़ेगा ,नहीं तो आप class के बाहर directly access नहीं कर पाएंगे।

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
class MyCalss{
  // define , properties and method public.
  public :
  string get_name(){
    return fname +" "+ lname;
  }
  
  string fname = "Rahul";
  string lname = "Verma";
};

int main() {
  cout << "Before update : ";
  MyCalss myobj;
  cout << myobj.get_name();
  
  cout << endl << "After update : ";
  myobj.fname = "Ramesh";
  myobj.lname = "Yadav";
  cout << myobj.get_name();
  
  // update
  return 0;
}
Output
Before update : Rahul Verma
After update : Ramesh Yadav

C++ Access static property inside non-static method

Non Static method में आप Static Properties / Methods को access कर सकते हैं , लेकिन Static Methods में Non Static Properties / Methods को access नहीं कर सकते हैं। क्योंकि Non Static Properties / Methods को access करने के लिए , Object का initialization जरूरी होता है जबकि Static Properties / Methods को बिना Object initialization के भी access कर सकते हैं।
For Example :

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
class MyCalss{
  // define , properties and method public.
  public :
  string get_name(){
    // access static properties.
    return fname +" "+ lname;
  }
  
  static string fname;
  static string lname;
};

// now initialize static values.
string MyCalss::fname = "Rahul";
string MyCalss::lname = "Verma";

int main() {
  MyCalss myobj;
  cout << myobj.get_name();
  return 0;
}
Output
Rahul Verma

अगर आप static method में non static property या non static method access करने की कोशिश करते हैं , तो कुछ इस तरह से एरर आती है।

class MyCalss{
  // access non-static properties in static method.
  public :
  static string get_name(){
    // access non static properties in static method.
    return fname +" "+ lname;
  }
  
  string fname = "First Name";
  string lname = "Last Name";
};

Error :
In static member function 'static std::string MyCalss::get_name()':
error: invalid use of member 'MyCalss::fname' in static member function
8 |     return fname +" "+ lname;
| ^~~~~ ^~~~~

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers