Documente Academic
Documente Profesional
Documente Cultură
Intr-o lista circular vor fi mereu doi pointeri care duc spre acelasi nod. O solutie ar fi
parcurgerea listei si verificarea daca la fiecare nod se pointeaza de doia ori. O alta solutie a I
parcurgerea listei si sse verifica daca un nod a fos parcurs de doua ori. O solutie optima este
folosirea a doi pointeri de salturi diferite care incep cu capul listei. Daca pointerul cu salt mai
mare ajunge este null, lista e aciclica. Daca pointerul cu salt mai mare este egal cu pointerul
de salt mai mic SAU urmatorul element pointat de pointerul cu salt mai mare este egal cu
pointerul de salt mic, atunci lista e ciclica.
2. How do you decide which integer type to use?
Daca e nevoie de valori mari (peste 32.767 sau -32767) se foloseste long. Daca
dimensiunea e importanta (adica sunt arrayuri mari sau structuri) se foloseste short int
(memorare pe un byte), altfel int (4 bytes), long int(8 bytes).
3. How do you differentiate between a constructor and destructor?
Constructorul este folosit pentrua initializa un obiect. Poate avea argumente.
Supraincarcarea este posibilia, insemnand ca pot exista mai multi constructori definiti
intr-o clasa.Are acelasi nume ca si clasa. Avem: constructor implicit, constructor cu
parametric, constructor de copiere. Se mai folosesc pentru alocarea dinamica a
memoriei folosind operatorul new.
Destructorul este folosit pentru a distruge un obiect creat anterior. Nu primeste
argumente. Supraincarcarea nu este posibila. Are acelasi nume ca si clasa cu
operatorul ~ in fata. Este folosit pentu eliberarea zonei de memorie alocata.
Foloseste indirect operatorul delete pentru a distruge un obiect.
4. How do you differentiate between aggregation and association?
Asocierea - relatie in care toate obiectele au propriul lor ciclu de viata si nu depend intre
ele. Ex. Profesor Elev. Mai multi studenti pot fi asociati unui singur professor sau un singur
student poate fi asociat mai multor profesori.
public class Foo {
void Baz(Bar bar) {
}
};
Agregarea o e forma de asociere unde toate obiectele au propriul ciclu de viata, dar exista si
dependente intre ele. Catedra si professor. Un professor nu poate apartine mai multor
catedre. Stergerea obiectului catedra nu implica si distrugerea obiectului professor.
public class Foo {
private Bar bar = new Bar();
}
Compunerea este o forma de agregare mai puternica. Obiectele copil nu au popriul ciclu de
viata. Daca obiectul parinte este sters, se sterge si obiectul copil (referinta). Exemplu. Casa
cu obiectul camera. O casa poate avea mai multe camere. Daca stergem obiectul casa, se
vor sterge.
public class Foo {
private Bar bar;
Foo(Bar bar) {
this.bar = bar;
}
}
5. How do you find out the size of a class?
Sizeof membri nonstatici. Ordinea membrilor influenteaza, existenta functiilor.
6. How do you implement an itoa function?
// Implementation of itoa()
char* itoa(int num, char* str, int base)
{
int i = 0;
bool isNegative = false;
/* Handle 0 explicitely, otherwise empty string is printed for 0 */
if (num == 0)
{
str[i++] = '0';
str[i] = '\0';
return str;
}
// In standard itoa(), negative numbers are handled only with
// base 10. Otherwise numbers are considered unsigned.
if (num < 0 && base == 10)
{
isNegative = true;
num = -num;
}
// Process individual digits
while (num != 0)
{
int rem = num % base;
str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
num = num/base;
}
// If number is negative, append '-'
if (isNegative)
str[i++] = '-';
str[i] = '\0'; // Append string terminator
// Reverse the string
reverse(str, i);
}
return str;
Sintaxa de definire a pointerilor la funcii este asemntoare cu sintaxa de definire a funciilor. n forma general, o
funcie se declar astfel:
tip_returnat nume_functie(lista parametri);
Un pointer la funcie se declar dup sintaxa:
tip_returnat (*nume_variabila_pointer)(lista_parametri);
Practic numele funciei se nlocuiete cu construcia (*nume_pointer).
8. How do you link a C++ program to C functions?
auto
register
static
extern
mutable
The register storage class is used to define local variables that should be stored in a register instead of
RAM. This means that the variable has a maximum size equal to the register size (usually one word) and
can't have the unary '&' operator applied to it (as it does not have a memory location).
The static storage class instructs the compiler to keep a local variable in existence during the life-time of
the program instead of creating and destroying it each time it comes into and goes out of scope.
Therefore, making local variables static allows them to maintain their values between function calls.
The static modifier may also be applied to global variables. When this is done, it causes that variable's
scope to be restricted to the file in which it is declared.
In C++, when static is used on a class data member, it causes only one copy of that member to be shared
by all objects of its class.
The extern Storage Class
The extern storage class is used to give a reference of a global variable that is visible to ALL the program
files. When you use 'extern' the variable cannot be initialized as all it does is point the variable name at a
storage location that has been previously defined.
The mutable specifier applies only to class objects, which are discussed later in this tutorial. It allows a
member of an object to override constness. That is, a mutable member can be modified by a const
member function.
Mostenirea multipla poate cauza probleme. Se considera clasa AdaptorWireless derivata din
UsbDevice si NetworkDevice, amvele clase avand metoda GetId. Se ceaza un nou obiect de
tip AdaptorWireless. cout << c54G.GetID(); nu stie care functie sa o apeleze.
cout << c54G.USBDevice::GetID(); obiectul AdaptorWireless contine doua functii GetID
care trebuiesc separate.
Iterative:
void reverse(Node*& head) {
if (!head) return;
Node* prev = NULL;
Node* curr = head;
while (curr) {
Node* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head = prev;
}
Recursive
void reverse(Node*& p) {
if (!p) return;
Node* rest = p->next;
if (!rest) return;
reverse(rest);
p->next->next = p;
p->next = NULL;
p = rest;
}
46. Whats the best way to declare and define global variables?
Se declara in afara claselor sau in afara blocurilor. se poate declara si in alte fisiere si se
foloseste extern pentru acces.