Notes in Hindi

C Classes and Object in Hindi

/ BCA / Programming with C and CPP

C Classes and Object in Hindi

C Classes and Object in Hindi

Introduction to C Classes and Object in Hindi

C भाषा एक procedural programming language है, यानी इसमें प्रोग्राम step-by-step instructions के रूप में लिखा जाता है। लेकिन जब हम बड़े और complex प्रोजेक्ट्स की बात करते हैं, तो वहां पर object-oriented programming (OOP) बहुत उपयोगी होती है। Object-oriented programming में मुख्य रूप से दो चीजें होती हैं: **Class** और **Object**।

लेकिन ध्यान देने वाली बात यह है कि C language खुद में पूरी तरह object-oriented language नहीं है, बल्कि ये procedural language है। फिर भी हम conceptually समझ सकते हैं कि class और object कैसे काम करते हैं, ताकि जब हम आगे C++ या Java जैसी OOP languages पढ़ें, तो ये बातें पहले से clear हों।

Defining Class and Object in C in Hindi

सबसे पहले हमें ये समझना होगा कि **Class** और **Object** का मतलब क्या होता है।

  • Class: Class एक blueprint होती है जिसमें किसी object के structure और behavior की जानकारी होती है। इसे हम एक template भी कह सकते हैं।
  • Object: Object उस class का real instance (वास्तविक रूप) होता है। जैसे class कार है, तो object Maruti, BMW, Tata जैसी कारें हैं।

C में सीधे-सीधे class की सुविधा नहीं होती, लेकिन हम structure (struct) का उपयोग करके class की तरह व्यवहार कर सकते हैं। चलिए एक उदाहरण से समझते हैं:

#include <stdio.h> #include <string.h> struct Student { int id; char name[50]; float marks; }; int main() { struct Student s1; s1.id = 101; strcpy(s1.name, "Rahul"); s1.marks = 92.5; printf("ID: %d\n", s1.id); printf("Name: %s\n", s1.name); printf("Marks: %.2f\n", s1.marks); return 0; }

ऊपर दिए गए उदाहरण में, हमने struct Student नाम का एक structure (class जैसा) बनाया है और उसका एक object s1 बनाया।
इस तरह struct का उपयोग करके हम C में class जैसा behavior पा सकते हैं।

Class Members and Object Access in Hindi

एक class (या struct) में दो प्रकार के members हो सकते हैं:

  • Data Members: ये वे variables होते हैं जो किसी object की जानकारी (data) को स्टोर करते हैं। जैसे name, id, marks आदि।
  • Member Functions: C में हम struct के अंदर functions को नहीं रख सकते (जैसा कि C++ में करते हैं), लेकिन हम struct से जुड़े functions बना सकते हैं जो उस struct के data को access करें।

C में किसी object के member को access करने के लिए dot (.) operator का उपयोग किया जाता है। उदाहरण:

struct Book { int id; char title[100]; }; int main() { struct Book b1; b1.id = 2001; strcpy(b1.title, "C Programming"); printf("Book ID: %d\n", b1.id); printf("Title: %s\n", b1.title); return 0; }

ऊपर के उदाहरण में b1 object के id और title को b1.id और b1.title से access किया गया है।
अगर object pointer हो, तब हम -> (arrow operator) का उपयोग करते हैं।

struct Book { int id; char title[100]; }; int main() { struct Book *ptr; struct Book b2; ptr = &b2; ptr->id = 3001; strcpy(ptr->title, "Learn C"); printf("Book ID: %d\n", ptr->id); printf("Title: %s\n", ptr->title); return 0; }

Limitations of Classes and Object in C in Hindi

C में class और object को पूरी तरह से implement करना संभव नहीं है क्योंकि C एक object-oriented language नहीं है। नीचे कुछ मुख्य limitations दिए गए हैं:

  • No Encapsulation: C में data को hide करने के लिए कोई built-in mechanism नहीं है जैसे कि private या protected access specifiers।
  • No Inheritance: C में आप एक structure को दूसरे से inherit नहीं कर सकते, जबकि object-oriented programming में inheritance बहुत महत्वपूर्ण feature होता है।
  • No Polymorphism: C में function overloading या virtual functions जैसी चीजें नहीं होतीं।
  • No Constructor/Destructor: C++ में objects के साथ automatically functions call होते हैं जैसे constructor और destructor, लेकिन C में ऐसा कोई system नहीं होता।
  • Function Binding: C में struct के अंदर function नहीं रख सकते, यानी member functions concept नहीं होता।

हालांकि, कुछ हद तक हम function pointers और structures की मदद से object-oriented जैसे system को simulate कर सकते हैं, लेकिन ये पूरी तरह object-oriented behavior नहीं होता।

Table: Difference between C and C++ in terms of Class and Object

Feature C C++
Class Support नहीं होता पूरी तरह होता है
Object Creation Structure द्वारा simulate किया जाता है Direct object बनता है
Encapsulation नहीं होता होता है
Inheritance नहीं हाँ
Polymorphism नहीं हाँ

Real-life Analogy to Understand Class and Object

मान लीजिए, आपके पास एक "Car" नाम की class है। इस class में कुछ common attributes हैं: color, model, speed आदि। अब इस class के आधार पर आप अलग-अलग objects बना सकते हैं जैसे Maruti, Honda, BMW। हर object का color, model, speed अलग हो सकता है, लेकिन वो सभी एक ही class (Car) से जुड़े हैं।

ठीक उसी तरह, जब हम C में struct बनाते हैं तो वो एक class की तरह behave करता है और जब हम उस struct का instance बनाते हैं, तो वह object होता है।

Important Points Summary

  • C में Class और Object को सीधे implement नहीं किया जा सकता, लेकिन struct के जरिए हम class जैसा structure बना सकते हैं।
  • struct में variables (data members) होते हैं, लेकिन functions को include नहीं किया जा सकता।
  • Object struct का instance होता है जिसे हम dot operator से access करते हैं।
  • C में object-oriented features जैसे inheritance, polymorphism, encapsulation नहीं होते।

FAQs

C में Class directly support नहीं करता क्योंकि यह एक procedural language है। लेकिन हम structure (struct) का उपयोग करके एक तरह से class का structure बना सकते हैं जिसमें data members को define किया जाता है।
C में object उस structure (struct) का instance होता है जिसे हम एक variable की तरह define करते हैं और उसके members को access कर सकते हैं। Object real-world entity को represent करता है।
नहीं, C में C++ की तरह full object-oriented support नहीं होता। हम केवल structure और function pointers की मदद से limited object-oriented behavior simulate कर सकते हैं, लेकिन inheritance, polymorphism आदि संभव नहीं हैं।
Object के members को access करने के लिए dot operator (.) का उपयोग किया जाता है। यदि object pointer के रूप में हो, तो arrow operator (->) का उपयोग होता है।
C में class और object का पूरा object-oriented support नहीं होता, जैसे: encapsulation, inheritance, polymorphism, constructor/destructor जैसी सुविधाएं C में नहीं होतीं।

Please Give Us Feedback