EXP-6 This pointer using class
EXP-6 This pointer using class
#include<iostream>
class Coordinate {
private:
int x;
int y;
public:
Coordinate (int x, int y) {
// Using this pointer inside the constructor
// to set values in data members.
this->x = x;
this->y = y;
}
void printCoordinate() {
cout<<"(x, y) = ("<<this->x<<", "<<this->y<<")"<<endl;
}
};
int main () {
// Passing x and y coordinate in the constructor.
Coordinate pointA(2, 3), pointB(5, 6);
return 0;
}
Output
(x, y) = (2, 3)
(x, y) = (5, 6)