class's () placement

Which is correct, or considered the right way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class posters
{
   public:
           //accessors--------------
           int getPost();
           void setPost(int age);
           //end of accessors-------
           void post();
   private:
           int postCount;
};

void poster::post()
{
   std::cout << "User Posted";
}
//note that the post() is below the class declaration 






OR






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//note that post() is on top of class declartion
void poster::post()
{
   std::cout << "User Posted";
}



class posters
{
   public:
           //accessors--------------
           int getPost();
           void setPost(int age);
           //end of accessors-------
           void post();
   private:
           int postCount;
};
Last edited on
IMO neither one is actually correct. You should declare member functions in a serperate .cpp file from the Header that you class is defined in. You could argue that this would be the same as your first option so if I had to pick one that would be it.
if it makes a different where I said
void post()

I meant to say
void poster::post()
or is that still wrong?
This book by 'Sam' says that you should keep information private, but have it accessed by public 'in-class' functions.
Defining in a seperate file is more convenient, but not more correct.
@hanst99, I'm guessing that you are talking about ComputerGeek's post.

But which one of mine is correct, if either?
Your class name is posters, however the function void poster::post() should have been void posters::post() (or your class should have been named poster).

But even after fixing the typo the second form will not compile because the compiler needs to have seen the definition of the class before it will recognize the definition of the member function.
Thanks Alrededor.

Two more question:

According to everything I read, it's "Bad Programming" not make things constant. If you plan for data to be changed every now and then I'm guess that it is okay not to make things constant?


And


Can I make global classes in C++/CLI (window forms)?
so that if I wanted to make a function:
makeTextFile().

I would been able to?
If data is not going to be changed it is best to make it constant. If you declare a variable as constant and then later on in the code make a mistake and try to assign a new value to the variable the compiler will report the error of trying to modify a constant. On the other hand, if the variable not supposed to be changed, but you don't declare it as constant the compiler will not be able to catch your mistake of modifying it. It is far better to catch a mistake of this type at compile time rather than at runtime when the results are not as expected and you don't know why.

Data that will need to be modified later cannot be made constant.

Why is not being "constant correct" considered "Bad Programming"? Consider the following example involving passing a parameter to a function by reference or constant reference. Suppose we have a function that takes a reference to a string and does not modify the string. Should we pass it by reference or constant reference?

1
2
3
4
   void myFunction( std::string& s )
   {
      // function code here
   }


or

1
2
3
4
  void  myFunction( const std::string& s )
   {
      // function code here
   }


Note that the const keyword guarantees that the function will not modify the string s.

Now suppose we call the function with this code snippet.

1
2
   const std::string firstMonth = "January";
   myFunction( firstMonth );


The first form above (without the keyword const) will not compile because the compiler knows that myFunction could possibly modify the string firstMonth which is supposed to be constant. On the other hand the second form above (with the keyword const) is just fine. You are passing myFunction a string which is supposed to be constant and the function guarantees that it will not modify it.

Note that if firstMonth was declared without the keyword const then both forms above would work or if we had a string that was inherently not constant both would still work. For example:

1
2
3
4
   std::string s;
   std::cout << "Please enter your last name: ";
   std::cin >> name;
   myFunction( name );



When first exposed to the concept of constant correctness it may seem counter intuitive that for a function that does not modify its reference parameter making the parameter a const reference is better. Doesn't making it a constant make it more restrictive? The problem here is that there are two points of view.

From inside the function the const reference parameter is more restrictive, i.e., the function can't modify the parameter. But from outside the function, i.e., the code that calls the function, the const reference makes it less restrictive since it can be called with either a constant or a non-constant value.

Why not just never use the keyword const? Because you lose the ability of the compiler to flag those programming errors where you have inadvertently modified a variable that shouldn't change. Being "constant correct" makes the compiler your friend and as I said above you should greatly prefer compile time errors over runtime errors.

As to your second question:
Can I make global classes in C++/CLI (window forms)?

In C++ a class would be available for use in any file which includes the class header file. I am not very familiar with Microsoft's C++/CLI which is really a different language.

Thanks this has been very helpful
If I could +rep, I would!
Thanks!
Topic archived. No new replies allowed.