The Object Oriented Paradigm; Classes and Objects: Diferență între versiuni

De la WikiLabs
Jump to navigationJump to search
Fără descriere a modificării
 
(Nu s-au afișat 4 versiuni intermediare efectuate de același utilizator)
Linia 19: Linia 19:
== Object Oriented Programming ==
== Object Oriented Programming ==


[http://en.wikipedia.org/wiki/Object-oriented_programming Programarea orientată obiect] are ca punct de plecare programarea procedurală, în sensul că se bazează tot pe noțiunile de stare și variabile, dar introduce o serie de concepte care ajută la structurarea programelor:
[http://en.wikipedia.org/wiki/Object-oriented_programming Object oriented programming] is somewhat derived from procedural programming, in the sense that it's based on internal state and variables, but also introduces concepts that help to structure programs and data:
* clase și obiecte;
* classes and objects
* încapsulare;
* encapsulation
* polimorfism;
* polymorphism
* moștenire.
* inheritance


Majoritatea acestor noțiuni vor fi prezentate în pagina [[noțiuni avansate de programare obiect-orientată]];
Most of these notions will be presented in the section [[Advanced Notions About Object Oriented Programming]].


=== Tipuri de date ===
The idea that stands at the base of object oriented programming, is that every system that performs a task, either real or software, is made of objects. Take for example a home theater system: it is made of a TV, a media box, some speakers, a remote control, some DVD and BluRay disks, a network interface, and all these objects interact with each other in order to perform the task. Now think about the DVD disks. There are multiple disks (objects), but all of them share the same properties and they function similarly, meaning that they are in fact contained in the same class of objects. This is exactly what a class and an object is, in object oriented programming. Let's try another approach!


==== Structura în C ====
=== Data Types ===


Plecând de la limbajul C, ne amintim că acesta pune la dispoziție o serie de tipuri de date primitive, incluse în standardul limbajului. Ca exemple, avem: ''int'', ''long'', ''char'', ''double'', etc. În plus, există posibilitatea de a crea structuri compuse folosind cuvântul cheie ''[http://en.wikipedia.org/wiki/Struct_(C_programming_language) struct]''. O structură în C este compusă din una sau mai multe variabile care pot fi ori de tip primitiv, ori alte structuri.
==== C struct ====


Starting from the C language, we recall that it provides us with a set of primitive types, included in the language standard. As an example, we have: ''int'', ''char'', ''double'', etc. Furthermore, we can create composite structures, by using the keyword ''[http://en.wikipedia.org/wiki/Struct_(C_programming_language) struct]''. A structure in C is composed of one ore more variables which may be of a primitive type, or another structure.


În continuare avem un exemplu de definiție a unor structuri în C.
Next, we have an example of C structures definitions:
<syntaxhighlight lang="C">
<syntaxhighlight lang="C">
struct string{
struct string{
Linia 50: Linia 51:
</syntaxhighlight>
</syntaxhighlight>


Se vede că structura ''person'' conține pointeri la două structuri de tip ''string''. Relația este descrisă de schema bloc următoare (cu exemple de valori pentru variabilele primitive):
We can see that structure ''person'' contains pointers to two structures of type ''string''. The relationship is described by the following diagram (with value examples for primitive types):


[[Fișier:struct_relations1.png|Schema bloc pentru relația dintre structurile string și person]]
[[Fișier:struct_relations1.png|Diagram for the relationship between string and person structures]]


În continuare, vom da un exemplu de utilizare a acestor structuri:
Next, we will give an example of using these structures:
<syntaxhighlight lang="C">
<syntaxhighlight lang="C">
#include<stdio.h>
#include<stdio.h>
Linia 76: Linia 77:
     new_person->weight = 68.9F;
     new_person->weight = 68.9F;


     printf("Persoana se numeste %s %s, are varsta de %d ani, inaltimea %f si greutatea %f\n", new_person->first_name->str,  
     printf("The person's name is %s %s, age %d years old, height %f and weight %f\n", new_person->first_name->str,  
         new_person->last_name->str,
         new_person->last_name->str,
         new_person->age,
         new_person->age,
Linia 86: Linia 87:
</syntaxhighlight>
</syntaxhighlight>


În acest exemplu, schema bloc este diferită dintr-un punct de vedere esențial: ambii pointeri de tip string sunt referință la aceeași adresă, respectiv la același string. Astfel, dacă se modifică ''new_person->first_name'', atunci implicit se modifică și ''new_person->last_name'' (de fapt este aceeași structură):
In this example, the diagram is different from an essential point of view: both pointers of type string refer the the same address, the same string respectively. Therefore, if ''new_person->first_name'' is changed, then automatically ''new_person->last_name'' is changed as well (in fact, it is the same structure).


[[Fișier:struct_relations2.png|Schema bloc pentru relația dintre structurile string și person cu același pointer pentru nume]]
[[Fișier:struct_relations2.png|Diagram for the relationship between string and person structures with the same pointer for name]]


To sum up, the structure, in C, is a '''data type''' composed of primitive variables or other structures. Similar to other data types, variables of the structure type can be defined, just like defining primitive type variables. The fundamental limitation of structures is that they can only hold '''data''' and not '''functions'''. This is how the '''class''' came to be.


Pentru a face un rezumat, structura, in C, este un '''tip de dată''' compusă din tipuri primitive, sau alte structuri. Analog cu orice alt tip de dată, se pot defini variabile de tipul structurii, așa cum se pot defini variabile de tip primitiv. Limitarea fundamentală a structurilor este că acestea nu pot conține decât '''date''', nu și '''funcții'''. Astfel se introduce noțiunea de '''clasă'''.
==== Class and Object ====


==== Clasa și obiectul ====
<div class="regula"><font color="#ff0000">Attention: </font>The '''class''' is a '''structure''' that can hold variables, called '''fields''' or '''properties''', and functions, called '''methods'''. Fields and methods are called class '''members'''.</div>


<div class="regula"><font color="#ff0000">Atenție: </font>'''Clasa''' este o '''structură''' care poate conține variabile, numite '''câmpuri''' ale clasei, și funcții, numite '''metode''' are clasei. Câmpurile și metodele sunt '''membrii''' clasei.</div>


<div class="regula"><font color="#ff0000">Attention: </font>The methods of a class change the state of the class properties.</div>


<div class="regula"><font color="#ff0000">Atenție: </font>Metodele unei clase acționează asupra câmpurilor clasei.</div>


 
<div class="regula"><font color="#ff0000">Attention: </font>The class is a data type. Variables of the class type are called '''objects'''. Objects are instances of the class. So, when a new object is created, it's called that a new object of that class is instantiated.</div>
<div class="regula"><font color="#ff0000">Atenție: </font>Clasa este un tip de dată. Datele de tipul clasei se numesc '''obiecte'''. Obiectele sunt instanțe ale clasei. Astfel, când se creează un obiect nou, se mai spune că se '''instanțiază''' un obiect de tipul clasei respective.</div>

Versiunea curentă din 21 octombrie 2013 18:52

Notions About Programming Paradigms

Since the invention of computing machines, the struggle for performance and technological advance has been fought on two fronts: on one hand by improving hardware performance by increasing clock frequency, the number of cores, the size of available physical memory and the complexity of the instruction set, and on the other hand, by finding new methods for efficiently programming the algorithms. By new methods we don't only mean new programming languages, although, if we follow their evolution over time, we will see a significant increase, in both numbers and variety. Why are new programming languages still being created, if there already is such a large number of them? There are several reasons, but the main one is that new functionality appears and it needs to be expressed in ways that we don't yet have (for example, SIMD - Single Instruction Multiple Data - accelerators are difficult to use in the C, because the language does not have support for vector data types). However, new programming languages are also triggered by something else: the invention of new programming paradigms.

The programming paradigm refers to the way an algorithm is described. Surely, by description, we don't mean the way the additions or multiplications are done, or the language itself, but a higher level. For example, how the data is structured, how this structures and the program behavior interacts, and so on. There is a relatively small number of programming paradigms, and we will name some examples, but it is important to note that it can't be stated that one is better than the other, but, just like in the case of programming languages, each is optimal for a certain class of applications.

Imperative Procedural Programming

Imperative programming describes an algorithm at an instruction level, which modifies the state of the program. Procedural programming structures these instructions into distinct sequences, called procedures or functions (not to be confused with functional programming), which work with global data structures, which are visible to all procedures, or local data structures, only visible to the procedure in which they are declared. As an example of a procedural programming language, we have the original C language (not C++). Programs written in C are built of functions (which can take arguments, compute and return data) and global data.

Procedural programming is being used extensively for low level programming (kernel, operating system, embedded, etc.), because C offers absolute control over the hardware resources and it maps very well to the way the processor executes the program. On the other hand, for high level applications, like text editors, client-server applications, simulators, synthesis software, etc., there a humongous quantity of code is written, the fact that procedural programming does not offer proper support for data structuring and encapsulation, makes it really difficult to use. In these cases, issues arise: security, memory allocation, maintenance and development, etc.

Functional Programming

Functional programming refers to the style and programming languages where computation is treated by evaluating mathematical expressions. As opposed to imperative programming, where we have state and variables, functional programming is based on a formal system called lambda calculus, which describes a program by composing mathematical functions.

One of the first functional languages is Lisp.

Object Oriented Programming

Object oriented programming is somewhat derived from procedural programming, in the sense that it's based on internal state and variables, but also introduces concepts that help to structure programs and data:

  • classes and objects
  • encapsulation
  • polymorphism
  • inheritance

Most of these notions will be presented in the section Advanced Notions About Object Oriented Programming.

The idea that stands at the base of object oriented programming, is that every system that performs a task, either real or software, is made of objects. Take for example a home theater system: it is made of a TV, a media box, some speakers, a remote control, some DVD and BluRay disks, a network interface, and all these objects interact with each other in order to perform the task. Now think about the DVD disks. There are multiple disks (objects), but all of them share the same properties and they function similarly, meaning that they are in fact contained in the same class of objects. This is exactly what a class and an object is, in object oriented programming. Let's try another approach!

Data Types

C struct

Starting from the C language, we recall that it provides us with a set of primitive types, included in the language standard. As an example, we have: int, char, double, etc. Furthermore, we can create composite structures, by using the keyword struct. A structure in C is composed of one ore more variables which may be of a primitive type, or another structure.

Next, we have an example of C structures definitions:

struct string{
    char* str;
    unsigned length;
};

struct person{
    struct string *first_name;
    struct string *last_name;
    unsigned age;
    float height;
    float weight;
};

We can see that structure person contains pointers to two structures of type string. The relationship is described by the following diagram (with value examples for primitive types):

Diagram for the relationship between string and person structures

Next, we will give an example of using these structures:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(){
    //make a default string with no content and length = 30
    struct string * some_string = (struct string*)malloc(sizeof(struct string));
    unsigned default_length = 30;
    some_string->length = default_length;
    some_string->str = (char*)malloc(default_length * sizeof(char));
    strcpy(some_string->str, "Vasile");

    //make a person structure in which all strings refer to the default empty string
    struct person* new_person = (struct person*)malloc(sizeof(struct person));
    new_person->first_name = some_string;
    new_person->last_name = some_string;
    new_person->age = 29;
    new_person->height = (float)1.7;
    new_person->weight = 68.9F;

    printf("The person's name is %s %s, age %d years old, height %f and weight %f\n", new_person->first_name->str, 
        new_person->last_name->str,
        new_person->age,
        new_person->height,
        new_person->weight);

    return 0;
}

In this example, the diagram is different from an essential point of view: both pointers of type string refer the the same address, the same string respectively. Therefore, if new_person->first_name is changed, then automatically new_person->last_name is changed as well (in fact, it is the same structure).

Diagram for the relationship between string and person structures with the same pointer for name

To sum up, the structure, in C, is a data type composed of primitive variables or other structures. Similar to other data types, variables of the structure type can be defined, just like defining primitive type variables. The fundamental limitation of structures is that they can only hold data and not functions. This is how the class came to be.

Class and Object

Attention: The class is a structure that can hold variables, called fields or properties, and functions, called methods. Fields and methods are called class members.


Attention: The methods of a class change the state of the class properties.


Attention: The class is a data type. Variables of the class type are called objects. Objects are instances of the class. So, when a new object is created, it's called that a new object of that class is instantiated.