2023-01-31
Max. 50 points
Name:
Task | Max. | Achieved |
---|---|---|
1 | 10 | |
2 | 15 | |
3 | 10 | |
4 | 15 | |
Sum | 50 |
Statement | True | False |
---|---|---|
C++ is a strict superset of C. | ||
In C++, the scope resolution operator ::
allows accessing identifiers within a given namespace. | ||
A C++ set is a container that stores
key / value pairs. | ||
A C++ struct cannot have private members. | ||
Composition is a "has-a" relationship. | ||
The C++ Core Guidelines recommend to use
struct only if the data members can vary
independently. | ||
A C++ template allows functions and classes to operate with generic types. | ||
Operator overloading is the mechanism of basing an object or class upon another object ("is-a" relationship). | ||
A default constructor is a constructor which can be called with no arguments. | ||
When providing a destructor, one should also provide (or delete) the 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 (auto it = v.rbegin(); it != v.rend(); ++it) std::cout << *it << " ";
std::cout << std::endl;
int i{3};
int& j{i};
int k{i};
j = 5;
std::cout << i << ' ' << j << ' ' << k << std::endl;
template <class Type>
void print_vector(const std::vector<Type>& v) {
for (auto it = v.rbegin(); it != v.rend(); ++it) {
std::cout << *it << std::endl;
}
}
int main() {
std::vector<int> v{1, 2, 3};
print_vector(v);
return 0;
}
std::set<int> s{7, 5, 16, 8};
s.insert(5);
s.insert(6);
std::cout << s.size() << std::endl;
struct TreeNode
that serves as an element
of a binary tree data structure. The objects created
by the struct should contain a value of any given type as well as a
pointer to the left and right child TreeNode
s.
Define the struct as it is seen in the diagram above. You also have to add
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 left
and right child nodes. Do not allow implicit conversion to your new type.
(10 points)
template <class Type>
struct TreeNode {
Type value;
TreeNode* left = nullptr;
TreeNode* right = nullptr;
explicit TreeNode(Type value) : value{value} {};
explicit TreeNode(Type value, TreeNode* next, TreeNode* 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_;
};