T3-4 – Class and Object

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



Support translation: http://amzn.to/1Z7d5oc
——————————————————————————————————————————————————
T3-4 – Class and Object
Object = DATA + METHODS
Methods encapsulate data so that
the data can be manipulated only through the methods.

Methods are routines (subprograms implemented as Functions/Procedures) responsible to manipulate data of the object.
Definition: An OBJECT has state, behaviour and identity. Structure and behaviour of similar objects are defined in their common CLASS. Object is INSTANCE of the class. (Terms instance and object are interchangeable).
CLASS: defines structure (data fields and their types) and behaviour (methods) of similar objects.
Example:
CLASS Personnel { /*———————-DATA—————*/ private:
char *name[25]; // data variables also called Instance Variables
int socSecNo;
float salary;

/*———————METHODS——–*/
public; //——-Interface to the user of the class…… const char *employeeName(); const int employeeSocSecNo();
protected: //…hidden methods towards the user of the class…. void setEmployeeName(char *name); void setEmployeeSocSecNo(int Number); void setEmployeeSalary(float sal); const float employeeSalary();
};

Object IDENTITY (object must be created)
The above class represents common structure of many objects. From this point of
view it can be compared to data TYPE.
One particular object (of the given class) is INSTANCE of the class:

E.g.
Personnel Pers_1, Pers_2, Pers_3; …this declaration creates three different variables
which represent INSTANCES (objects) of the class.
The above objects, however, do NOT have their identity yet. The objects must be
CREATED (usually on a heap):

E.g.
new( Pers_1);
new( Pers_3);

Rem: A special method, frequently called constructor, is dedicated to the object creation and also to initialization of object’s data fields (putting the object into initial state).
Summary: OBJECT = INSTANCE of the class (variable of the given class type). IDENTITY = object created (most frequently on the heap).
Object STATE (current values of data fields)
The current values of the object’s data fields determine object’s state.
Initial state is most frequently gained when the object is created by calling the
Constructor method. This assigns some initial values into the object’s data fields.

During the process the data values might be changed. It is important to eliminate unauthorized change of the object state. The data part of the object should be encapsulated.
E.g.
Refer to class Personnel clause private: allows only methods of the class to change the values of the data fields (all functions declared under the clause protected).
clause public: interface part – the only part visible to the user (client) of the
object. The functions can be called to get employee’s name and social security
number. In addition the functions return constant value, i.e. even the returned
value can not be changed by the user.

clause protected: Employee’s salary is accessible through the function
employeeSalary(), but, being in protected part, the function can be called only
by limited (special) users – friends or by some other methods of the class
itself.

Object BEHAVIOUR (methods determine the behaviour)
Behaviour is how the object acts and reacts, in terms of its state changes and
message passing.
Message: An operation which one object performs upon another, although the
mechanism used is somewhat different.

E.g. A client object (if allowed) might invoke the operation Append (i.e. send a message Append) to grow the Queue object. This causes change of the Queue’s state. A client might also invoke an operation Length_Of (i.e. send a message Length_Of) which returns a length of the queue without the change of Queue’s state.

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

Appoġġ traduzzjoni: http://amzn.to/1Z7d5oc
——————————————————————————————————————————————————
T3-4 – Klassi u Għan
Għan = DATA + METODI
Metodi jirrappreżentaw dejta sabiex
id-data jkunu jistgħu jiġu mmanipulati biss permezz tal-metodi.

