asd Flashcards

1
Q
int returnByValue()
    {
        return 5;
    }
    int& returnByReference()
    {
         static int x{ 5 }; 
         return x;
    }
    int main()
    {
        int value{ returnByReference() }; 
        int &ref{ returnByValue() }; 
        const int &cref{ returnByValue() }; 
    return 0;
}
A

-static ensures x isn’t destroyed when the function ends

case A – ok, treated as return by value
case B – compile error since the value is an r-value, and an r-value can’t bind to a non-const reference
case C – ok, the lifetime of the return value is extended to the lifetime of cref

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

const int& returnByReference()
{
return 5;
}

int main()
{
    const int &ref { returnByReference() }; 
}
A

runtime error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

const int returnByValue()
{
return 5;
}

int main()
{
    const int &ref { returnByValue() }; 
}
A

ok, we’re extending the lifetime of the copy passed back to main

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
string get_name()
    {
        string s="magic";
        return s;
    }
    int main()
    {
        string p=get_name();
        std::cout<<p></p>
A

ok

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
#include 
using namespace std;
string get_name()
{
    string s="magic";
    return s;
}
int main()
{
    string&amp; p=get_name();
    std::cout<<p></p>
A
//nu compileaza
												//nu ai voie sa faci bind unei referinte de o valoare temporara
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
#include 
using namespace std;
const string&amp; get_name()
{
    string s="magic";
    return s;
}
int main()
{
    string&amp; p=get_name();
    std::cout<<p></p>
A

nu compileaza, s-a scapat ilegal de const

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
#include 
using namespace std;
string get_name()
{
    string s="magic";
    return s;
}
int main()
{
    const string&amp; p=get_name();
    std::cout<<p></p>
A

/functioneaza, iar valoarea temporara va fi distrusa atunci cand va iesi referinta din scop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
#include 
using namespace std;
const string&amp; get_name()
{
    string s="magic";
    return s;
}
int main()
{
    const string&amp; p=get_name();
    std::cout
A

runtime error, se mosteneste durata de viata a valorii trimise prin const reference si era o variabila locala deci a murit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
#include 
using namespace std;
string o= "opapa";
const string&amp; get_name()
{
    string s="magic";
    return o;
}
int main()
{
    const string&amp; p=get_name();
    std::cout<
A

opapa

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Ce e aia lifetime extension? cand functioneaza si cand nu functioneaza?

A

Functioneaza atunci cand un local const T& sau T&& este initializat din reziltatul unei expresii care returneaza:
-un temporar T
-T subobiectul unui temporar(un struct care contine T)
In cazul acesta, obiectul T este distrus atucni cand referinta iese din scop.

Cand nu merge lifetime extension?
-cand facem assignment la T& nu const T& - eroare de compilare

-cand se face non-polimorfic type conversion. type-conversion e permisa atunci cand faci assignment la T& dintr-un temporar U , unde U este copil al lui T

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
Compileaza sau nu? Daca da de ce, daca nu de ce?
#include 
using namespace std;
struct Person {
  struct Name {
    std::string first_name;
    std::string last_name;
  } name;
};
Person birth;
int main()
{
  const std::string &amp;first_name = birth.name.first_name;
}
A

Merge, si birth este distrus dupa ultima acolada.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
Spuneţi de câte ori se execută fiecare constructor în programul de mai jos şi în ce ordine.
#include 
class cls1
{
protected:
    int x;
public:
    cls1()
    {
        x=13;
    }
};
class cls2: public cls1
{
    int y;
public:
    cls2()
    {
        y=15;
    }
    int f(cls2 ob)
    {
        return (ob.x+ob.y);
    }
};
int main()
{
    cls2 ob;
    cout<
A

eroare: - nu exista
- lipseste namespace std si avem cout
- daca nu ar lipsi s-ar afisa 28
- se apeleaza cls1(), cls2(), cls1(ob), cls2(ob)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
Spuneţi dacă programul de mai jos este corect. În caz afirmativ, spuneţi ce afişează, altfel, spuneţi de ce nu este corect.
#include 
class cls1
{
    int x;
public:
    cls1()
    {
        x=13;
    }
    int g()
    {
        static int i;
        i++;
        return (i+x);
    }
};
class cls2
{
    int x;
public:
    cls2()
    {
        x=27;
    }
    cls1&amp; f()
    {
        static cls1 ob;
        return ob;
    }
};
int main()
{
    cls2 ob;
    std::cout<
A
  • nu exista
  • lipseste namespace std si avem cout

se afiseaza 14

static in funtie face sa nu se distruga obiectul la iesirea din functie

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
#include 
class cls1
{
protected:
    int x;
public:
    cls1(int i=10)
    {
        x=i;
    }
    int get_x()
    {
        return x;
    }
};
class cls2: cls1
{
public:
    cls2(int i):cls1(i) {}
};
int main()
{
    cls d(37);
    std::cout<
A
  • nu exista
  • lipseste namespace std si avem cout

-nu exista clasa cls

  • daca acolo ar fi cls1 s-ar afisa 37
  • daca acolo ar fi cls2 ar fi eraore(mostenirea e automat privata)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
Ruleaza sau nu? Daca da, de ce, daca nu, de ce?
#include 
class Money
{
public:
    Money() : amount{ 0.0 } {};
    Money(double _amount) : amount{ _amount } {};
explicit operator double() const { return amount; } private:
double amount; };
void display_balance(const Money balance)
{
    std::cout << "The balance is: " << balance << std::endl;
}
A

Nu ruleaza pentru ca explicit la conversion opeartor previne conversia implicita catre tipul catre care se converteste.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
Ruleaza sau nu? Daca da, de ce, daca nu, de ce?
#include 
class Money
{
public:
    Money() : amount{ 0.0 } {};
    Money(double _amount) : amount{ _amount } {};
double operator double() const { return amount; } private:
double amount; };
void display_balance(const Money balance)
{
    std::cout << "The balance is: " << balance << std::endl;
}
A

Nu ruleaza pentru ca e ilegal sa pui return type la conversion function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
Ruleaza sau nu? Daca da, de ce, daca nu, de ce?
#include 
class Money
{
public:
    Money() : amount{ 0.0 } {};
    Money(double _amount) : amount{ _amount } {};
    operator double(double x) const { return amount+x; }
private:
    double amount;
};
void display_balance(const Money balance)
{
    std::cout << "The balance is: " << balance << std::endl;
}
A

Nu ruleaza pentru ca e ilegal sa pui parametri ca conversion operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
#include 
class cls
{
    int x;
public:
    cls(int y)
    {
        x=y;
    }
    int operator*(cls a,cls b)
    {
        return (a.x*b.x);
    }
};
int main()
{
    cls m(100),n(15);
    cout<
A

Eroare: operatorul * poate primi 0 sau 1 argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Spuneţi de câte ori este moştenită clasa B în clasa M1. Dar în clasa M2 ? Justificaţi.

class B { /* instructiuni */ };
class D1: virtual B { /* instructiuni */ };
class D2: virtual B { /* instructiuni */ };
class D3: B { /* instructiuni */ };
class D4: private B { /* instructiuni */ };
class D5: virtual public B { /* instructiuni */ };
class M1: D1, public D2, D3, private D4, virtual D5
{ /* instructiuni */ };
class M2: D1, D2, virtual D3, virtual D4, virtual D5
{ /* instructiuni */ };
A

Pentru M1: de 3 ori :B() D5() D1() D2() B() D3() B() D4() M1()

Pentru M2: de 3 ori B() B() D3() B() D4() D5() D1() D2() M2()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
#include 
using namespace std;
class B1
{
public:
    int x;
};
class B2
{
    int y;
};
class B3
{
public:
    int z;
};
class B4
{
public:
    int t;
};
class D: private B1, protected B2, public B3, B4
{
    int u;
};
int main()
{
    D d;
    cout<
A

doar pentru z e corect

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
#include 
class cls
{
    int vi;
public:
    cls(int v=37)
    {
        vi=v;
    }
    friend int&amp; f(cls);
};
int&amp; f(cls c)
{
    return c.vi;
}
int main()
{
    const cls d(15);
    f(d)=8;
    std::cout<
A

compileaza, dar nu se afiseaza nimic pentru ca returnez adresa unui int

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
#include 
class cls1
{
public:
    int x;
    cls1(int i=20)
    {
        x=i;
    }
};
class cls2
{
public:
    int y;
    cls2(int i=30)
    {
        y=i;
    }
    operator cls1()
    {
        cls1 ob;
        ob.x=y;
        return ob;
    }
};
cls1 f(cls1 ob)
{
    ob.x++;
    return ob;
}
int main()
{
    cls1 ob1;
    f(ob1);
    std::cout<
A

2030

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
#include 
class cls
{
    int x;
public:
    cls(int i=25)
    {
        x=i;
    }
    int f();
};
int cls::f()
{
    return x;
}
int main()
{
    const cls d(15);
    std::cout<
A

passing ‘const cls’ as ‘this’ argument discards qualifiers – ar trebui ca functia sa fie const si atunci s-ar afisa 15

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q
#include
class cls
{
public:
    int x;
    cls(int i=0)
    {
        x=i;
    }
};
cls f(cls c)
{
    c.x++;
    return c;
}
int main()
{
    cls r(10);
    cls s=f(r);
    return 0;
}
A

compileaza

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q
#include 
template 
tip dif(tip x, tip y)
{
    return x-y;
}
unsigned dif(unsigned x, unsigned y)
{
    return x>=y?x-y:y-x;
}
int main()
{
    unsigned i=7,j=8;
    std::cout<
A

afiseaza 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q
#include
class cls
{
    int x;
public:
    cls()
    {
        x=23;
    }
    int get_x()
    {
        return x;
    }
};
int main()
{
    cls *p1, *p2;
    p1=new cls;
    p2=(cls*)malloc(sizeof(cls));
    int x=p1->get_x()+p2->get_x();
    std::cout<
A

compileaza si afiseaza 1330860892

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q
#include
class cls1
{
public:
    int x;
    cls1(int i=13)
    {
        x=i;
    }
};
class cls2: virtual public cls1
{
public:
    cls2(int i=15)
    {
        x=i;
    }
};
class cls3: virtual public cls1
{
public:
    cls3(int i=17)
    {
        x=i;
    }
};
class cls4: public cls1
{
public:
    cls4(int i=19)
    {
        x=i;
    }
};
class cls5: public cls2, public cls3, public cls4
{
public:
    int y;
    cls5(int i,int j):cls4(i),cls2(j)
    {
        y=i+j;
    }
    cls5(cls5&amp; ob)
    {
        y=-ob.y;
    }
};
int main()
{
    cls5 ob1(-9,3), ob2=ob1;
    std::cout<
A

6

concluzie: nu trebuie neaparat sa apelezi constructorii bazelor ca sa compileze

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q
#include
class cls
{
    int x;
    const int y;
public:
    cls(int i, int j):x(i), y(j) { }
    int imp(int, int) const;
};
int imp(int i, int j) const
{
    x=i;
    y=j;
    return x+y;
}
int main()
{
    cls ob(5);
    std::cout<
A

nu compileaza

  • nu exista constructor cu un singur parametru
  • imp nu are specificatorul cls::
  • imp este functe const si modifica campurile
  • y este const
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q
#include 
class B
{
    int a;
public:
    B(int i=0)
    {
        a=i;
    }
    int get_a()
    {
        return a;
    }
};
class D: private B
{
public:
    D(int x=0): B(x) {}
    int get_a()
    {
        return B::get_a();
    }
};
int main()
{
    D d(-89);
    std::cout<
A

-89

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q
#include
class B
{
protected:
    int x;
    B(int i=10)
    {
        x=i;
    }
public:
    virtual B operator+(B ob)
    {
        B y(x+ob.x);
        return y;
    }
};
class D: public B
{
public:
    D(int i=10)
    {
        x=i;
    }
    void operator=(B p)
    {
        x=p.x;
    }
    B operator+(B ob)
    {
        B y(x+ob.x+1);
        return y;
    }
    void afisare()
    {
        std::cout<afisare();
    return 0;
}
A

nu compileaza
x e protected

  • daca il fac pe x public afiseaza -26
  • pot sa fac si un getter doar ca dupa tot o sa fie constructorul lui B protected deci trebuie sa il fac pe el public
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q
#include
class cls
{
public:
    int sa;
    cls(int s=0)
    {
        sa=s;
    }
    operator int()
    {
        return sa;
    }
    int f(int c)
    {
        return (sa*(1+c/100));
    }
};
int main()
{
    cls p(37);
    std::cout<
A

37

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q
#include
class B
{
public:
    int x;
    B(int i=0)
    {
        x=i;
    }
    virtual B aduna(B ob)
    {
        return(x+ob.x);
    }
    B minus()
    {
        return(1-x);
    }
    void afisare()
    {
        std::cout<aduna(*p1);
    *p1=p2->minus();
    p1->afisare();
    return 0;
}
A

-100

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q
#include 
template 
class cls
{
    tip z;
public:
    cls(tip i)
    {
        z=i;
    }
    tip operator-(cls);
};
template 
tip cls::operator-(cls a)
{
    return z-a.z;
}
template 
tip dif(tip x, tip y)
{
    return x-y;
}
int dif(int x, int y)
{
    return x>=y?x-y:y-x;
}
int main()
{
    cls i=3;
    cls j=4;
    std::cout<
A

nu compileaza
-nu e nicio functie dif cu 2 tipuri diferite

-int dif(int x, int y)
{
return x>=y?x-y:y-x;
} nu o sa funtioneze pentru ca eu apelez dif(cls, cls)

ce schimb ca sa functioneze?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q
#include
class cls
{
    static int x;
public:
    cls(int i=25)
    {
        x=i;
    }
    friend int&amp; f(cls);
};
int cls::x=-13;
int&amp; f(cls c)
{
    return c.x;
}
int main()
{
    cls d(15);
    std::cout<
A

15

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q
#include
class cls
{
    int v,nr;
public:
    cls(int i)
    {
        nr=i;
        v=new int[i];
    }
    friend int&amp; operator[](int);
    friend std::ostream&amp; operator<
A

nu compileaza

  • operator[] must be a nonstatic member funtion => nu poate fi friend
  • invalid conversion from int* to int =>trebuie ca v sa fie int*
-definitia operatorului din afara clasei nu are specificatori si nu are acceasi signatura cu cea dinauntrul clasei => varianta corecta ar fi :
int&amp; cls::operator[](int i)
{
    return v[i];
}

acum programul rulezaza si afiseaza
13046584 13041856 0 0 83886085 7 13046584 13041856 0 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q
#include
class cls
{
    static int i;
    int j;
public:
    cls(int x=7)
    {
        j=x;
    }
    static int imp(int k)
    {
        cls a;
        return i+k+a.j;
    }
};
int cls::i;
int main()
{
    int k=5;
    std::cout<
A

compileaza, afiseaza 12

concluzie: static int si daca nu e initializat se initializeaza automat cu 0

37
Q
#include
class cls
{
    int x;
public:
    cls(int i=32)
    {
        x=i;
    }
    int f() const;
};
int cls::f() const
{
    return x++;
}
void main()
{
    const cls d(-15);
    std::cout<
A

nu compileaza

  • se incearca modificarea campurilor dintr-o functie const
  • main trebuie sa returneze int
daca sterg modific f: 
int cls::f() const
{
    return x;
}
si schimb void in int la main afiseaza -15
38
Q
#include 
float f(float f)
{
    if (f)
        throw f;
    return f/2;
}
int main()
{
    int x;
    try
    {
        std::cout<>x;
        if (x)
            f(x);
        else
            throw x;
        std::cout<
A

-daca numarul citit de la tastatura este 0 o sa se afiseze “Numarul 0 nu e bun!”
-daca numarul citit este diferit de 0 o sa se arunce float care nu e prins si o sa se afiseze
terminate called after throwing an instance of ‘float’

39
Q
in ce ordine sunt aplelati constructorii:
#include 
class cls1
{
protected:
    int x;
public:
    cls1()
    {
        std::cout
A

cls1()cls2()cls3()cls1(ob)cls2(ob)cls3(ob)

40
Q
#include
class cls
{
    int x;
public:
    cls()
    {
        x=23;
    }
    int get_x()
    {
        return x;
    }
};
int main()
{
    cls *p1, *p2;
    p1=new cls;
    p2=(cls*)malloc(sizeof(cls));
    int x=p1->get_x()+p2->get_x();
    std::cout<
A

compileaza si afiseaza 23 + valoare nedeterminata
de ce? pentru ca new apeleaza si constructorul de la cls, in timp ce cast-ul la cls* de la malloc nu apeleaza constructorul

41
Q
#include 
class B
{
    int a;
    B(int i=0)
    {
        a=i;
    }
    int get_a()
    {
        return a;
    }
};
class D: protected B
{
public:
    D(int x=0): B(x) {}
    int get_a()
    {
        return B::get_a();
    }
};
int main()
{
    D d(-89);
    std::cout<
A

eroare: constructorul lui B e privat

- daca fac constructorul si getterul publice o sa afiseze -89

42
Q
#include
class cls1
{
public:
    int a;
    cls1()
    {
        a=7;
    }
};
class cls2
{
public:
    int b;
    cls2(int i)
    {
        b=i;
    }
    cls2(cls1&amp; x)
    {
        b=x.a;
    }
};
int main()
{
    cls1 x;
    std::cout<
A

77

43
Q
#include
class cls
{
public:
    int sa;
    cls(int s=0)
    {
        sa=s;
    }
    operator float()
    {
        return sa;
    }
};
int f(float c)
{
    return (c*(1+c/100));
}
int main()
{
    cls p(35);
    std::cout<
A

47

44
Q
#include
class B
{
protected:
    int x;
public:
    B()
    {
        x=78;
    }
};
class D1: virtual public B
{
public:
    D1()
    {
        x=15;
    }
};
class D2: virtual public B
{
public:
    D2()
    {
        x=37;
    }
};
class C: public D2, public D1
{
public:
    int get_x()
    {
        return x;
    }
};
int main()
{
    C ob;
    std::cout<
A

compileaza si afiseaza 15, adica valoarea pe care i-a dat-o constructorul ultimei baze

daca mostenirea se facea in ordinea class C: public D1, public D2 atunci s-ar fi afisar 37

45
Q
#include
#include
using namespace std;
class A{
public:
    void afis()
    {
        cout< (x);
    b1->afis();

}

A

se afiseaza aici

46
Q
#include 
template 
class cls
{
    tip z;
public:
    cls(tip i)
    {
        z=i;
    }
    tip operator-(cls a)
    {
        return z-a.z;
    }
    int operator>=(cls a)
    {
        return z>=a.z;
    }
};
template 
tip dif(tip x, tip y)
{
    return x-y;
}
int dif(cls x, cls y)
{
    return x>=y?x-y:y-x;
}
int main()
{
    cls i=3;
    cls j=4;
    std::cout<
A

nu compileaza

cum repar? pune intrebare pe stackoverflow

47
Q
class cls
{
    int x;
public:
    cls(int i=0)
    {
        x=i;
    }
    cls(cls&amp; ob)
    {
        x=ob.x;
    }
};
cls&amp; f(cls &amp;c)
{
    return c;
}
int main()
{
    cls r;
    cls s=r;
    f(f(f(s)));
}
A

ruleaza si nu afiseaza nimic

48
Q
#include
class cls
{
    static int i;
    int j;
public:
    cls(int x=7)
    {
        j=x;
    }
    static int imp(int k)
    {
        cls a;
        return i+k+a.j;
    }
};
int cls::i;
int main()
{
    int k=5;
    std::cout<
A

12

49
Q
#include
class cls
{
    int x;
public:
    cls(int i=32)
    {
        x=i;
    }
    int f() const
    {
        return x++;
    }
};
void main()
{
    const cls d(-15);
    std::cout<
A

nu compileaza, se incerca modificarea unui obiect const

50
Q
#include 
float f(int y)
{
    try
    {
        if (y%2)
            throw y/2;
    }
    catch (int i)
    {
        if (i%2)
            throw;
        std::cout<>x;
        if (x)
            f(x);
        std::cout<
A

Numarul 3 e bun!

51
Q
#include
class B
{
    int x;
public:
    B(int i=2):x(i) {}
    int get_x() const
    {
        return x;
    }
};
class D: public B
{
    int *y;
public:
    D(int i=2):B(i)
    {
        y=new int[i];
        for(int j=0; j<i></i>
A

nu compileaza: am const D& a si se apeleaza a[i] iar supraincarcarea operatorului [] nu e const => se trimite un this constant la o metoda neconstanta

daca fac supraincarcarea operatorului [] const atunci se afiseaza 11111

52
Q
#include
class B
{
    int x;
public:
    B(int i=10)
    {
        x=i;
    }
    int get_x()
    {
        return x;
    }
};
class D: public B
{
public:
    D(int i):B(i) {}
    D operator+(const D&amp; a)
    {
        return x+a.x;
    }
};
int main()
{
    D ob1(7), ob2(-12);
    std::cout<
A

nu compileaza pentru ca x este private, nu protected

daca era protected se afisa -5

as putea si sa inlocuiesc return x+a.x; cu x+a.get_x(); doar ca trebuie sa fac getterul const ca asa trimit un obiect const la o functie neconst

53
Q
#include
class B
{
public:
    int x;
    B(int i=16)
    {
        x=i;
    }
    B f(B ob)
    {
        return x+ob.x;
    }
};
class D: public B
{
public:
    D(int i=25)
    {
        x=i;
    }
    B f(B ob)
    {
        return x+ob.x+1;
    }
    void afisare()
    {
        std::cout<f(*p2));
    std::cout<x;
    return 0;
}
A

41

54
Q
#include
class cls
{
    int *v,nr;
public:
    cls(int i)
    {
        nr=i;
        v=new int[i];
        for (int j=1; j
A

garbege000-1500000

55
Q
#include
class cls
{
    int x;
public:
    cls(int i=-20)
    {
        x=i;
    }
    const int&amp; f()
    {
        return x;
    }
};
int main()
{
    cls a(14);
    int b=a.f()++;
    std::cout<<b></b>
A

eroare: incerc sa modific o valoare constanta const int&

daca sterg const-ul de la f compileaza si afiseaza 14

56
Q
#include
class B
{
    static int x;
    int i;
public:
    B()
    {
        x++;
        i=1;
    }
    ~B()
    {
        x--;
    }
    static int get_x()
    {
        return x;
    }
    int get_i()
    {
        return i;
    }
};
int B::x;
class D: public B
{
public:
    D()
    {
        x++;
    }
    ~D()
    {
        x--;
    }
};
int f(B *q)
{
    return (q->get_i())+1;
}
int main()
{
    B *p=new B;
    std::cout<
A

nu compileaza pentru ca x este private si nu am acces la el din D

daca pun protected la inceputul lui B atunci afiseaza 221

57
Q
#include
class B
{
    int x;
public:
    B(int i=17)
    {
        x=i;
    }
    int get_x()
    {
        return x;
    }
    operator int()
    {
        return x;
    }
};
class D: public B
{
public:
    D(int i=-16):B(i) {}
};
int main()
{
    D a;
    std::cout<<27+a;
    return 0;
}
A

11

58
Q
#include
class cls
{
    static int x;
public:
    cls (int i=1)
    {
        x=i;
    }
    cls f()
    {
        return x;
    }
    static int g()
    {
        return f()/2;
    }
};
int cls::x=7;
int main()
{
    cls ob;
    std::cout<
A

nu compileaza, nu pot sa apelez functia f fara obiect

merge adca o scriu: 
static int f()
{
    return x;
}
59
Q
#include
class cls
{
    static int x;
public:
    cls (int i=1)
    {
        x=i;
    }
    cls f(cls a)
    {
        return x+a.x;
    }
    static int g()
    {
        return f()/2;
    }
};
int cls::x=7;
int main()
{
    cls ob;
    std::cout<
A
nu compileaza
daca inlocuiesc functia f cu 
static int f()
    {
        return x;
    }
functioneaza
60
Q
#include
class cls
{
    int *v,nr;
public:
    cls(int i=0)
    {
        nr=i;
        v=new int[i];
        for (int j=0; j
A

afiseaza 8693064 8654400 12 -30 24 30 -30 42 48 54

intreaba pe stack overflow

61
Q
#include
class B
{
    int i;
public:
    B()
    {
        i=1;
    }
    int get_i()
    {
        return i;
    }
};
class D: public B
{
    int j;
public:
    D()
    {
        j=2;
    }
    int get_j()
    {
        return j;
    }
};
int main()
{
    B *p;
    int x=0;
    if (x)
        p=new B;
    else
        p=new D;
    if (typeid(p).name()=="D*")
        std::cout<get_j();
    return 0;
}
A

eroare: trebuie sa include

daca includ typeinfo nu afiseaza nimic, dar ruleaza

62
Q
#include 
class cls
{
    int x;
public:
    cls(int i)
    {
        x=i;
    }
    int set_x(int i)
    {
        int y=x;
        x=i;
        return y;
    }
    int get_x()
    {
        return x;
    }
};
int main()
{
    cls *p=new cls[10];
    int i=0;
    for(; i<10; i++)
        p[i].set_x(i);
    for(i=0; i<10; i++)
        std::cout<<p></p>
A

eroare: nu am constructor fara parametri

daca pun valoare implicita la constructor imi da eraore ca nu gaseste get_x(int)

=> tai i de la get_x() si atunci functioneaza

afiseaza 0123456789

63
Q
#include 
template
int f(T x, T y)
{
    return x+y;
}
int f(int x, int y)
{
    return x-y;
}
int main()
{
    int a=5;
    float b=8.6;
    std::cout<
A

-3

64
Q
#include
class cls
{
    int *v,nr;
public:
    cls(int i)
    {
        nr=i;
        v=new int[i];
        for (int j=1; j
A

x[4] si not an lvalue

rezolvare: int& operator

65
Q
#include 
using namespace std;
class B
{
    int i;
public:
    B()
    {
        i=50;
    }
    virtual int get_i()
    {
        return i;
    }
};
class D: public B
{
    int j;
public:
    D()
    {
        j=47;
    }
    int get_j()
    {
        return j;
    }
};
int main()
{
    D *p=new B;
    cout<get_i();
    cout<get_j();
    return 0;
}
A

pointer la clasa derivata instantiat cu pointer din clasa de baza

daca modific: D *p=new D; sau B *p=new B; sau B *p=new D; compileaza

ce afiseaza in fiecare caz?

1) 5047
2) 50 si valoare nedeterminata
3) 5047

66
Q
#include
using namespace std;
template
int functie(X x, X y)
{
    return x+y;
}
int functie(int x, int *y)
{
    return x-*y;
}
int main()
{
    int a=27, *b=new int(45);
    cout<
A

-18

67
Q
#include 
using namespace std;
class A
{
protected:
    static int x;
private:
    int y;
public:
    A(int i)
    {
        x=i;
        y=-i+3;
    }
    int put_x(A a)
    {
        return x+a.y;
    }
};
int A::x=5;
int main()
{
    A a(10);
    cout<
A

3

68
Q
#include
using namespace std;
class A
{
    int i;
public:
    A(int x=3):i(x) {}
    virtual int get_i()
    {
        return i;
    }
};
class B: public A
{
    int j;
public:
    B(int x=10):j(x) {}
    virtual int get_j()
    {
        return A::get_i()+j;
    }
};
int main()
{
    A o1(5);
    B o2;
    cout<
A

5133

69
Q
#include 
using namespace std;
class B
{
protected:
    int i;
public:
    B(int j=5)
    {
        cout << " cb ";
        i=j-2;
    }
    ~B()
    {
        cout << " db ";
    }
    int get_i()
    {
        return i;
    }
};
class D1: public B
{
    int j;
public:
    D1(int k=10)
    {
        cout << " cd1 ";
        j=i-k+3;
    }
    ~D1()
    {
        cout << " dd1 ";
    }
};
class D2: public D1
{
    int k;
public:
    D2(int l=15)
    {
        cout << " cd2 ";
        k=i+j-l+3;
    }
    ~D2()
    {
        cout << " dd2 ";
    }
};
D1 f(D1 d1, D2 d2)
{
    return d1.get_i()+d2.get_i();
}
int main()
{
    D2 ob2;
    D1 ob1(3);
    cout<
A

eroare: j e privat si e folosit in D2

daca il fac pe j protected o sa se afiseze:
cb cd1 cd2 cb cd1 cb cd1 6 dd1 db dd1 db dd2 dd1 db dd1 db dd2 dd1 db

70
Q
#include 
using namespace std;
class B
{
protected:
    int i;
public:
    B(int j=5)
    {
        cout << " cb ";
        i=j-2;
    }
    ~B()
    {
        cout << " db ";
    }
    int get_i()
    {
        return i;
    }
};
class D1: public B
{
protected:
    int j;
public:
    D1(int k=10)
    {
        cout << " cd1 ";
        j=i-k+3;
    }
    ~D1()
    {
        cout << " dd1 ";
    }
};
class D2: public D1
{
    int k;
public:
    D2(int l=15)
    {
        cout << " cd2 ";
        k=i+j-l+3;
    }
    ~D2()
    {
        cout << " dd2 ";
    }
};
D1 f(D1 d1, D2 d2)
{
    return d1.get_i()+d2.get_i();
}
int main()
{
    D2 ob2;
    D1 ob1(3);
    cout<
A

cb cd1 cd2 cb cd1 cb cd1 6 dd1 db dd1 db dd2 dd1 db dd1 db dd2 dd1 db

hai sa o luam pe rand:
cb cd1 cd2 -crearea lui ob2
cb cd1 -crearea lui ob1
cb cd1 -crearea obiectului temporar returnat de f : D(6)
6 - f(ob1,ob2).get_i()+ob2.get_i();
dd1 db -distrugerea obiectului temporar returnat de f
dd1 db -distrugerea copiei lui ob1 cu care se lucreaza in f : d1
dd2 dd1 db -distrugerea copiei lui ob2 cu care se lucreaza in f : d2
dd1 db -distrugerea lui ob1
dd2 dd1 db - distrugerea lui ob2

71
Q
#include 
using namespace std;
class B
{
protected:
    int i;
public:
    B(int j=5)
    {
        cout << " cb ";
        i=j-2;
    }
    ~B()
    {
        cout << " db ";
    }
    int get_i()
    {
        return i;
    }
};
class D1: public B
{
protected:
    int j;
public:
    D1(int k=10)
    {
        cout << " cd1 ";
        j=i-k+3;
    }
    ~D1()
    {
        cout << " dd1 ";
    }
};
class D2: public D1
{
    int k;
public:
    D2(int l=15)
    {
        cout << " cd2 ";
        k=i+j-l+3;
    }
    ~D2()
    {
        cout << " dd2 ";
    }
};
D1 f(D1&amp; d1, D2&amp; d2)
{
    return d1.get_i()+d2.get_i();
}
int main()
{
    D2 ob2;
    D1 ob1(3);
    cout<
A

cb cd1 cd2 cb cd1 cb cd1 6 dd1 db dd1 db dd2 dd1 db

72
Q
#include 
using namespace std;
class B
{
protected:
    int x;
public:
    B(int i=3)
    {
        x=i;
    }
    virtual B f(B ob)
    {
        return x+ob.x;
    }
    void afisare()
    {
        cout<f(*p2));
    p3->afisare();
    return 0;
}
A

se afiseaza 9

pentru constructia obiectului 3 se apeleaza f din B pentru ca pot sa fac usor upcasting dar nu downcasting

73
Q
#include 
using namespace std;
class cls
{
    int x;
public:
    cls(int i)
    {
        x=i;
    }
    int set_x(int i)
    {
        int y=x;
        x=i;
        return y;
    }
    int get_x()
    {
        return x;
    }
};
int main()
{
    cls *p=new cls[10];
    for(int i=3; i<9; i++)
        p[i].set_x(i);
    for(int i=0; i<4; i++)
        cout<<p></p>
A

nu compeleaza pentru ca nu am constructor fara parametri

daca pun cls(int i=0) se afiseaza 0003

74
Q
#include 
using namespace std;
struct X {   int i;
public:   X(int ii ) { i = ii; };
     int f1() const { return i; }
X f2() const {   int i=this->f1(); return X(74-i); }};
const X f3() {   return X(8); }
int f4(const X&amp; x) { return x.f1(); }
int main() { X ob(19);
    cout<
A

se afiseaza 55

75
Q
#include 
using namespace std;
class A
{   protected: int x;
    public: A(int i):x(i){}
            int get_x(){ return x; } };
class B: private A
{   protected: int y;
    public: B(int i,int j):y(i),A(j){}
            int get_y(){ return get_x()+y; } };
class C: private B
{   protected: int z;
    public: C(int i,int j,int k):z(i),B(j,k){}
            int get_z(){ return get_x()+get_y()+z; } };
int main()
{   C c(1,2,3);
    cout<
A

nu ruleaza

C(int i,int j,int k):z(i),B(j,k){} linia asta functioneaza bine pentru ca C are acces la B si B(int i,int j):y(i),A(j){} B are acces la A

in schimb cand ajunge la

int get_z(){ return get_x()+get_y()+z; }
A nu este o baza accesibila a lui C

rezolvare: fac mostenirea lui A in B publica si se afiseaza 9

76
Q
#include 
using namespace std;
class A
{   static int *x;
    public: A() {}
            int get_x() { return (++(*x))++; } };
int *A::x(new int(10));
int main()
{   A *p=new A,b;
    cout<get_x();
    return 0;
}
A

11 13

77
Q
#include 
using namespace std;
class X {   int i;
public:   X(int ii = 5) { i = ii; cout<< i<< " ";};
          const int tipareste(int j) { cout<<i>tipareste(7);
  return 0;
}
</i>
A

nu compileaza pentru ca se trimite un obiect constant ca this unei metode neconstante

rezolvare: fac metoda tipareste constanta si atunci se afiseaza 7 7 7 14

78
Q
#include 
using namespace std;
class X {public: int i;
public:   X(int ii = 5) { i = ii; cout<< i<< " ";};
          const int tipareste(int j) const { cout<<i></i>
A

se afiseaza 3

observam ca daca am initializa X O2=O s-ar afisa 7
dar fiindca initializam X& O2=O O2 este practic un alt nume pentru valoarea de la adresa accea de memorie

79
Q
#include
using namespace std;
class A
{ int i;
  public: A(int x=2):i(x+1) {}
  virtual int get_i() { return i; }};
class B: public A
{ int j;
  public: B(int x=20):j(x-2) {}
  virtual int get_j() {return A::get_i()+j; }};
int main()
{ A o1(5);
  B o2;
  cout<
A

6 21 3

80
Q
#include 
using namespace std;
class A
{   protected: static int x;
    private: int y;
    public: A(int i) { x=i; y=-i+4; }
        int put_x(A a) { return a.x+a.y; } };
int A::x=7;
int main()
{   A a(10);
    cout<
A

4

81
Q
#include
using namespace std;
template
int functie(X x, X y)
{ return x+y;
}
int functie(int &amp; x, int *y)
{ return x-*y;
}
int main()
{ int a=7, *b=new int(4);
  cout<
A

3

82
Q
#include 
using namespace std;
class B
{ int i;
  public: B() { i=80; }
          virtual int get_i() { return i; }
};
class D: public B
{ int j;
  public: D() { j=27; }
          int get_j() {return j; }
};
int main()
{ D *p=new B;
  cout<get_i();
  cout<get_j();
  return 0;
}
A

nu compileaza pentru ca am downcasting, retin obiect de tip B in D*

pot sa fac B= new B => 80 gibberish
pot sa fac D
= new D => 8027
pot sa fac B*= new D => 8027

83
Q
#include 
using namespace std;
class B
{ protected: int i;
  public: B(int j=5) {cout << " cb "; i=j-2; }
          ~B(){ cout << " db ";}
          int get_i() { return i; } };
class D1: public B
{ int j;
  public: D1(int k=10) {cout << " cd1 "; j=i-k+3; }
          ~D1(){ cout << " dd1 ";} };
 class D2: public D1
{ int k;
  public: D2(int l=15) {cout << " cd2 "; k=i-l+3; }
           ~D2(){ cout << " dd2 ";}  };
D1 f(D1 d1, D2 d2) {return d1.get_i()+d2.get_i(); }
int main()
{ D2 ob2; D1 ob1(3);
  cout<
A

cb cd1 cd2 cb cd1 cb cd1 6 dd1 db dd1 db dd2 dd1 db dd1 db dd2 dd1 db

84
Q
#include 
using namespace std;
class A
{   static int *x;
    public: A() {}
            int get_x() { return (++(*x))++; } };
int *A::x(new int(19));
int main()
{   A *p=new A,b;
    cout<get_x();
    return 0;
}
A

20 22

85
Q
#include 
using namespace std;
class X {   int i;
public:   X(int ii = 5) { i = ii; cout<< i<< " ";};
          const int tipareste(int j) const { cout<<i>tipareste(7);
  return 0;
}
</i>
A

7 7 7 7 14

86
Q
#include 
using namespace std;
class A
{   protected: int x;
    public: A(int i):x(i){}
            int get_x(){ return x; } };
class B: A
{   protected: int y;
    public: B(int i,int j):y(i),A(j){}
            int get_y(){ return get_x()+y; } };
class C: protected B
{   protected: int z;
    public: C(int i,int j,int k):z(i),B(j,k){}
            int get_z(){ return get_x()+get_y()+z; } };
int main()
{   C c(5,6,7);
    cout<
A

nu compileaza: get_x() nu e accessible din C

modificare: fac mostenirea din A in B publica
observatie: constructorii sunt accesibili din lista de initializare la orice adancime, dar restul functiilor nu

87
Q
#include 
using namespace std;
class cls
{	int x;
public: cls(int i=2) { x=i; }
int set_x(int i) { int y=x; x=i; return y; }
int get_x(){ return x; } };
int main()
{ cls *p=new cls[15];
      for(int i=2;i<8;i++) p[i].set_x(i);
	  for(int i=1;i<6;i++) cout<<p></p>
A

22345

88
Q
#include 
using namespace std;
struct X {   int i;
public:   X(int ii ) { i = ii; };
     int f1() { return i; }
X f2() const {   int i=this->f1(); return X(34-i); }};
const X f3() {   return X(16); }
int f4(const X&amp; x) { return x.f1(); }
int main() {X ob(11);
    cout<
A

nu compileaza pentru ca f1 nu e const

avem 2 locuri in care apare aceasta eroare:

  1. la X f2() const { int i=this->f1(); return X(34-i); }}; cand this e const si e apelat pe el functia f1
  2. la int f4(const X& x) { return x.f1(); } cand am obiectul constant X si apelez pe el f1

daca fac f1 sa fie const afiseaza 23

89
Q
#include 
using namespace std;
class A
{ protected: static int x;
  public: A(int i=1) {x=i; }
  int get_x() { return x; }
  int&amp; set_x(int i) { int y=x; x=i; return y;}
  A operator=(A a1) { set_x(a1.get_x()); return a1;}
} a(33);
int main()
{ A a(18), b(7);
  cout<
A

nu compileaza pentru ca nu am initializat staticul x

daca adaug
int A::x=0; o sa compileze si nu o sa afiseze nimic pentru ca incerc sa afisez adresa unei valori temporare
daca set_x ar returna int ar afisa 7