Curs 3 Flashcards

1
Q

class MyClass
{
int x;
public:
MyClass(int value) { this->x = value; }
};
class Date
{
MyClass m;
public:
Date() { }
};
void main()
{
Date d;
}

A

slide 25
error C2512: ‘MyClass’: no appropriate default
constructor available

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

class MyClass
{
int x;
public:
MyClass(int value) { this->x = value; }
};
class Date
{
MyClass m;
public:
Date() : m(123) { }
};
void main()
{
Date d;
}

A

slide 26
This code will compile properly

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

class MyClass
{
int x;
public:
MyClass(int value) { this->x = value; }
};
class Date
{
MyClass m;
public:
Date() : m(123) { }
Date(int value) { }
};
void main()
{
Date d;
}

A

slide 27
error C2512: ‘MyClass’: no appropriate default
constructor available
This code will NOT compile. There is at least one constructor that does not
instantiate data member “m” from class Date.

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

class MyClass
{
int x;
public:
MyClass(int value) { this->x = value; }
};
class Date
{
MyClass m;
public:
Date() : m(123) { }
Date(int value) : m(value+10) { }
};
void main()
{
Date d;
}

A

the code compiles correctly

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

class MyClass
{
int x;
public:
MyClass(int value) { this->x = value; }
MyClass() { this->x = 0; }
};
class Date
{
MyClass m;
public:
Date() { }
Date(int value) : m(value+10) { }
};

A

slide 29
This code compiles correctly.

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

class MyClass
{
int x;
public:
};
class Date
{
MyClass m;
public:
Date() { }
Date(int value) { }
};

A

slide 30
This code compiles correctly

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

class MyClass
{
int x;
public:
MyClass(int value) { this->x = value; }
MyClass() { this->x = 0; }
};
class Date
{
MyClass m = { 123 };
public:
Date() { }
Date(int value) { }
};

A

slide 31
yes

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

class MyClass
{
int x;
public:
MyClass(int value) { this->x = value; }
MyClass() { this->x = 0; }
};
class Date
{
MyClass m = 123;
public:
Date() { }
Date(int value) { }
};

A

slide 31
yes

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

class Tree {
public:
Tree(const char * name) { printf(“Tree: %s\n”, name); }
};
class Car {
public:
Car(const char * name) { printf(“Car: %s\n”, name); }
};
class Animal {
public:
Animal(const char * name) { printf(“Animal: %s\n”, name); }
};
class Object
{
Tree t;
Car c;
Animal a;
public:
};
void main() {
Object obj;
}

A

slide 32
This code will not compile because data members obj.t , obj.c and obj.a need a
custom call to their own constructor.

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

class Tree {
public:
Tree(const char * name) { printf(“Tree: %s\n”, name); }
};
class Car {
public:
Car(const char * name) { printf(“Car: %s\n”, name); }
};
class Animal {
public:
Animal(const char * name) { printf(“Animal: %s\n”, name); }
};
class Object
{
Tree t;
Car c;
Animal a;
public:
Object(): t(“oak”), a(“fox”), c(“Toyota”) {}
};
void main() {
Object obj;
}

A

slide 33
Output:
Tree: oak
Car: Toyota
Animal: fox

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

class Tree {
public:
Tree() { printf(“CTOR: Tree\n”); }
};
class Car {
public:
Car() { printf(“CTOR: Car\n”); }
};
class Animal {
public:
Animal() { printf(“CTOR: Animal\n”); }
};
class Object
{
Tree t1,t2;
Car c;
Animal a;
};
void main() {
Object obj;
}

A

slide 34
Output:
CTOR: Tree
CTOR: Tree
CTOR: Car
CTOR: Animal

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

class Object
{
int x, y, z;
public:
Object(int value) : x(value), y(xx), z(valuey) {}
};
void main()
{
Object o(10);
}

A

slide 35
o.x = 10 (the first one to be computed)
❑ o.y = o.x * o.x = 10 * 10 = 100 (the second one to be computed)
❑ o.z = 10 * o.y = 10 * 100 = 1000 (the third one to be computed)

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

class Object
{
int x, y, z;
public:
Object(int value) : y(value), z(value/2), x(y*z) {}
};
void main()
{
Object o(10);
}

A

slide 36
o.x = o.y * o.z = unknown results (it depends on the values that resides on the stack when
the instance “o” is created). (the first one to be computed !)
❑ o.y = 10 (the second one to be computed)
❑ o.z = 10 (value) / 2= 10 / 2 = 5 (the third one to be computed)

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

class Date
{
private:
int x;
const int y;
public:
};
void main()
{
Date d;
}

A

slide 38
const int y nu e initializat

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

class Date
{
private:
int x;
int & y;
public:
};
void main()
{
Date d;
}

A

slide 38
int & y nu e initializat

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