Metodi huma rutini (subprograms implimentati kif Funzjonijiet / Proċeduri) responsabbli li jimmanipulaw id-data ta ‘l-oġġett.
Definizzjoni: Oġġett għandu istat, l-imġiba u l-identità. Struttura u l-imġieba ta ‘oġġetti simili huma mfissra fl KLASSI komuni tagħhom. Għan huwa ISTANZA tal-klassi. (Termini istanza u oġġett huma interkambjabbli).
KLASSI: jiddefinixxi istruttura (oqsma tad-data u t-tipi tagħhom) u l-imġieba (metodi) ta ‘oġġetti simili.
eżempju:
KLASSI Persunal {/ * ———————- DATA ————— * / privata:
char * Isem [25]; varjabbli ta ‘data // imsejħa wkoll Varjabbli Istanza
int socSecNo;
float salarju;

/ * ——————— METODI ——– * /
pubblika; // ——- Interface għall-utent tal-€ | â classâ € | char CONST * employeeName (); int CONST employeeSocSecNo ();
protetti: // â € |hidden metodi lejn l-utent tal-€ classâ |. setEmployeeName null (char * isem); setEmployeeSocSecNo null (Numru int); setEmployeeSalary null (SAL float); float CONST employeeSalary ();
};

Għan IDENTITÀ (oġġett iridu jinħolqu)
Il-klassi ta ‘hawn fuq jirrappreżenta struttura komuni ta’ ħafna oġġetti. Minn dan il-punt ta ‘
tarah tistax tiġi mqabbla ma TIP data.
Wieħed oġġett partikolari (tal-klassi mogħtija) huwa ISTANZA tal-klassi:


Persunal Pers_1, Pers_2, Pers_3; dikjarazzjoni € |this joħloq tliet fatturi varjabbli differenti
li jirrappreżentaw KAŻIJIET (oġġetti) tal-klassi.
L-oġġetti ta ‘hawn fuq, madankollu, ma jkollu l-identità tagħhom għadu. L-oġġetti għandhom ikunu
STABBILITI (normalment fuq borġ):


ġdid (Pers_1);
ġdid (Pers_3);

Rem: Metodu speċjali, spiss imsejħa kostruttur, huwa ddedikat għall-ħolqien oġġett u wkoll biex inizjalizzazzjoni tal objectâ € ™ s taqsimiet tad-dejta (tqegħid l-oġġett fis istat inizjali).
Sommarju: L-GĦAN = ISTANZA tal-klassi (varjabbli tat-tip klassi partikolari). IDENTITÀ = oġġett maħluq (aktar spiss fuq il-borġ).
ISTAT oġġett (valuri kurrenti ta ‘oqsma tad-dejta)
Il-valuri attwali tal-objectâ € ™ s oqsma tad-data jiddeterminaw objectâ € ™ s-istat.
istat inizjali huwa l-aktar spiss miksuba meta l-oġġett hija maħluqa billi ċċempel il-
Metodu kostruttur. Din il jassenja xi valuri inizjali fil-objectâ € ™ s oqsma tad-data.

Matul il-proċess il-valuri tad-dejta jista ‘jinbidel. Huwa importanti li jiġu eliminati bidla fil mhux awtorizzat ta ‘l-istat oġġett. Il-parti tad-data tal-oġġett għandu jiġi inkapsulati.

Irreferi għall-klassi klawsola Persunal privat: jippermetti biss il-metodi tal-klassi li jibdlu l-valuri tal-oqsma tad-dejta (l-funzjonijiet kollha ddikjarati taħt il-klawsola protetta).
Klawsola pubbliku: parti interface â € “”l-unika parti viżibbli għall-utent (klijent) tar-
oġġett. Il-funzjonijiet jistgħu jiġu msejħa biex jiksbu l-isem u soċjali employeeâ € ™ s-sigurtà
in-numru. Barra minn hekk il-funzjonijiet ritorn valur kostanti, jiġifieri anki l-rritornat
valur ma jistax jinbidel mill-utent.

Klawsola protetti: employeeâ € ™ s-salarju huwa aċċessibbli permezz tal-funzjoni
employeeSalary (), iżda, parzjalment protett, il-funzjoni jistgħu jiġu msejħa biss
mill-utenti limitata (speċjali) â € “”ħbieb jew minn xi metodi oħra tal-klassi
innifsu.