Hello, I'm a C++ beginner.
You want to create a function that sends a one-dimensional array as a factor and obtains size, item, and a print function to output.
I made it as far as I know, but it's blocked afterwards.
Here's the question.
class Set {
private:
int *set;
int size = 0;
public:
Set(int *set, int size);
int getsize();
int getitem(int index);
void print();
};
Set::Set(int *set, int size) {
int item = 0;
for (int i = 0; i < size; i++) {
item = set[i];
}
int item size = sizeof(item) / sizeof(int); // why 1 is output..?
}
int Set::getsize() {
//return itemSize?
}
int Set::getitem(int index) {
}
void Set::print() {
}
void main() {
int a[] = { 3,1,9,10,5,8 };
int b[] = { 2,4,5,8 };
Set a1(a, 6);
Set a2(b, 4);
}
Hello! I will reply to you again this time... h;
I'll comment on the code and explain it to you.
class Set {
private:
int *set;
//intsize; Unnecessary variable. This is because a variable called item size was created.
int itemsize;
public:
Set(int *set, int _size);
~Set();
int getsize();
int getitem(int index);
void print();
};
Please use Set::Set(int *_set /*) with the same name. */, int_size : itemsize(0)// Initialize member variable.
{
// int item = 0; I think you used it to store an int-
// But you actually declared an int-type variable. And
// Since the constructor is also a function, the local variable declared inside the function is that the function is
// It disappears together at the end.
this->set = new int[_size]{0,};
for (int i = 0; i < _size; i++) {
//item = set[i]; stored in the int-type variable will overwrite it again, so only the last data will be saved.
this->set[i] = _set[i];
}
//itemsize = sizeof(item) /sizeof(int); // Why is 1 output..?
// int item is a 4-byte int type variable. So if you divide by the size of int, you get 1.
item size = _size; // We'll take over the size anyway, so you can use it as it is.
}
int Set::getsize() {
Return item size; // You can use it as it is.
}
int Set::getitem(int index) {
return this->set[index];
}
void Set::print() {
}
Set::~Set() {
delete this->set;
}
int main() {
int a[] = { 3,1,9,10,5,8 };
int b[] = { 2,4,5,8 };
Set a1(a, 6);
Set a2(b, 4);
return 0;
}
© 2024 OneMinuteCode. All rights reserved.