Remember that the execution of any program begins with the main function. So while executing, the first statement of the main function will get executed first.

Rectangle rt; - This statement declares rt as an object of class Rectangle. It is the same as saying that 'rt' is a 'Rectangle'. For this, 'Rectangle' must be defined.

class Rectangle - We have defined our own class named 'Rectangle'. 'class' is a keyword which means that 'Rectangle' is a class.

Inside the Rectangle class, we declared two variables and three functions. These variables and functions belong to the class Rectangle since these are declared inside the class and thus are called members of the class. There are two types of members of a class:

data members - In the class Rectangle, length and breadth are the data members since these store the information ( length and breadth ) of the objects of the class.

member functions - setLength(), setBreadth and getArea() are the member functions of the Rectangle class.

Note that while defining the member functions, we have written Rectangle:: before the function name. This is to tell the compiler that the function belongs to the class Rectangle.

rt.setLength(7); - This statement will call the function setLength with the parameter value 7. To call any function, we use (.) DOT after the object and then call that function.
Since 'rt' is an object of the 'Rectangle' class, so 'rt.setLength' will call the function 'setLength' of 'Rectangle' class for 'rt'. This will set the value of length as 7 for 'rt'

Similarly, rt.setBreadth(4) will call the function setBreadth and will set the value of breadth as 4.

int area = rt.getArea(); - 'rt' will call the function getArea() which will return length * breadth which is 28 (since the value of length is 7 and that of braedth is 4). This value will get assigned to the variable area.

public: - We declared all the members of the class as public. public is a modifier which allows the members of a class to be accessed directly from outside the class.

Like public, there are other modifiers also like private and protected. We will study more about modifier later.

We can also define our member methods at the time it is declared in the function as in the following example.

Since we declared the variables length and breadth as public, we directly accessed these and assigned them values.

rt.length = 7; - We directly accessed the data member 'length' by the object of the Rectangle class. Similarly, we assigned breadth to the Rectangle object by directly accessing it.rt.getArea(); - We also accessed getArea() directly since it is also declared as public.

Thus we can access the members declared as public from anywhere.

Private

The member declared as private can only be accessed inside the class in which it is declared. Thus, the object of the class cannot directly access its members as we did in the case of public.

By default, all the members of a class are private.

If we try to access a private member of any class from outside that class, we will get a compile time error. Let's see an example.

In this example when we created the object rt of class Rectangle, the constructor Rectangle() automatically got called and initialized the data members for the object 'rt'. It initialized the length and breadth of rt to 10 each.

When the constructor was called, length and breadth were created and then in the body of the constructor, these member variables were assigned values.

We can also make a constructor with nothing in its body.
Rectangle(){ };

In this example, we have parameters in our constructor. As told earlier, a constructor is also a function which is executed at the time of creating an object and has the same name as that of its parent class. So, it will work like a function and assign values passed from Rectangle rt( 7, 4 ); to length and breadth.

Rectangle rt( 7, 4 );

It will create an object 'rt' of class 'Rectangle' and pass 7 to 'l' and 4 to 'b' ( 'l' and 'b' are used in the constructor of the class 'Rectangle' ).

Use of static

'static' is used to make access to any data variable or function without making an object of that class. It means that 'static' is used so that we can access any data variable or function without making an object of that class.