class Date
{
private:
int x;
const int y;
public:
Date();
};
Date::Date() : x(100), y(123)
{
}
void main()
{
Date d;
}

A

slide 40
The code compiles – y is initialized with value 123 in example

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

class Date
{
private:
int x;
int & y;
public:
Date();
};
Date::Date() : x(100), y(x)
{
}
void main()
{
Date d;
}

A

slide 40
yes

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

class Date
{
private:
int x;
const int y;
public:
Date();
Date(int value);
};
Date::Date() : x(100), y(123)
{
}
Date::Date(int value) : x(value)
{
}

A

slide 41
no

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

class Date
{
private:
int x;
int & y;
public:
Date();
Date(int value);
};
Date::Date() : x(100), y(x)
{
}
Date::Date(int value) : x(value)
{
}

A

slide 41
no

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

class Date
{
private:
int x;
const int y;
public:
Date();
Date(int value);
};
Date::Date() : x(100), y(123)
{
}
Date::Date(int value) : x(value), y(value*value)
{
}
void main()
{
Date d;
D

A

slide 42
yes

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

class Date
{
private:
int x;
const int y;
public:
Date();
};
Date::Date() : x(100)
{
y = 123;
}
void main()
{
Date d;
}

A

slide 43
no

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

class Date
{
private:
int x;
int & y;
public:
Date();
};
Date::Date() : x(100)
{
y = x;
}
void main()
{
Date d;
}

A

slide 43
no

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

class Date
{
private:
int x;
const int y = 123;
public:
Date();
Date(int value);
};
Date::Date() : x(100)
{
}
Date::Date(int value) : x(value), y(value*value)
{
}
void main()
{
Date d;
Date d2(100);
}

A

Slide 44
This code compiles correctly

24
Q

class Date
{
private:
int x;
int & y = x;
public:
Date();
Date(int value);
};
Date::Date() : x(100)
{
}
Date::Date(int value) : x(value), y(value)
{
}
void main()
{
Date d;
Date d2(100);
}

A

slide 44
This code compiles correctly

25
class Date { int & y; public: Date() : y(123) {} }; void main() { Date d; }
slide 45 eroare References that are not constant can not be instantiated with a constant value
26
class Date { const int & y; public: Date() : y(123) {} }; void main() { Date d; }
slide 45 yes
27
class Date { public: const int & y; Date() : y(123) {} void Test() { int a[1000]; for (int tr = 0; tr < 1000; tr++) a[tr] = 50; } }; void main() { Date d; printf(“%d\n”,d.y); d.Test(); printf(“%d\n”,d.y); }
slide 49 Output: 123 50 s-a suprascris stackul
28
class Object { int x, y; public: Object(int value) : x(value), y(value) {} Object() : Object(0) , y(1) { } }; void main() { Object o; }
slide 52 This code will not compile. When calling a constructor from another constructor initialization list, other initializations are not possible
29
class Object { int x, y; public: Object(int value) : x(value), y(value) {} Object() : Object(0) { y = 1; } }; void main() { Object o; }
slide 54 This code will compile.
30
class Object { int x, y; const int z; public: Object(int value) : x(value), y(value), z(value) {} Object() : Object(0) { y = 1; } }; void main() { Object o; }
slide 57 yes
31
class Data { public: int x; char t; const char* m; }; void main() { Data d1{ 10, 'A', "test" }; Data d2 = { 5, 'B', "C++" }; Data array[] = { { 1, 'A', "First element" }, { 2, 'B', "Second element" }, { 3, 'C', "Third element" }, }; }
slide 62 This code works, but it is important for data members to be public
32
class Data { int x; public: char t; const char* m; }; void main() { Data d1{ 10, 'A', "test" }; Data d2 = { 5, 'B', "C++" }; Data array[] = { { 1, 'A', "First element" }, { 2, 'B', "Second element" }, { 3, 'C', "Third element" }, }; }
slide 63 This code will not work as “x” is not public ! If a class has at least one member that is NOT public and no matching constructor, these assignments will not be possible.
33
class Data { int x; public: char t; const char* m; Data(int xx, char tt, const char * mm) : x(xx), t(tt), m(mm) {}; }; void main() { Data d1{ 10, 'A', "test" }; Data d2 = { 5, 'B', "C++" }; Data array[] = { { 1, 'A', "First element" }, { 2, 'B', "Second element" }, { 3, 'C', "Third element" }, }; }
slide 64 This code will compile because a proper public constructor has been added.
34
class Data { public: int x; char t; const char* m; Data(int xx, char tt) : x(xx), t(tt), m(nullptr) {}; }; void main() { Data d1{ 10, 'A', "test" }; Data d2 = { 5, 'B', "C++" }; Data array[] = { { 1, 'A', "First element" }, { 2, 'B', "Second element" }, { 3, 'C', "Third element" }, }; }
slide 65 This code will not compile. If there at least one constructor and its parameters do NOT match the ones from the initialization list, the compiler will throw an error
35
class Data { int x; public: char t; const char* m; Data(int xx, int tt, const void * p) : x(xx), t(tt), m((const char *)p) {}; }; void main() { Data d1{ true, 'A', "test" }; Data d2 = { true, 'A', "test" }; }
slide 66 yes
36
class Data { int x; public: char t; const char* m; Data(…) {}; }; void main() { Data d1{ true, 'A', "test" }; Data d2 = { true, 'A', "test" }; }
slide 67 yes
37
class Data { int x; public: char t; const char* m; Data(int xx, int tt, const void * p) : x(xx), t(tt), m((const char *)p) {}; }; void main() { Data d1{}; Data d2 = {}; }
slide 68 This code will NOT compile. Trying to initialize this class with an empty initialization list { } will result in an error if no default constructor is present
38
class Data { int x; public: char t; const char* m; Data() {}; }; void main() { Data d1{}; Data d2 = {}; }
slide 69 yes
39
class Data { int x; public: char t; const char* m; Data(…) {}; }; void main() { Data d1{}; Data d2 = {}; }
slide 69 yes
40
class Data { int x; public: char t; const char* m; }; void main() { Data d1 {}; }
slide 70 This code will compile. “d1” object will have the following values after the execution: ❑ d1.x = 0 ❑ d1.t = ‘\0’ ❑ d1.m = nullptr;
41
class Data { int x; public: Data(int value) : x(value) {} }; void main() { Data d = 10; }
slide 73 yes
42
class Data { int x; public: Data(int value) : x(value) {} }; void main() { Data d = ‘A’; }
74 yes
43
class Data { int x; public: Data(int value) : x(value) {} }; void main() { Data d = true; }
74 yes
44
class Data { int x; public: Data(int value) : x(value) {} }; void main() { Data d = 4.5; }
74 yes
45
class Data { int x[4]; public: Data() : x{ 1, 2, 3, 4 } {} }; void main() { Data d; }
75 yes
46
class Data { int x = 5; float y = 10.5f; bool t = false; public: Data() : x(10) { } }; void main() { Data d; }
80 the default values can be overridden in the constructor list. In this case, “x” will be initialized with 10, “y” with 10.5 and “t” with false
47
class Date { int x; public: Date(const Date &d) { x = d.x; } Date(Date &d) { x = d.x * 2; } Date(int v) { x = v; } }; int main() { const Date d(1); Date d2 = d; return 0; }
100 yes
48
class Date { int x; public: Date(Date &d) { x = d.x * 2; } Date(int v) { x = v; } }; int main() { const Date d(1); Date d2 = d; return 0; }
101 eroare In this case the code will not compile. There a copy constructor defined, but it does not accept const parameters
49
class Date { int x; public: Date(int v) { x = v; } }; int main() { const Date d(1); Date d2 = d; return 0; }
102 This code will compile.  Since there is no copy constructor defined, the compiler will generate a code that copies the data from “d” to “d2” (similar to what memcpy function does).
50
class Object { int value; static Object* instance; Object() { value = 0; } public: static Object* GetInstance() { … } }; Object* Object::instance = nullptr; void main() { Object obj1; Object * obj = new Object(); }
116 no error C2248: 'Object::Object': cannot access private member declared in class 'Object' note: see declaration of 'Object::Object' note: see declaration of 'Object'
51
class Object { int value; Object(): value(0) { } friend class ObjectUser; }; class ObjectUser { public: int GetValue(); }; int ObjectUser::GetValue() { Object o; return o.value; } void main() { ObjectUser ou; printf("%d\n", ou.GetValue()); }1
117 yes
52
class Object { int value; Object(): value(0) { } friend class ObjectUser; }; class ObjectUser { public: int GetValue(); }; int ObjectUser::GetValue() { Object o; return o.value; } void main() { Object obj; }
118 This code will NOT compile error C2248: 'Object::Object': cannot access private member declared in class 'Object' note: see declaration of 'Object::Object' note: see declaration of 'Object'
53
class Date { int value; public: Date(int x) { value = x; } }; int main() { Date d(‘0'); return 0; }
122 This code works, due to promotion mechanism (‘a’ (a char) is promoted to an int).
54
class Date { int value; public: Date(char x) = delete; Date(int x) { value = x; } }; int main() { Date d(‘0'); return 0; }
123 This code will not compile.  Using the delete keyword in this manner tells the compiler that there is a constructor that has a char parameter, but it can not be used !
55
class Date { int value; public: explicit Date(int v1, int v2, int v3) { value = v1+v2+v3; } }; int main() { Date d = { 1, 2, 3 }; return 0; }
126 explicit error C3445: copy-list-initialization of 'Date' cannot use an explicit constructor note: see declaration of 'Date::Date'