Documente Academic
Documente Profesional
Documente Cultură
n aceast seciune sunt prezentate soluii pentru unele din exerciiile propuse
n text. n general, s-au selectat acele exerciii care ofer o modalitate de abordare a
unei anumite categorii de probleme i cele care ofer rspunsuri n situaii mai
deosebite de execuie. Celelalte, care extind doar materialul deja soluionat, rmn ca
exerciiu de programare pentru cititori.
Deoarece valoarea *p++ = *q++ este *q, se poate scrie nc mai concis:
while(*p++ = *q++);
E1.8 n aceast funcie trebuie s fie testat pointerul returnat de operatorul new:
void f(long n){
int* p = new int[n];
if (!p)
cout << "Eroare alocare\n";
else
cout << "Alocare corecta\n";
}
2 Clase i obiecte
E2.4 Mesajele sunt:
Nr. obiecte 1
Nr. obiecte 2
Nr. obiecte 3
Nr. obiecte 4
Nr. obiecte 5
Nr. obiecte 4
Nr. obiecte 3
Nr. obiecte 4
Nr. obiecte 5
Nr. obiecte 6
Nr. obiecte 5
Nr. obiecte 4
Nr. obiecte 3
Nr. obiecte 2
Nr. obiecte 1
Nr. obiecte 0
Primele trei obiecte (ob1, ob2, ob3) sunt create la nceputul funciei
main(). La intrarea n blocul nou creat se mai creeaz 2 obiecte (ob4 i ob5) (deci
numrul de obiecte ajunge la 5), care sunt imediat distruse la ieirea din bloc. Dup
aceasta se creeaz un vector de 3 obiecte, care sunt distruse prin apelul operatorului
delete. La ieirea din funcia main() se distrug i cele 3 obiecte rmase, ob1, ob2,
ob3.
class Item{
static int numbers; // (a)
public:
Item(){ // constructorul creste cu 1 nr obiectelor
numbers++;
cout << "Nr. obiecte " << numbers << endl;
}
~Item(){ //destructorul descreste cu 1 nr obiectelor
numbers--;
cout << "Nr. obiecte " << numbers << endl;
}
};
int Item::numbers = 0; // (b)
void main(){
Item ob1, ob2, ob3;
{ // se deschide un nou bloc
Item ob4, ob5;
}
Item *pob6 = new Item[3];
delete [] pob6;
}
int *p1 = p;
p = new int[count + grows];
for (int j=0; j < i;j++)
p[j] = p1[j];
p[i] = x;
for (j=i; j < count;j++)
p[j+1] = p1[j];
delete p1;
count++;
}
}
return count - 1;
}
}
int IntArray::RemoveAt(int i) {
if (i < 0 || i >= count){
cout << Eroare de stergere element\n;
return 0;
}
else {
for(int j=i; j < count-1;j++)
p[j] = p[j+1];
count--;
return 1;
}
}
E2.11 Este necesar ca unele funcii membre ale clasei s acceseze obiecte declarate
const. Deci sunt necesare urmtoarele modificri:
int GetCount() const {return count;}
int GetAt(int i) const;
void IntDList::Display(){
IntDListNode* p = first;
while (p){
cout << p->v <<" ";
p = p->next;
}
cout << endl;
}
void IntDList::ReverseDisplay(){
IntDListNode* p = last;
while (p){
cout << p->v<<" ";
p = p->prev;
}
cout << endl;
}
count--;
if (count == 0){
last = 0;
}
return v;
}
}
int IntDList::RemoveTail(){
if (count == 0){
cout << Eroare lista vida\n;
return -1;
}
else {
int v = last->v;
IntDListNode* p = last;
last = last->prev;
if (last)
last->next = 0;
delete p;
count--;
if (count == 0){
first = 0;
}
return v;
}
}
IntTree::~IntTree(){
delTree1(root);
}
void PointTree::preorder(){
preorder1(root);
}
void PointTree::postorder1(PointNode *root){
if(!root) return;
postorder1(root->left);
postorder1(root->right);
if(root->d) g(root->d);
}
void PointTree::postorder(){
postorder1(root);
}
void PointTree::delTree1(PointNode *root){
if(!root) return;
delTree1(root->left);
delTree1(root->right);
delete root;
}
PointTree::~PointTree(){
delTree1(root);
}
NTree::~NTree(){
RemoveAll();
if (label){
delete label;
label = NULL;
}
}
4 Suprancrcarea operatorilor
E4.1 Clasa Date se definete astfel:
class Date{
int day;
int month;
int year;
public:
Date(int d=5, int m=7,int y=1999){
year = y;
month = m;
day = d;
}
Date operator++();
friend ostream& operator <<(ostream& stream, Date& d);
int& operator[](int i);
};
ostream& operator <<(ostream& stream, Date& d){
stream << " Ziua: " << d.day << " Luna: " << d.month
<< " Anul: " << d.year << endl;
return stream;
}
day = 1;
return *this;
}
if (day==31 && month == 12){
year++;
month = 1;
day = 1;
return *this;
}
if (day < 30){
day++;
return *this;
}
if (day==31 && (month == 1 || month == 3 ||
month == 5 || month == 7 ||
month == 8 || month == 10)){
day = 1;
month++;
return *this;
}
if (day==30 && (month == 4 || month == 6 ||
month == 9 || month == 11 )){
day = 1;
month++;
return *this;
}
return *this;
}
CPoint2d(double *p):CPoint(2,p){}
~CPoint2d(){
cout << "Destructor 2d" << endl;
}
void Display (){
cout << "Display 2d ";
cout << *this;
}
};
class CPoint3d:public CPoint{
public:
CPoint3d():CPoint(3){}
CPoint3d(double x, double y, double z):CPoint(3){
cout << "Constructor 3d" << endl;
(*this)[0] = x;
(*this)[1] = y;
(*this)[2] = z;
}
CPoint3d(double *p):CPoint(3,p){}
~CPoint3d(){
cout << "Destructor 3d" << endl;
}
void Display(){
cout << "Display 3d ";
cout << *this;
}
};
DListNode* DList::RemoveTail(){
if (count == 0)return 0;
else {
DListNode* v = last;
last = last->prev;
if (last)last->next = 0;
count--;
if (count == 0)first = 0;
return v;
}
}
if (p){
int v = p->GetV();
delete p;
return v;
}
else return 0;
}
ostream& operator <<(ostream& stream, IDList& list){
IDListNode* p = list.GetHead();
while (p){
stream << p->GetV()<<" ";
234 Elemente de Programare Orientat pe Obiecte
p = (IDListNode*)p->GetNext();
}
stream << endl;
return stream;
}
Dup definirea acestor funcii operator, elementele listei pot fi accesate prin
indexare att pentru citire ct i pentru modificare, deoarece funcia operator []
Soluii 235
Constructor copiere
Destructor
Destructor
Destructor
n acest caz, ultimul cmp afiat n fiecare linie este valoarea pointerului
node convertit n ntreg i afiat zecimal.
E7.7 Pentru a se folosi acelai iterator iter pentru o nou afiare a listei nlnuite
este necesar repoziionarea iteratorului pe primul element al listei. Pentru aceasta n
clasa MapIter se adaug funcia:
void first(){
node = map->head;
}
p[i] = p1[i];
delete p1;
p[count++] = x;
}
return count-1;
}
template <class E>ostream& operator << (ostream& stream,
TArray<E>& r){
int count = r.count;
for (int i =0;i<count;i++)
stream << r.p[i] <<" ";
stream << endl;
return stream;
}
template <class E> TArray<E>::TArray<E>(E* v, int n){
count = n;
p = new E[count];
for (int i = 0; i < count; i++)
p[i] = v[i];
}
template <class E> int TArray<E>::RemoveAt(int i){
if (i < 0 || i >=count)
return 0;
else {
for(int j=i; j < count-1;j++)
p[j]=p[j+1];
count--;
return 1;
}
}
TArray<Complex> carray;
carray.Add(Complex(3.5,8));
carray.Add(Complex(6,9.2));
cout << carray; // afiseaza (3.5,8) (6,9.2)
}
E7.20 O stiva de numere complexe se poate implementa prin clasa template Tlist
astfel:
void ft3(){
TList<Complex> list4;
Soluii 241
8 Tratarea excepiilor
E8.3 Se completeaz clasa IntArray astfel:
class IntArray {
//.
public:
class Range{
int index;
public:
Range(int i){index = i;}
int GetIndex(){return index;}
};
int GetAt(int i) const;
int& operator[](int i);
};
int GetAt(int i) const{
if (i >= 0 && i < size)
return p[i];
else{
throw Range(i);
return -1;
}
}
int& operator[](int i){
if (i >= 0 && i < size)
return p[i];
else{
throw Range(i);
return p[0];
}
}
}
}
Se observ faptul c dup tratarea erorii programul este continuat dup rutina
de tratare a excepiei i nu cu instruciunile care urmeaz instruciunii care a provocat
excepia.
E8.4 Se definete clasa global pentru excepia de extragere a unei date dintr-o list
goal astfel:
class Empty{
public:
Empty(){};
};
Captarea i tratarea unei excepii de list vid se poate face n felul urmtor:
Soluii 243
void fdlist3(){
try{
IntDList list2;
list2.AddHead(1); // push 1
list2.AddHead(2); // push 2
list2.AddHead(3); // push 3
cout << list2.RemoveHead(); // pop, afiseaza 3
cout << list2.RemoveHead(); // pop, afiseaza 2
cout << list2.RemoveHead(); // pop, afiseaza 1
cout << list2.RemoveHead(); // lanseaza exceptia
cout << list2.RemoveHead();
}
catch(Empty){
cout << "\nExceptie lista goala\n";
}
cout << Dupa rutina de tratare a excepiei\n;
}
321
Exceptie lista goala
Dupa rutina de tratare a exceptiei