1. 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: a. b.v(); b. p=&a; c. p->m(); d. p->v(); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 2. 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 corecta: a. b.m(); b. b.v(); c. p->f(); d. p->m(); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3. 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 corecta: a. b.m(); b. b.v(); c. p->f(); d. p->v(); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 4. Fie urmatorul program: // Public redeclarat private // Metoda PRIVATE DEVINE ACCESIBILA PRIN MECANISMUL DE LEGARE DINAMICA #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: a. b.m(); b. p=&a; c. p->m(); d. p->v();
5. Fie urmatorul program: #include class Cerc{ public: Cerc(float r):raza(r){} float getRaza(){return raza;} Cerc operator++(){raza++;return *this;}// return by value Cerc& operator--(){raza--;return *this;}// return by reference private: float raza; }; void main(){ Cerc c(1.0); cout<<(++(++c)).getRaza()<<” ”; cout< class Cerc{ public: Cerc(float r):raza(r){} float getRaza(){return raza;} Cerc operator++(){raza++;return *this;}// return by value Cerc operator--(){raza--;return *this;}// return by value private: float raza; }; void main(){ Cerc c(1.0); cout<<(++(++c)).getRaza()<<” ”; cout<
7. Fie programul: // singleton // constructor private #include #include class S{ public: static S* create() { if(i==0){ refS=new S(); i++; } return refS; } void setName(char *s){name=s;} char* getName(){return name;} static int getNr(){return nr;} private: static S* refS; static int i; static int nr; S(){nr++;} char *name; }; int S::i=0; int S::nr=0; S* S::refS=0; void main(){ S *o1, *o2; o1=S::create(); o2=S::create(); o1->setName("Matematica"); o2->setName("Informatica"); cout<getName()<<endl; cout<getName()<<endl; cout<<S::getNr()<<endl; getch(); } Programul afiseaza: a. Matematica Informatica 1 b. Informatica Informatica 1 c. Informatica Informatica 2 d. Matematica Informatica 2 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
8. Se considera clasa: #include "iostream.h" template class C{ public: C(int nrmax=0); void put(T); T get(); void remove(); private: int nrmax; int nrelem; int first; int free; T* support; }; template C::C(int nrmax):nrmax(nrmax){ first=free=nrelem=0; support = new T [nrmax-1]; } template void C::put(T e){ support[free]=e; free= ++free % nrmax; nrelem++; } template T C::get(){ return support[first]; } template void C::remove(){ first= ++first % nrmax; --nrelem; } a. Clasa C defineste o structura de tip coada (FIFO) b. Clasa C defineste o structura de tip stiva (LIFO) c. Clasa C este un vector d. Clasa C este abstracta /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 9. 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< class B{ public: ~B(){cout<<"~B()"<<endl;} }; class D: public B{ public: ~D(){cout<<"~D()"<<endl;} }; void main(){ clrscr(); B *pb; D *pd; pd= new D(); pb=new D(); delete pb; delete pd; } Programul afiseaza : a. ~ B() ~B() ~D() b. ~ B() ~D() c. ~ B()~D()~B() ~D() d. ~ B() ~D() ~B() ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
11. Fie urmatorul program: // Constructor cu semantica prin referinta // Operatorul = este cel implicit #include class C{ public: C(int n=0, int v[]); void set(int i, int val){pi[i]=val;} 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; } void main(){ int a[]={1,2,3}, b[]={10,20}; C x(3,a),y(2, b); x=y; y.set(0,-1000); cout<<x<<endl; } Programul afiseaza: a. -100 20 b. -1000 20 c. 100 2 3 d. 1 20 30 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
12. Fie programul: #include class B{ public: void st(){cout<< "static method st() of B"<<endl;} virtual void v(){cout<< "virtual method v() of B"<<endl;} }; class D: public B{ public: void st(){cout<< "static method st() of D"<<endl;} virtual void v(){cout<< " virtual method v() of D"<<endl;} }; int main(){ B *pb; D d; // pointing to a subclass pb=&d; ((D*)pb)->v(); ((D*)pb)->st(); return 0; } Programul afiseaza: a. virtual method v() of D static method st() of D b. virtual method v() of B static method st() of D c. virtual method v() of D static method st() of B d. virtual method v() of B static method st() of B ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
13. Fie programul: #include class B{ public: void st(){cout<< "static method st() of B"<<endl;} virtual void v(){cout<< "virtual method v() of B"<<endl;} }; class D: public B{ public: void st(){cout<< "static method st() of D"<<endl;} virtual void v(){cout<< " virtual method v() of D"<<endl;} }; int main(){ B b, *pb; D d, *pd; pb=&d; pb->st(); pb->v(); return 0; } Programul afiseaza: a. static method s() of D virtual method v() of D b. static method s() of B virtual method v() of B c. static method s() of D virtual method v() of B d. static method s() of B virtual method v() of D ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
14. Fie programul #include <string.h> #include class Person{ public: Person(char *p){nr=0; name=new char[strlen(p)+1]; strcpy(name,p); nr++; cout<
15. Fie programul : // constructor de copiere in clasa de baza; dar absent in clasa derivata // compilatorul creeaza un constructor de copiere implicit // care apeleaza constr de copiere al clasei de baza pentru copierea atributelor din clasa de baza // si copiaza membru cu membru atributele din D #include class B{ public: B(){cout<<"B()"<<endl;} B(B &b){cout<<"B(B &b)"<<endl;} }; class D: public B{ public: D(){cout<<"D()"<<endl;} }; void main(){ B b; // apel B() B b1(b); // apel B(B & ); nu se mai utilizeaza B()! D d; // apel B();D() D d1(d); B bd(d); // conversie implicita la clasa de baza; getch(); } Programul afiseaza : a. B() B() B(B&b) B() D() B(B &b) B(B &b) b. B() B() B(B&b) B() D() B(B &b) D() B(B &b) c. B() B(B&b) B() D() B(B &b) B(B &b) d. B() B(B&b) D() B(B &b) D() B(B &b) /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 16. 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(); } a. La iesirea din functia f(), spatiul alocat pentru sirul de caractere “Balanescu” nu este eliberat si memoria va fi epuizata b. La iesirea din functia f(), spatiul alocat pentru sirul de caractere “Balanescu” este eliberat c. Programul se termina datorita epuizarii memoriei d. Programul se termina dupa primul apel al functiei f()
17. Fie urmatorul program: #include class A{ public: void s(){cout<<"void A::s()"<<endl;} void s(int i){i++;cout<<"void A::s(int)"<<endl; } virtual void v(){cout<<"virtual void A::v()"<<endl;} virtual void v(int i){i++;cout<<"virtual void A::v(int)"<<endl;} }; Care afirmatie este corecta: a. Definitia virtual void v()supraincarca definitia virtual void v(int i) b. Definitia virtual void v(int i) este incorecta deoarece exista o definitie pentru metoda v. c. void v() este metoda ce va fi legata static. d. in expresia p->v(); metoda v este legata static /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 18. Fie urmatorul program: // Constructor cu semantica prin referinta // Operatorul = este cel implicit t#include class C{ public: C(int n=0, int v[]);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;} void main(){ int a[]={1,2,3}, b[]={10,20}; C x(3,a),y(2, b); a[0]=-100; cout<<(x=y)<<endl; } Programul afiseaza: AFIZEAZA 10, 20, NU ESTE NICI UN X /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 19. Fie programul : #include class Cerc{ public: Cerc(float r):raza(r){} float getRaza(){return raza;} void operator++(){raza++;} private: float raza; }; class Cilindru: public Cerc{ public: Cilindru(float raza, float inaltime):Cerc(raza), inaltime(inaltime){}; void operator++(){inaltime++;} float getInaltime(){return inaltime;} private: float inaltime; }; void main(){ Cerc *pc; Cilindru c(1,5); pc=&c; ++ *pc; cout<getRaza()<<" "<
20. Fie programul: #include <string.h> #include class Person{ public: Person(char *p){name=new char[strlen(p)+1]; strcpy(name,p); nr++; cout< #include <string.h> #include class Person{ public: Person(char *p){name=new char[strlen(p)+1]; strcpy(name,p); nr++; cout<
22. Fie următorul program C++: #include class Punct{ public: Punct(int=0, int=0); //constructor protected: int x,y; friend ostream& operator<<(ostream&, const Punct&); }; class Cerc: public Punct{ public: Cerc(double r=0.0, int x=0, int y=0);// constructor protected: float raza; friend ostream& operator<<(ostream&, const Cerc&); }; //implementare Punct Punct::Punct(int a, int b):x(a),y(b){} // constructor ostream& operator<<(ostream& output, const Punct& p){ output<<"Punct"<<'['<