2023-01-11
Max. 50 points
Name:
Task | Max. | Achieved |
---|---|---|
1 | 10 | |
2 | 15 | |
3 | 10 | |
4 | 15 | |
Sum | 50 |
Statement | True | False |
---|---|---|
Every C code can be directly compiled as C++. | ||
In C++, files automatically create namespaces. | ||
The C++ standard library offers containers like
list , map and set . | ||
A C++ struct offers roughly the same features as a C++ class. | ||
Encapsulaton allows limiting direct access to an object's data members. | ||
The C++ Core Guidelines recommend to use
struct only if the data members can vary
independently. | ||
A C++ template is a guideline describing best practices for writing code. | ||
Operator overloading allows to use the built in C++ operators on own types. | ||
A default constructor is always automatically generated. | ||
It is a best practice to provide a destructor, even without providing a copy constructor and copy assignment. |
int main(.)
(unless main is part of the snippet).
All neccessary
#include
s are present.stdout
?
Write their exact output below each snippet.
int val(123);
std::cout << "Hello " << val << std::endl;
std::vector<int> v{1, 2, 3};
for (const auto& e : v) std::cout << e << " ";
std::cout << std::endl;
void f(int i) {
i = 5;
}
int main() {
int i{3};
int& j{i};
f(i);
f(j);
std::cout << i << ' ' << j << std::endl;
return 0;
}
template <class Type>
void print_vector(const std::vector<Type>& v) {
for (const auto& e : v) {
std::cout << e << std::endl;
}
}
int main() {
std::vector<int> v{1, 2, 3};
print_vector(v);
return 0;
}
std::list<int> l{7, 5, 16, 8};
l.push_front(1);
l.push_back(3);
std::cout << l.size() << std::endl;
struct ListNode
that serves as an element
of a doubly linked list. The objects created
by the struct should contain a value of any given type as well as a
pointer to the next and previous ListNode
s. Define two
constructors. The first
takes a single argument holding the value of a node. The second
takes three arguments: the value of a node and a pointer to the previous
and next nodes. Do not allow implicit conversion to your new type.
(10 points)
template <typename T>
struct ListNode {
T value;
ListNode* next = nullptr;
ListNode* prev = nullptr;
explicit ListNode(T value) : value{value} {};
explicit ListNode(T value, ListNode* next, ListNode* prev)
: value{value}, next{next}, prev{prev} {};
};
Vector
with operations to
insert elements at the beginning and at the end. You do not need to define
any operations, just declare them. Declare the big 3. It is enough to
define the destructor.
Make the compiler generate the default constructor.
Properly use encapsulation. Give all data members reasonable default values.
(15 points)
template <class T>
class Vector {
public:
void push_back(T value);
void push_front(T value);
Vector() = default;
~Vector() { delete[] data_; };
Vector(const Vector& v);
Vector& operator=(const Vector& other);
private:
T* data_ = nullptr;
size_t size_;
size_t reserved_;
};