-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyContainer.cpp
77 lines (67 loc) · 1.67 KB
/
MyContainer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "MyContainer.hpp"
MyContainer::MyContainer(std::size_t size):
size(size),
numbers(new int[size]),
count(0){
container_number++;
}
MyContainer::MyContainer(const MyContainer& other):
size(other.size),
numbers(other.numbers),
count(other.count){
container_number++;
}
MyContainer::MyContainer(MyContainer&& other):
size(other.size),
numbers(other.numbers),
count(other.count){
container_number++;
other.numbers = nullptr;
}
MyContainer::MyContainer(std::initializer_list<int> data){
container_number++;
size = data.size();
numbers = new int[size];
int i {0};
for(auto j : data) {
numbers[i++] = j;
count++;
}
}
MyContainer::~MyContainer(){
delete [] numbers;
}
void MyContainer::operator=(const MyContainer& other){
container_number++;
size = other.size;
numbers = other.numbers;
count = other.count;
}
void MyContainer::operator=(MyContainer&& other){
container_number++;
size = other.size;
numbers = other.numbers;
count = other.count;
numbers = nullptr;
}
void MyContainer::add(int n){
if(count >= size){
throw std::invalid_argument("Invalid argument\n");
}
else{
numbers[count] = n;
count++;
}
}
void MyContainer::print_content(){
if(count > 0){
std::cout << "Container nr: " << container_number << std::endl;
for(std::size_t i = 0; i < count; i++){
std::cout << "[" << i << "]\tval: " << numbers[i] << std::endl;
}
}
else{
std::cout << "Container nr: " << container_number << " is empty\n";
}
}
int MyContainer::container_number = 0;