Questions Regarding Coding Conventions (and others)

Hello! I am attempting to clear up a few things. I come from the world of java so I need to confirm a few things.

The class definition is always put in a header file. But the corresponding functions are not put in that header file.
The functions are put in a cpp file of the EXACT same name.
The main function/file is called main.cpp and the header for the class and functions are #included?

The qualifier "private" means access is only permitted from public functions. It doesn't matter if the variable is declared in the same class or not.

LPCSTR is const char*. A pointer. It allocates one byte of storage memory on the heap. As a pointer, you can do this:
const char* ch = "COM1";
It won't give an error because the char refers to the amount of memory and C++ trusts you to be careful and assign only one char.
But I am mighty confused about one thing.
It is the type of one of the parameters of a function called CreateFile().
And as per multiple guides, the following is a valid value of that parameter:
"COM3".
That is 4 bytes. What is going on??
The qualifier "private" means access is only permitted from public functions. It doesn't matter if the variable is declared in the same class or not.


The private keyword means that those methods or members will not be inherited by children. It also means they cannot be accessed by any external objects. Loop up encapsulation as it's a fundamental OO principle across all OO languages.

By doing const char* ch = "COM1"; The compile will set up memory allocations for you so you do not need to worry about this. This is really only useful for hard-coded values or if you're converting from the std::string in to a const char* for methods that do not support a string parameter.

The ch will be addressing 4 bytes of memory (1 per char).
const char* ch = "COM1";
the string requires five characters, as there will be a null terminator '\0' after the last ordinary character.
Ah, I appreciate it a lot!
^^ It also allocates space for the pointer (4 or 8 bytes, depending on OS).

I unfortunately don't know how to do this in C++, but here is some C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdio>

char arr[]  = {'H','e','l','l','o','\0'};
// Memory, 6 bytes
// [ H ][ e ][ l ][ l ][ o ][ \0 ]

const char* str = "Hello";
// Memory 6 + 4 (or 8) bytes
// [ MemAddress of H ][ H ][ e ][ l ][ l ][ o ][ \0 ]

int main(void)
{
  printf("%-8s: %p %p\n", "Array",   &arr, &arr[0]);
  printf("%-8s: %p %p\n", "Pointer", &str, &str[0]);
  return 0;
}

...
Output

Array   : 00402000 00402000
Pointer : 00402008 00403064


Perhaps familiar, printf() is just like print() (or the other way around). '%p' says to print a memory address, which is exactly what is given with the '&' character.
Last edited on
Topic archived. No new replies allowed.