Header files, Classes and Constructors.

Hey guys, I'm matt, and these are 3 things I hate. More fool me for missing the lectures about them but I'm completely stuck.

Part of what I'm trying to do is create a Class in another .cpp file, and then have a header file connect that to my main.cpp, except it's really not as easy as I thought. Everything I do ends up making it worse, until it's a jumble of code I don't understand at all. Could someone possibly show me an example of a header file, which links a class to the main.cpp?

But in reality, that's the least of my worries. I have very little understanding of how getters and setters work - everything I've managed to find so far has just managed to confuse me further which isn't really helping at all :D

Essentially, if someone could explain to me how getters and setters work, and some basic example code of using them that'd be fantastic.. And then if you really had the time, if someone could tell me what the hell a constructor does, and how to utilise it, that'd be even more fantastic! This forum seems like a helpful place with loads of really talented people, so hopefully someone can ease my stress :)

Cheers in advance guys!

Mattx
A header file is just a helpful shortcut. When you #include a header file, all it does is cut and paste the contents of that header file into the place where you #include it.

So now that you know this, it becomes clear that a header file cannot possibly "connect" one cpp file to another. It's just part of the cpp file.

A class is just a kind of object you define yourself. You get some kinds of objects from the start; int, double, float, that sort of thing. A class is just you defining a new kind of object. Here's a new kind of object that contains an int and a float.

1
2
3
4
5
class someNewKindOfObject
{
  int a;
  float b;
}


A constructor is a function that belongs to a class and runs when you make one of those objects. It allows you to set things up how you want them. Here is a constructor for the class that sets the internal values.

1
2
3
4
5
someNewKindOfObject::someNewKindOfObject()
{
  a = 0;
  b  =4.7;
}



getters and setters are an obsessive way to change the internal variables. They're just class functions. You commonly see Java programmers obsessively writing them for every variable they can find, whether or not they're needed.

Is that all clear? We can go deeper.
Last edited on
I think you pretty much nailed it with the constructors and classes, so cheers very much for that :)

As for the getters and setters, I'm pretty much required to use them to pass my course this year so if we could go deeper that'd be great.

From what I understand, getters and setters just seem to be completely useless considering I can just input the values I need directly into the variables, which probably isn't helping my understanding. So I guess the question there is why are they actually used?

As for implementation, how are they used? I've looked at code with them in, I have a copy of C++ How to program 8th edition and I've been through the classes section on that 10 times at least, and all they do is still confuse me unfortunately.

Thanks for the speedy response and clear answers though, much appreciated!
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
26
27
28
class someNewKindOfObject
{
  int a;
  float b;
  void setTheVariables(int value1, float value2);
   int getTheInt();
  someNewKindOfObject();
};

// constructor
someNewKindOfObject::someNewKindOfObject()
{
  a = 0;
  b  =4.7;
}

// setter
void someNewKindOfObject::setTheVariables(int value1, float value2)
{
  a = value1;
  b = value2;
}

// getter
int someNewKindOfObject::getTheInt()
{
  return a;
}


So I guess the question there is why are they actually used?

In a class, you can make variable private or public. If they're private, you can't directly set them (and can't call private functions). If public, you can. So, if you make the variables private, the only way to change them is to have a public setter function. That's pretty much it.
Last edited on
Perfect, cheers :) I'm gonna shoot off and crack on with it and hopefully get somewhere. If not, I'll be back tomorrow with even sillier questions (probably). Once again, thanks very much for the help!
Topic archived. No new replies allowed.