I still can't seem to grasp the whole setters and getters in C++
This is the assignment:
1 2 3 4 5 6 7
In this file, you declare a class named MyString.
– This class contains 3 private attributes, str, size, and strCount.
– str is a char* that points to a dynamic array of characters.
– size is an integer that contains the current size of the string.
– strCount is a static member variable that keeps track of the number of MyString objects that currently
exist in memory. Its value is initially 0 before any MyString object is created.
– Define public getters and setters for the 2 instance variables, as well as a getter for the static variable.
I would love if someone can point me in the right direction and help me define the static and char* getter and setters because I am having lots of trouble.
The #ifndef at line 2 needs a #endif at the end of the file.
str should be char *str. It points to the first character in the dynamic array.
Your setSize() and getSize() are reversed. In other words, setSize() gets the size and getSize() sets it. So swap the names.
Also, when you set the size, you need to do something with str. What if the new size is smaller than the old size? What if the new size is larger?
Finally, you haven't handled the requirement for strCount yet. You need to create a constructor, copy constructor, and destructor. If you've learned about move constructors then you'll need one of those too.
#ifndef MYSTRING_H
#define MYSTRING_H
class MyString {
private:
char* str[]; // this is an array of pointers to char. Did you mean "char* str;" ?
int size;
static strCount = 0; // type missing. Did you mean "static int strCount"?
public:
void setStr(char* str) { str = s; } // 1) type mismatch; see above
// 2) "s" is undefined: your argument name is "str"
void getStr() { return str; } // if you return something, your function can't be 'void'.
// ----------------------------------------------------------------------
// You can use names as you like, of course, but usually the "get...()"
// method returns a value, while the "set...()" one accepts an argument.
// I mean, perhaps you want to swap the following two function names:
int setSize() { return size; }
void getSize(int size) { size = sz; }
// ----------------------------------------------------------------------
};
#endif // MYSTRING_H
Flaze07 wrote:
I am using you as guinea pig
Semirxbih wrote:
What's the proper code for the auto function @Flaze07?
It seems Flaze07 was asking you the same question.
As far as I know, it should be “constexpr static auto” (or “const static auto” if it’s expected to become an integer type):
1 2 3
class MyString {
constexprstaticauto myproperty = 1.3;
};
One thing which stands out from the original question:
str is a char* that points to a dynamic array of characters.
Because it is a dynamic array, you need to determine the required length at runtime, then allocate the array using the new [] operator. And release it when finished with delete [].
Note that the strCount invokes the Rule of Three too.
There is a way to avoid calling delete [] in your code and that is delegating that task to a smart pointer type that does that reliably on your behalf. However, copy constructor and copy assignment still require attention.
Alas, no smart pointers for you this time. Your homework is explicit: