Classes and objects
In the goal to do programs approaching the reality, the concept of objects has been added to programming languages, in the form of classes describing attributes of objects of the real world. A class is a structure which has variables (named in this case attributes) and functions (named methods of the class). Once a class is defined, it becomes a kind of complex type and an instance of the class may be declared as a variable. Such instances are what is called "objects". Meanwhile Scriptol programmers are not encouraged to become object-maniacs, and to describe anything as an object, it is a good practice to assemble all variables and functions related to a process in a class, since they may be used even without instance of the class, as we will see further in the "Static" section. By convention, a class name is capitalized.
Défining a class
The description of a class begins with the "class" keyword and ends with
"/class". The attributes (variables) must be stored before functions. A class
opens a space of visibility. Global variables and user-defined functions are
not visible inside a class. The attributes and methods of the class or of
an instance of another class are visible directly inside all methods of the
class.
Example of class, with these attributes: - speed, - power, and methods to access them. |
class
Car int speed int power int getSpeed() return speed int getPower() return power void setSpeed(int s) speed = s return /class |
Exercises |
1) An architect wants manage an allotment, with number
of rooms, area, and presence of of a garden, for each house. Describe
the house class with methods to read and modify attributes. Answer |