1. Fie programul: //destructor explicit #include <string.h> #include class Person{ public: Person(char *p){name=new char[strlen(p)+1]; strcpy(name,p);} ~Person(){delete[] name;} private: char *name; }; void f(){Person *p = new Person("Balanescu"); delete p; // fara aceasta instructiune, obiectele se acumuleaza // in heap si blocheaza executarea, spre deosebire de Java } void main(){ while(1)f(); } La iesirea din functia f(), spatiul alocat pentru sirul de caractere “Balanescu” este eliberat 2. Fie urmatorul program, in care se utilizeaza clase abstracte: // abselem.h #ifndef ABSTRACTELEM_H #define ABSTRACTELEM_H class AbstractElem{ public: virtual void display()=0; virtual void process()=0; }; #endif //LIST.h #ifndef LIST_H #define LIST_H #include "abselem.h" class LIST{ public: LIST(int nrmax=0); void put(AbstractElem *); AbstractElem* get(); void remove(); private: int nrmax; int nrelem; int first; int free;
AbstractElem* *support; }; #endif //elements.h #include "abselem.h" #include "iostream.h" class Person: public AbstractElem{ public: Person(char *name); virtual void display(); virtual void process(); private: char *name; }; class Car: public AbstractElem{ public: Car(char *name); virtual void display(); virtual void process(); private: char *name; }; //driver.cpp #include "elements.h" #include "LIST.h" LIST x(3); void main(){ x.put(new Person("Tudor")); x.put(new Person("Andrei")); x.put(new Car("B-39-TDR")); x.get()->process(); x.remove(); x.get()->process(); x.remove(); x.get()->process(); x.remove(); } //LIST.cpp #include "LIST.h" LIST::LIST(int nrmax):nrmax(nrmax){ first=free=nrelem=0; support = new AbstractElem* [nrmax-1]; } void LIST::put(AbstractElem * pe){ support[free]=pe; free= ++free % nrmax; nrelem++; } AbstractElem* LIST::get(){ return support[first]; }
void LIST::remove(){ first= ++first % nrmax; --nrelem; } //elements.cpp #include "elements.h" Person::Person(char *name):name(name){}; void Person::display(){cout<
Programul afiseaza datele in ordinea: Tudor Andrei B-39-TDR 3. Fie urmatorul program, in care se utilizeaza clase abstracte: // abselem.h #ifndef ABSTRACTELEM_H #define ABSTRACTELEM_H class AbstractElem{ public: virtual void display()=0; virtual void process()=0; }; #endif //LIST.h #ifndef LIST_H #define LIST_H #include "abselem.h" class LIST{ public: LIST(int nrmax=0); void put(AbstractElem *); AbstractElem* get(); void remove(); private: int nrmax; int nrelem;
int first; int free; AbstractElem* *support; }; #endif //LIST.cpp #include "LIST.h" LIST::LIST(int nrmax):nrmax(nrmax){ first=free=nrelem=0; support = new AbstractElem* [nrmax-1]; } void LIST::put(AbstractElem * pe){ support[free]=pe; free= ++free % nrmax; nrelem++; } AbstractElem* LIST::get(){ return support[first]; } void LIST::remove(){ first= ++first % nrmax; --nrelem; } Care afirmatie este adevarata: LIST este o structura de date de tip coada 4. Fie urmatorul program: // Public redeclarat private // #include class A{ public: void m(){cout<<"A:m()"<<endl;}; virtual void v(){cout<<"A:v()"<<endl;}; }; class B: public A{ private: void m(){cout<<"B:m()"<<endl;}; virtual void v(){cout<<"B:v()"<<endl;}; }; void main(){ A a,*p; B b; b.m(); b.v(); // obiect de tip A p=&a; p->m(); p->v(); // obiect de tip B p=&b; p->m(); p->v(); } Care expresie este incorecta: b.v();
5. Fie programul : // Constructor cu semantica prin referinta // Operatorul = cu semantica prin valoare si stergere destinatie #include class C{ public: C(int n=0, int v[]); C& C::operator=(C& x); friend
ostream& operator<<(ostream &o, const C&);
private: int dim; int *pi; }; C::C(int n, int v[]) dim=n; pi= v;
{
} ostream& operator<<(ostream &o, const C &m){ for(int j=0; j<m.dim; j++)o<< m.pi[j]<<" "; return o; } C& C::operator=(C& x){ // stergerea valorior din vectorul destinatie, care oricum vor fi modificate delete[] pi; // atribuire dim=x.dim; pi=new int[dim]; for(int j=0; j
RASPUNS prima optiune 6. Fie urmatorul program: #include class A{ public: // supraincaracre void s() void s(){cout<<"void A::s()"<<endl;} void s(int i){i++;cout<<"void A::s(int)"<<endl; } // supraincarcare void v() virtual void v(){cout<<"virtual void A::v()"<<endl;} virtual void v(int i){i++;cout<<"virtual void A::v(int)"<<endl;} }; class B:public A{ public: // 1. supraincarcare metoda statica int s(){cout<<"int B::s()"<<endl; return 1;} // 2. specializare metoda statica void s(int i){i++;cout<<"void B::s(int)"<<endl; } // 3. specializare metoda virtuala virtual void v(int i){i++;cout<<"virtual void B::v(int)"<<endl;} }; void main(){ A *pa; B b; pa=&b; pa->s(); pa->v(1); } Prin executarea sa, programul afiseaza: void A::s() void B::v(int) 7. Fie programul urmator: // Constructor cu semantica prin referinta // Operatorul = cu semantica prin valoare si stergere destinatie #include class C{ public: C(int n=0, int v[]); void set(int i, int val){pi[i]=val;} C& C::operator=(C& x); friend
ostream& operator<<(ostream &o, const C&);
private: int dim; int *pi;
}; C::C(int n, int v[]) dim=n; pi= v;
{
} ostream& operator<<(ostream &o, const C &m){ for(int j=0; j<m.dim; j++)o<< m.pi[j]<<" "; return o; } C& C::operator=(C& x){ // stergerea valorior din vectorul destinatie, care oricum vor fi modificate delete[] pi; // atribuire dim=x.dim; pi=new int[dim]; for(int j=0; j
import java.awt.event.*; class Model{ private int x=0; public Model(){}; public void increment(){x++;} public int get_x(){return x;} } public class View extends Frame{ private Button b; protected Model m; private Controller c; protected TextField tf; public static void main(String args[]){ Frame f= new View(); } public View(){ setTitle("Exemplu Model-View-Controller"); b= new Button("Actiune"); add("North",b); m=new Model(); c=new Controller(this); b.addActionListener(c); tf=new TextField(10); add("Center",tf);
}
setSize(100,250); setVisible(true);
} class Controller implements ActionListener{ private View vw; public Controller(View v){ vw=v; } public void actionPerformed(ActionEvent e){ vw.m.increment(); vw.tf.setText(String.valueOf(vw.m.get_x())); } } Care afirmaþie este adevãratã, dacã se face de douã ori click pe butonul stang mouse asupra butonului Actiune În câmpul de text f.tf este afiºatã valoarea 2 10. Fie urmãtorul bloc de instrucþiuni Java: { int x = 10; x=1; { int x = 100; } x=2; } Care afirmaþie este adevãratã: instrucþiunea int x = 100; este incorectã