class definition beginning and end, and "this"

I'm trying to understand classes in C++ and this example is stumping me. I don't understand where the class definition ends at. I thought it was at the first }; but I'm finding one more after the OneMoreCheeseGone function.

I'm not sure if the int variable CheeseCount is included in the class. I'm not sure if the OneMoreCheeseGone function is part of the class, or if it is a standard function that can be called from anywhere.

I'm really stumped on exactly how the "this" pointer variable works and I'm getting a little hung up on the confusion in the class definition too.

#include <iostream>

using namespace std;

class Cheese
{
public:
    string status;
    void eat();
    void rot();
};

int CheeseCount;

void OneMoreCheeseGone(Cheese *Block)
{
    CheeseCount--;
    Block->status = "Gone";
};

void Cheese::eat()
{
    cout << "Eaten up! Yummy" << endl;
    OneMoreCheeseGone(this);
}

void Cheese::rot()
{
    cout << "Rotted away! Yuck" << endl;
    OneMoreCheeseGone(this);
}

int main()
{
    Cheese *asiago = new Cheese();
    Cheese *limburger = new Cheese();

    CheeseCount = 2;

    asiago->eat();
    limburger->rot();

    cout << endl;
    cout << "Cheese count: " << CheeseCount << endl;
    cout << "asiago: " << asiago->status << endl;
    cout << "limburger: " << limburger->status << endl;

    return 0;
}

This code comes from C++ for Dummies. The code does run, and I copied the code from the examples provided with the book, not something I typed myself, so it's not my typos.

I'm going to go look at some other examples on the internet to try to understand "this".
Thanks for any help!

this is an implicit parameter to every class member function, and points to the current instance of the class.

TheMemberFormerlyKnownAsAWOL:
this is an implicit parameter to every class member function, and points to the current instance of the class.

that answer is just as hard to understand as all the other definitions I have been looking at! I'm starting to understand "this", just a little bit.

But, again, I am puzzled by "void OneMoreCheeseGone(Cheese *Block)" function. Does it belong to the class, or is it a standard function? It closes the function with the extra semicolon:

void OneMoreCheeseGone(Cheese *Block)
{
CheeseCount--;
Block->status = "Gone";
};//this puzzles me, why is this semicolon here?

It clearly doesn't belong to the class, because you can't see it in the class definition.

The semicolon doesn't need to be there.
Looks like a typo.

Thanks, I removed it and code still worked. I thought that I had tried that before. I guess I did not.
That part is making a lot more sense now. I'm still getting hung up on "this", but I will continue to dig into it. I'm just getting started, and at this point many of the terms mean so little to me, it takes time for "objects" and "classes", and other terms to have meaning. I'll get there.

"this" is a pointer to the current instantiation of the class. Such a pointer is needed since classes can have several, independent instances while sharing the same logic. If you're familiar with Python, "this" == "self".

I'll take all the explanations I can get! Each time I get a little more out of it. I understand the instances of the class. I'm not understanding how the variable in the class gets targeted. I don't have time to explain further, I'm out of time. Right now I am reading a different tutorial covering classes. A different instructor explains things differently which can help. Thanks!

amdkt7:
I'm not understanding how the variable in the class gets targeted.

Although it isn't obvious, the compiler determines the "targeting" of the "this" pointer automatically.

Unless you want to learn about what's under the hood of a C++ compiler, it's easiest to just imagine your compiler figures out how to use "this" with magic :slight_smile:

A quick code illustration might help.

This code:

Button button;
button.press();

Is conceptually equivalent to this:

Button button;
press(&button);

(The latter example is more like how you'd do it in C which doesn't have classes.)

The compiler passes a pointer to the object as a "hidden" argument to the function, and the function accesses that hidden argument by the name this. With button.press(), this (inside the press() function) is a pointer to button.

It's starting to seep into my brain. I even thought about it during the middle of the night. I'm about to take a new look at it now.

This code example helps me to understand a lot better.

#include <iostream>
using namespace std;

class Demo {
private:
  int num;
  char ch;
public:
  void setMyValues(int input, char chInput){
    this->num = input;
    this->ch= chInput;
  }
  void displayMyValues(){
    cout<<num<<endl;
    cout<<ch<<endl;
  }
};

int main(){
  Demo amdkt7;
  Demo dude;
  amdkt7.setMyValues(100, 'A');
  amdkt7.displayMyValues();

  dude.setMyValues(99, 'b');
  dude.displayMyValues();
  return 0;
}

I modified the original example which used num and ch twice, which confused me:

