· 7 years ago · Oct 19, 2018, 03:50 PM
1Ok
2Rules are simple: Don't use your compiler, don't look anything up, answer as fast as possible
3skouliouToday at 17:19
4ok
5NotAPenguinToday at 17:19
6Ready?
7skouliouToday at 17:19
8yeah :smile:
9NotAPenguinToday at 17:20
10Ok, Q1: What is the difference between a function declaration and definition?(edited)
11skouliouToday at 17:21
12you can have multiple function declarations but only one definition(edited)
13NotAPenguinToday at 17:21
14That's one thing yeah, so what does a declaration tell the compiler vs what a definition tells it?
15skouliouToday at 17:22
16the declaration tells the compiler about the signature of the function, like arguments types and return type
17the definition tells the compiler what the function do
18NotAPenguinToday at 17:22
19Good
20skouliouToday at 17:23
21^_^
22NotAPenguinToday at 17:23
23Q2: What's a translation unit?
24skouliouToday at 17:23
25it's a source file after all preprocessing
26after all macros and defines and such are preprocessed
27NotAPenguinToday at 17:24
28Yeah
29Q3: Why does main return int?
30skouliouToday at 17:24
31main returns int to signify the result of the program execution
32it returns 0 on success, and other values to denote faileur
33NotAPenguinToday at 17:25
34Ye
35Q4: What's the proper way of creating a null pointer in C++?
36skouliouToday at 17:26
37to assign it to a nullptr
38NotAPenguinToday at 17:26
39Yeah, can you show an example?
40skouliouToday at 17:26
41int *i = nullptr;
42NotAPenguinToday at 17:26
43Ok
44skouliouToday at 17:27
45yey!
46NotAPenguinToday at 17:27
47Q5: Create a single function that takes two numbers and returns the larger of the two. This function must work for every numeric type
48skouliouToday at 17:27
49only numeric types?
50NotAPenguinToday at 17:28
51Don't worry about checking if it's a numeric type
52skouliouToday at 17:28
53ok
54template <typename T>
55T max(const T& l, const T& r) { return (l > r) ? l : r; }
56NotAPenguinToday at 17:29
57Yeah, also note you don't need the parenthesis around l > r :smile:
58Ok
59skouliouToday at 17:29
60yeah, but I like them for readability :smiley:
61NotAPenguinToday at 17:29
62Q6: What's the difference between a variable declared const and a variable declared constexpr?
63That makes perfect sense
64skouliouToday at 17:30
65this is modern C++ :/
66NotAPenguinToday at 17:30
67constexpr exists since C++11, so not that modern anymore :smile:
68skouliouToday at 17:30
69lol, I'm not familiar with those, but I'll take a wild guess
70const can only be with runtime code, while constexpr can only be used with compile time code.(edited)
71NotAPenguinToday at 17:32
72Comes close
73A variable marked constexpr can be used where a compile time context is required (for example array declaration), but also at runtime if you want
74skouliouToday at 17:33
75oh, I didn't know you could use it at runtime actually.
76NotAPenguinToday at 17:33
77Q7: Say you have a class called Foo. How do you overload the << operator so that you can do std::cout << someFoo << "\n";?
78Yeah you can
79skouliouToday at 17:34
80std::ostream& operator<<(std::ostream& os, const Foo& ins) { /* code goes here */; return os; }
81(edited)
82NotAPenguinToday at 17:35
83Yeah, now Q7b: Why is it impossible to implement it as a member function of Foo?(edited)
84skouliouToday at 17:35
85you use the friend keyword
86class Foo {
87friend std::ostream& operator<<(std::ostream& os, const Foo& ins) { /* ... */; return os; }
88}
89NotAPenguinToday at 17:38
90Yeah that's nearly the same as above. I meant as a member function of Foo, like
91class Foo
92{
93public:
94 int i;
95
96 std::ostream& operator<<(std::ostream& os)
97 {
98 os << i; //inside Foo, so I can access this?
99 return os;
100 }
101};
102
103Why does this code not work
104skouliouToday at 17:39
105because every member function has an implicit pointer the instance as first parameter
106NotAPenguinToday at 17:39
107Yes
108skouliouToday at 17:40
109so this function actually have the signature std::ostream& operator<<(Foo& ins, std::ostream& os)
110NotAPenguinToday at 17:40
111Ok
112Q8: What is a pure virtual function?
113skouliouToday at 17:41
114it's a member function declared with the keyword virtual
115and assigned a 0
116NotAPenguinToday at 17:41
117And what does it do?
118skouliouToday at 17:42
119internally?
120NotAPenguinToday at 17:42
121Like, how does it affect classes that derive from a class with a pure virtual function?
122skouliouToday at 17:42
123it builds a virtual table with function pointers, which are assigned at runtime
124they need to implement the virtual function in order to subclass it.
125NotAPenguinToday at 17:43
126Yeah
127Ok, last question
128Q9: What is RAII?
129skouliouToday at 17:44
130Resource Acquisition Is Initialization
131it means that you should allocated resources in the constructor, and free them in the destrocture
132(roughly speaking, lol)
133NotAPenguinToday at 17:45
134Sounds good
135Alright
136I'll get the quiz log to the staff so we can discuss your role
137But I think you'll get it :smiley:
138skouliouToday at 17:45
139sorry, this inappropriate, but I need to go
140oh, we're done :smile:
141I thought there are more question, lol
142NotAPenguinToday at 17:46
143Nah we're good
144skouliouToday at 17:46
145thanks, :smile: