1class Rectangle
2{
3 int width, height;
4public:
5 void set_values (int,int);
6 int area() {return width*height;}
7};
8
9void Rectangle::set_values (int x, int y)
10{
11 width = x;
12 height = y;
13}
1class Rectangle {
2 int width, height;
3 public:
4 void set_values (int,int);
5 int area (void);
6} rect;
1/*
2 Keyword "this"
3You can use keyword "this" to refer to this instance inside a class definition.
4
5One of the main usage of keyword this is to resolve ambiguity between the names of
6data member and function parameter. For example:
7*/
8class Circle {
9private:
10 double radius; // Member variable called "radius"
11 ......
12public:
13 void setRadius(double radius) { // Function's argument also called "radius"
14 this->radius = radius;
15 // "this.radius" refers to this instance's member variable
16 // "radius" resolved to the function's argument.
17 }
18 ......
19}