T3-4 — Class and Object

 —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  — —











Sup­port trans­la­tion: http://amzn.to/1Z7d5oc
 — —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  — —
T3-4 — Class and Object
Object = DATA + METHODS
Meth­ods encap­su­late data so that
the data can be manip­u­lated only through the meth­ods.

Meth­ods are routines (sub­pro­grams imple­men­ted as Functions/Procedures) respons­ible to manip­u­late data of the object.
Defin­i­tion: An OBJECT has state, beha­vi­our and iden­tity. Struc­ture and beha­vi­our of sim­il­ar objects are defined in their com­mon CLASS. Object is INSTANCE of the class. (Terms instance and object are inter­change­able).
CLASS: defines struc­ture (data fields and their types) and beha­vi­our (meth­ods) of sim­il­ar objects.
Example:
CLASS Per­son­nel { /* —  —  —  —  —  —  — -DATA —  —  —  —  — */ private:
char *name[25]; // data vari­ables also called Instance Vari­ables
int soc­SecNo;
float salary;

/* —  —  —  —  —  —  — METHODS — — –*/
pub­lic; // —  — -Inter­face to the user of the class…… con­st char *employ­ee­Name(); con­st int employ­eeSoc­SecNo();
pro­tec­ted: //…hidden meth­ods towards the user of the class…. void setEmployeeName(char *name); void setEmployeeSocSecNo(int Num­ber); void setEmployeeSalary(float sal); con­st float employ­eeSalary();
};

Object IDENTITY (object must be cre­ated)
The above class rep­res­ents com­mon struc­ture of many objects. From this point of
view it can be com­pared to data TYPE.
One par­tic­u­lar object (of the given class) is INSTANCE of the class:

E.g.
Per­son­nel Per­s_1, Per­s_2, Per­s_3; …this declar­a­tion cre­ates three dif­fer­ent vari­ables
which rep­res­ent INSTANCES (objects) of the class.
The above objects, how­ever, do NOT have their iden­tity yet. The objects must be
CREATED (usu­ally on a heap):

E.g.
new( Per­s_1);
new( Per­s_3);

Rem: A spe­cial meth­od, fre­quently called con­struct­or, is ded­ic­ated to the object cre­ation and also to ini­tial­iz­a­tion of object’s data fields (put­ting the object into ini­tial state).
Sum­mary: OBJECT = INSTANCE of the class (vari­able of the given class type). IDENTITY = object cre­ated (most fre­quently on the heap).
Object STATE (cur­rent val­ues of data fields)
The cur­rent val­ues of the object’s data fields determ­ine object’s state.
Ini­tial state is most fre­quently gained when the object is cre­ated by call­ing the
Con­struct­or meth­od. This assigns some ini­tial val­ues into the object’s data fields.

Dur­ing the pro­cess the data val­ues might be changed. It is import­ant to elim­in­ate unau­thor­ized change of the object state. The data part of the object should be encap­su­lated.
E.g.
Refer to class Per­son­nel clause private: allows only meth­ods of the class to change the val­ues of the data fields (all func­tions declared under the clause pro­tec­ted).
clause pub­lic: inter­face part – the only part vis­ible to the user (cli­ent) of the
object. The func­tions can be called to get employee’s name and social secur­ity
num­ber. In addi­tion the func­tions return con­stant value, i.e. even the returned
value can not be changed by the user.

clause pro­tec­ted: Employee’s salary is access­ible through the func­tion
employ­eeSalary(), but, being in pro­tec­ted part, the func­tion can be called only
by lim­ited (spe­cial) users – friends or by some oth­er meth­ods of the class
itself.

Object BEHAVIOUR (meth­ods determ­ine the beha­vi­our)
Beha­vi­our is how the object acts and reacts, in terms of its state changes and
mes­sage passing.
Mes­sage: An oper­a­tion which one object per­forms upon another, although the
mech­an­ism used is some­what dif­fer­ent.

E.g. A cli­ent object (if allowed) might invoke the oper­a­tion Append (i.e. send a mes­sage Append) to grow the Queue object. This causes change of the Queue’s state. A cli­ent might also invoke an oper­a­tion Length_Of (i.e. send a mes­sage Length_Of) which returns a length of the queue without the change of Queue’s state.

“ —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  — —

Appoġġ traduzz­joni: http://amzn.to/1Z7d5oc
 — —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  — —
T3-4 — Klassi u Għan
Għan = DATA + METODI
Met­odi jir­rappreżentaw dejta sabiex
id-data jkunu jist­għu jiġu mma­nip­u­lati biss per­mezz tal-met­odi.

