Concepts of Class and Object

Class એ blueprint છે જે નક્કી કરે છે કે કોઈ વસ્તુ પાસે કયો data અને કયું વર્તન હશે; object એ એ જ blueprint પરથી બનેલી એક સાચી વસ્તુ છે, અને memory ફક્ત object જ રોકે છે.

9 min read · 11 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

આ શું કામ મહત્વનું છે

એક mark ને float માં અને એક નામ ને character array માં રાખતાં તમને આવડે છે. પણ student એટલે એક જ value નહીં. Student એટલે એક roll number, એક નામ, ત્રણ વિષયના marks, અને પોતાનું result જાતે print કરવાની આવડત. આ બધું અલગ અલગ variables માં રાખવું 10 students સુધી ચાલે અને 10,000 પર વિખેરાઈ જાય. Class એ આનો C++ વાળો જવાબ છે, અને પછીના બે units નો લગભગ દરેક topic class ને લગતો કોઈ ને કોઈ નિયમ જ છે.

Theory

એક template, હજારો ID cards

તમારી college એ ID card નું એક જ layout બનાવ્યું છે: photo નું ખાનું, નામની લીટી, roll number ની લીટી, એક barcode. એ design પોતે ID card નથી. એને library ના gate પર swipe ના કરી શકાય. Office એ જ design પરથી હજારો સાચાં cards છાપે છે, દરેક પર જુદો photo અને જુદો roll number, પણ ખાનાં એનાં એ જ અને એ જ જગ્યાએ.

એ design એટલે class. છપાયેલું દરેક card એટલે object.

Theory

બે વ્યાખ્યાઓ

Class એ user-defined data type છે જે data (member variables) અને એ data પર કામ કરતાં functions (member functions) ને એક જ એકમમાં બાંધે છે. એ blueprint છે, વસ્તુ નથી.

Object એ class નું instance છે: એ જ blueprint પરથી બનેલી એક સાચી entity, પોતાની values સાથે.

પરીક્ષામાં આમ લખો: Student એ class છે, s1 અને s2 એ class નાં objects છે.

Theory

પરીક્ષક જે લીટી સાંભળવા માંગે છે

Class લખવાથી કોઈ memory allocate થતી નથી. class Student { int roll; float marks; }; કશું જ રોકતી નથી. એ ફક્ત compiler ને કહે છે કે Student કેવો દેખાશે.

Memory ત્યારે બને છે જ્યારે તમે object બનાવો. Student s1; લખતાં જ s1 ને પોતાનું roll અને પોતાનું marks મળે છે. બે objects એટલે data ની બે અલગ નકલો, જ્યારે member functions એક જ વાર સંઘરાય છે અને બધા objects એને વહેંચીને વાપરે છે.

At a glance

Class સામે Object, પરીક્ષાનો જવાબ

પાસુંClassObject
એ છે શુંએક blueprint કે templateએના પરથી બનેલું instance
MemoryDeclare કરતાં કશું નહીંદરેક object માટે allocate થાય
કેટલાંએક જ વાર લખાયતમે બનાવો એટલાં
કેવી રીતે બનેclass Student { ... };Student s1, s2;
શું સંઘરેData ની રચનાસાચી values

Practical

એક class, બે objects

#include <iostream>
using namespace std;

class Student {          // the blueprint: nothing exists yet
  public:
    int roll;            // member variable
    float marks;         // member variable

    void show() {        // member function
        cout << roll << " scored " << marks << endl;
    }
};

int main() {
    Student s1, s2;      // two objects, NOW memory is allocated

    s1.roll = 101;  s1.marks = 78.5;
    s2.roll = 102;  s2.marks = 64.0;

    s1.show();           // 101 scored 78.5
    s2.show();           // 102 scored 64
    return 0;
}

Think first

મનમાં trace કરો

s1 અને s2 એ એક જ class Student નાં બે objects છે. તમે s1.roll = 101 ગોઠવો છો, અને પછી s2.roll = 202 ચલાવો છો. પાંચ સેકન્ડ લો: હવે s1.roll શું છે, અને શા માટે?

Show the answer

s1.roll હજુ પણ 101 જ છે. દરેક object ને દરેક member variable ની પોતાની નકલ મળે છે, એટલે s2 વાટે લખવાથી s1 ને કશું થતું નથી.

ફક્ત member functions વહેંચાય છે: show() ની એક જ નકલ memory માં રહે છે અને બંને objects એનાથી જ કામ ચલાવે છે. જો તમે 202 ધાર્યું હોય, તો તમે class ને એક વહેંચાયેલો ડબ્બો સમજી બેઠા હતા, અને આ topic ની સૌથી સામાન્ય ભૂલ એ જ છે.

Watch out

ફાંદો: class ના નામને object ગણી લેવું

Student.roll = 101; compile નહીં થાય. Student એ type છે, વસ્તુ નથી, બરાબર એ જ રીતે જેમ x = 5; ને બદલે int = 5; લખવું.

પહેલાં object બનાવો, પછી એ object પર dot operator વાપરો: Student s1; અને પછી s1.roll = 101;. Viva માં આ પકડવું એ સૌથી ઝડપી રીત છે એ બતાવવાની કે બે શબ્દોનો ભેદ તમને સાફ છે.

Quiz

`class Student { int roll; float marks; };` હમણાં જ compile થઈ. આ ઘડીએ `roll` અને `marks` માટે કેટલી memory allocate થઈ છે?

  1. કશું નહીં, કારણ કે હજુ કોઈ object બન્યો જ નથી
  2. `roll` માટે 4 bytes અને `marks` માટે 4 bytes
  3. એક Student જેટલી, compiler એ અગાઉથી રોકી લીધી
  4. એ તમે આગળ કેટલા objects બનાવો છો એના પર આધાર રાખે
Show the answer

કશું નહીં, કારણ કે હજુ કોઈ object બન્યો જ નથી

Class declaration ફક્ત એક type નું વર્ણન કરે છે, એટલે allocate કશું થતું નથી. "દરેક માટે 4 bytes" અને "એક Student જેટલી", બંને જવાબ એવું માની લે છે કે class પોતે values સંઘરે છે, અને આ topic ની સૌથી સામાન્ય ભૂલ એ જ છે. છેલ્લો વિકલ્પ ભાવમાં નજીક છે પણ જગ્યા ખોટી બતાવે છે: class વધતી નથી, દરેક Student s1; એ જ ક્ષણે એ object ની પોતાની નકલ allocate કરે છે.

Formula

આ આગળ ક્યાં જાય છે

આગળનો આખો syllabus આ બે શબ્દોના નિયમો જ છે. Unit 2 નક્કી કરે છે કે object ના data ને કોણ અડી શકે (public, private, protected), પછી constructors અને destructors વાટે દરેક object ને જન્મ અને અંત આપે છે. Unit 3 માં એક જ call પર જુદી જુદી classes નાં objects જુદો જવાબ આપે છે.

પરીક્ષામાં "define class and object with an example" એ 3 થી 4 ગુણનો પાકો સવાલ છે. બંને વ્યાખ્યાઓ, એક નાનું code skeleton, અને memory વાળી લીટી, ત્રણેય લખો.

Summary

Key takeaways

  • Class એ data members અને member functions ને એક user-defined type માં બાંધે છે. એ blueprint છે.
  • Object એ class નું instance છે, અને ખરેખર memory એ જ રોકે છે.
  • Class declare કરવાથી કશું allocate થતું નથી; Student s1; થી s1 માટે થાય છે.
  • દરેક object પોતાના data members ની પોતાની નકલ રાખે છે, જ્યારે member functions એક જ વાર સંઘરાઈને વહેંચાય છે.
  • Members સુધી object ના dot operator થી પહોંચો, class ના નામથી ક્યારેય નહીં.
  • યાદ રાખવાની રીત: એક template, હજારો ID cards.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Concepts of OOPS

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Concepts of Class and Object · Object Oriented Programming and Data Structures (OOPs & D.S.) · Gri-Learn