In Java you can refer to the current object by doing: this.x = x
. How do you do this in C++?
Assume that each of these code examples are part of a class called Shape
.
Java:
public void setX(int x) { this.x = x; }
C++:
public: void setX(int x) { //? }
A later lecture will introduce classes in C++. The C++ equivalent of the Java import statement is the #include statement.
The C++ equivalent is this ; that is, the keyword is the same. Do not forget the -> thats probably what the OP is missing.
There is nothing equivalent to classes . Its a totally different paradigm. You can use structures in C. Have to code accordingly to make structures do the job.
Java does not have pointers. It has references almost exactly as C++ has.
Same word: this
Only difference is it is a pointer, so you need to use the ->
operator:
void setX(int x) { this->x = x; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With