Setters and Getters in C++

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.



This is the code that I have so far,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#define MYSTRING_H
#ifndef MYSTRING_H

class MyString {

private:
  char* str[];
  int size;
  static strCount = 0;

public:
  void setStr(char * str) {
    str = s;
  }
  void getStr() {
    return str;
  }
  int setSize() {
    return size;
  }
  void getSize(int size) {
    size = sz;
  }
};


The error that it's currently producing is:
1
2
MyString.h:2:0: error: unterminated #ifndef
 #ifndef MYSTRING_H 


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.
You also need to swap the #define and #ifndef

1
2
3
4
5
6
7
8
#ifndef MYSTRING_H
#define MYSTRING_H

class MyString {
	...
};

#endif 


If MYSTRING_H has not been defined:
  define MYSTRING_H
  define the MyString class
the getter must have the same type as the variable it returns...

anyways try to use auto as function type I wonder what will happen (yes, I am using you as guinea pig)
What's the proper code for the auto function @Flaze07?
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
#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 {
    constexpr static auto myproperty = 1.3;
};

“myproperty” should be interpreted as double.
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 [].

See tutorial page:
http://www.cplusplus.com/doc/tutorial/dynamic/
Last edited on
The need to use delete [] leads to writing a user-defined destructor and ownership of that dynamic memory, which in turn calls for the Rule of Three:
http://en.cppreference.com/w/cpp/language/rule_of_three

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:
str is a char*
ah..
rather than
 
char* str[]

it is better to have
 
char* str


and calling the new in constructor and delete in destructor


edit : how about ask this question in SO (StackOverflow) I wonder what'll will happen to your question
Last edited on
Topic archived. No new replies allowed.