#include <iostream>
using namespace std;
class Demo {
private:
  int num;
  char ch;
public:
  void setMyValues(int num, char ch){
    this->num =num;
    this->ch=ch;
  }
  void displayMyValues(){
    cout<<num<<endl;
    cout<<ch;
  }
};
int main(){
  Demo obj;
  obj.setMyValues(100, 'A');
  obj.displayMyValues();
  return 0;

I also expanded to include a second instance of Demo that helps me to visualize what is being demonstrated.

Is it just something I am overlooking, or is this part of the code also wrong?

int main()
{
Cheese *asiago = new Cheese();
Cheese *limburger = new Cheese();

This is a snippet from the first example I posted at the beginning.
The weird thing to me is the opening and closing () at the end of the statements creating new instances of Cheese. I tried removing the () pair and the code still works fine.

Is this a typo or is this a programming technique that the author is not explaining? I'm going with typo.

Take a close look at the constructor.

TheMemberFormerlyKnownAsAWOL:
Take a close look at the constructor.

You have lost me. What section of code is that? Where the class Cheese is defined?

Power_Broker:
Constructors in C++ - GeeksforGeeks

I have been looking at that page, I found it myself. It is not answering the question. The closest thing I see on that page is the section about Parameterized Constructor, but this example code is not using a parameterized constructor. Here is the code example again:

#include <iostream>
using namespace std;

class Cheese
{
public:
    string status;
    void eat();
    void rot();
};

int CheeseCount;

void OneMoreCheeseGone(Cheese *Block)
{
    CheeseCount--;
    Block->status = "Gone";
}

void Cheese::eat()
{
    cout <<"Eaten up!" << endl;
    OneMoreCheeseGone(this);
}

void Cheese::rot()
{
    cout <<"Rotted away!" << endl;
    OneMoreCheeseGone(this);
}


int main()
{

    Cheese *cheddar = new Cheese();//this is the lines that are confusing me
    Cheese *swiss =   new Cheese();//why is the () on the end? Code works the same with or without them.

    CheeseCount = 2;

    cheddar->eat();
    swiss->rot();

    cout << endl;
    cout <<"Cheese count: " << CheeseCount     << endl;
    cout <<"cheddar: "      << cheddar->status << endl;
    cout <<"swiss: "       << swiss->status   << endl;

    return 0;
}

I'm trying to understand this. I am using many resources including the forum. I am new at this and would like better hints, or better yet an answer to the question "Is this a typo, or does it make sense?"

Thanks

You don't have a constructor explicitly defined, therefore the class uses the (hidden) default constructor with no parameters. Things might make more sense if you look at how a servo is instantiated via Servo.h. That's a good starting point.

this pointer demo

// this pointer compare
//  https://www.tutorialspoint.com/cplusplus/cpp_this_pointer.htm

class Box {
  public:
    // Constructor definition
    Box(double l = 2.0, double b = 2.0, double h = 2.0) {
      Serial.println("Constructor called.");
      length = l;
      breadth = b;
      height = h;
    }
    double Volume() {
      return length * breadth * height;
    }
    int compare(Box box) {
      double sum1 = this->Volume();
      double sum2 = box.Volume();
      return this->Volume() > box.Volume();
    }

  private:
    double length;     // Length of a box
    double breadth;    // Breadth of a box
    double height;     // Height of a box
};

void setup() {
  Serial.begin(115200);
  Box Box1(3.3, 1.2, 1.5);    // Declare box1
  Serial.print("Box 1 vol = ");
  Serial.println(Box1.Volume());
  Box Box2(8.5, 6.0, 2.0);    // Declare box2
  Serial.print("Box 2 vol = ");
  Serial.println(Box2.Volume());

  Serial.print("\nBox1.compare(Box2) - ");
  if (Box1.compare(Box2)) {
    Serial.println("Box2 is smaller than Box1");
  } else {
    Serial.println("Box2 is equal to or larger than Box1");
  }

  Serial.print("\nBox2.compare(Box1) - ");
  if (Box2.compare(Box1)) {
    Serial.println("Box1 is smaller than Box2");
  } else {
    Serial.println("Box1 is equal to or larger than Box2");
  }
}

void loop() {

}

Power_Broker:
You don't have a constructor explicitly defined, therefore the class uses the (hidden) default constructor with no parameters. Things might make more sense if you look at how a servo is instantiated via Servo.h. That's a good starting point.

Thanks. It was interesting to look at the servo.h file. It's not answering my question though. It's probably a good example to study and I will continue to look at it.

[quote author=dougp link=msg=4474157 date=1581472585]
[color=blue]this[/color] pointer demo

// this pointer compare
//  https://www.tutorialspoint.com/cplusplus/cpp_this_pointer.htm

Thanks dougp,
I bookmarked this site, and I think I will spend sometime reviewing the Class and Objects section to help round out my understanding.