The General form of a class
A class is declared through use of the class keyword. The classes which have been used up to this point are in fact very limited instances of its complete form. Classes can get much more complex. The common form of a class definition is displays here:
class classname {
type instance-variable1;
type instance-variable2;
//...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
//body of method
}
//...
type methodnameN(parameter-list) {
//Body of method
}
}
The data, or variables, defined inside a class are instance variables. The code is hold inside methods. Collectively, the methods and variables described inside a class are known as members of the class. Within most classes, the instance variables are acted upon and accessed through the methods defined for that particular class. Within most classes, the object variables are acted upon and accessed through the methods defined for that class. Therefore, it is the methods which determine how a class' data could be used.
Variables defined inside a class object variables since each instance of the class holds its own copy of these variables. Therefore, the data for one object is separate and unique from the data for another. Shortly we will come back to this point, but it is an extremely important concept to learn early.
All methods have the similar general form as main (), that we have been using therefore far. However, most methods will not be specified as public or static.
A simple class
Let's starts our study of the class along with a simple instance. Here is a class called box which defines three instance variables: width, height, and depth. Presently, Box does not holds any methods
Class box {
double width, height, depth;
}