Met­odi huma rutini (sub­pro­grams impli­ment­ati kif Fun­z­jon­iji­et / Proċed­uri) responsab­bli li jim­ma­nip­u­law id-data ta l-oġġett.
Definizz­joni: Oġġett għan­du istat, l-imġiba u l-iden­tità. Strut­tura u l-imġieba ta oġġet­ti simili huma mfis­s­ra fl KLASSI komuni tagħhom. Għan huwa ISTANZA tal-klassi. (Ter­mini istan­za u oġġett huma inter­kam­b­jab­bli).
KLASSI: jid­definixxi istrut­tura (oqs­ma tad-data u t-tipi tagħhom) u l-imġieba (met­odi) ta oġġet­ti simili.
eżem­pju:
KLASSI Per­sunal {/ * — —  —  —  —  —  — - DATA — —  —  — — * / privata:
char * Isem [25]; var­jab­bli ta data // imse­jħa wkoll Var­jab­bli Istan­za
int soc­SecNo;
float salar­ju;

/ * — —  —  —  —  — — METODI — — – * /
pubb­lika; // — — - Inter­face għall-utent tal-€ | â classâ € | char CONST * employ­ee­Name (); int CONST employ­eeSoc­SecNo ();
pro­tet­ti: // â € |hid­den met­odi lejn l-utent tal-€ classâ |. setEm­ploy­ee­Name null (char * isem); setEm­ploy­eeSoc­SecNo null (Num­ru int); setEm­ploy­eeSalary null (SAL float); float CONST employ­eeSalary ();
};

Għan IDENTITÀ (oġġett iridu jinħolqu)
Il-klassi ta hawn fuq jir­rappreżenta strut­tura komuni ta’ ħafna oġġet­ti. Minn dan il-punt ta ’
tarah tistax tiġi mqabbla ma TIP data.
Wieħed oġġett partikolari (tal-klassi mogħti­ja) huwa ISTANZA tal-klassi:


Per­sunal Per­s_1, Per­s_2, Per­s_3; dikjarazz­joni € |this joħ­loq tli­et fat­turi var­jab­bli dif­fer­enti
li jir­rappreżentaw KAŻIJIET (oġġet­ti) tal-klassi.
L-oġġet­ti ta hawn fuq, madankollu, ma jkollu l-iden­tità tagħhom għadu. L-oġġet­ti għand­hom ikunu
STABBILITI (nor­mal­ment fuq borġ):


ġdid (Per­s_1);
ġdid (Per­s_3);

Rem: Met­odu speċ­jali, spiss imse­jħa kostrut­tur, huwa dded­ikat għall-ħolqi­en oġġett u wkoll biex iniz­jalizza­zz­joni tal objectâ € ™ s taqsi­mi­et tad-dejta (tqegħid l-oġġett fis istat iniz­jali).
Som­mar­ju: L-GĦAN = ISTANZA tal-klassi (var­jab­bli tat-tip klassi partikolari). IDENTITÀ = oġġett maħluq (aktar spiss fuq il-borġ).
ISTAT oġġett (valuri kur­renti ta oqs­ma tad-dejta)
Il-valuri attwali tal-objectâ € ™ s oqs­ma tad-data jid­de­terminaw objectâ € ™ s-istat.
istat iniz­jali huwa l-aktar spiss miksuba meta l-oġġett hija maħluqa bil­li ċċem­pel il–
Met­odu kostrut­tur. Din il jassen­ja xi valuri iniz­jali fil-objectâ € ™ s oqs­ma tad-data.

Mat­ul il-proċess il-valuri tad-dejta jista jin­bidel. Huwa import­anti li jiġu elim­in­ati bid­la fil mhux awtorizzat ta l-istat oġġett. Il-parti tad-data tal-oġġett għan­du jiġi inkapsu­lati.

Irreferi għall-klassi klawsola Per­sunal privat: jip­per­met­ti biss il-met­odi tal-klassi li jibd­lu l-valuri tal-oqs­ma tad-dejta (l-fun­z­jon­iji­et koll­ha ddikjar­ati taħt il-klawsola pro­tetta).
Klawsola pubb­liku: parti inter­face â € ““l-unika parti viżib­bli għall-utent (klijent) tar–
oġġett. Il-fun­z­jon­iji­et jist­għu jiġu mse­jħa biex jiks­bu l-isem u soċ­jali employ­eeâ € ™ s-sig­urtà
in-num­ru. Bar­ra minn hekk il-fun­z­jon­iji­et ritorn valur kostanti, jiġi­fieri anki l-rrit­or­nat
valur ma jistax jin­bidel mill-utent.

Klawsola pro­tet­ti: employ­eeâ € ™ s-salar­ju huwa aċċess­ib­bli per­mezz tal-fun­z­joni
employ­eeSalary (), iżda, par­z­jalment pro­tett, il-fun­z­joni jist­għu jiġu mse­jħa biss
mill-utenti lim­it­ata (speċ­jali) â € ““ħbieb jew minn xi met­odi oħra tal-klassi
innif­su.