Electron microscopy
 
CLASS in DM and C++
- Practical Electron Microscopy and Database -
- An Online Book -
Microanalysis | EM Book                                                                                   http://www.globalsino.com/EM/        


=================================================================================

 

CLASS is the foundation of C++'s support for object-oriented programming, and is at the core of many of its advanced features. The class is C++'s basic unit of encapsulation and provides the mechanism by which objects are created. A class defines a new data type, which specifies the form of an object. A class actually includes both data and the code that will operate on that data. Therefore, a class links data with code. A class specification is used to construct objects. In other words, objects are instances of a class, and thus a class is essentially a set of plans that specify how to build an object. When a class is defined, you declare the data that it contains and the code that operates on that data. Even though very simple classes might contain only code or only data, most real-world classes contain both. Within a class, the data is contained in variables and code is contained in functions. Collectively, the functions and variables, which constitute a class, are called members of the class. Therefore, a variable declared within a class is called a member variable, and then a function declared within a class is called a member function.

In C++, a class can contain private as well as public members. By default, all items defined in a class are private. However, in DM, a class can only contain private members. The general form of a class declaration in C++ is,
           class class-name {
           data and functions
           }
           object-list
where,           
           class-name: the name of the class. This name becomes a new type name, which can be used to create objects of the class. The objects of the class can be created by specifying them immediately after the class declaration in object-list, but this is optional. Once a class has been declared, then objects can be created where needed.

A class is created by using the keyword class, for instance, shown in Table 1110a. Note the general form of class declaration in DM, shown in Table 1110a, is very different from the one for C++ mentioned above. If the script is run from a script window, and the script declares a class, then that class will only be usable during execution of that script. The class remains available as long as any object created from that class exists, or until the class is explicitly unloaded. In general, if a class is needed outside of the script, then the class should be installed as libraries by "Installing a script library".

Furthermore, Passing-Objects-to-Functions can be used in CLASS. In this case, an object can be passed to a function in the same way as any other data type, namely a copy of the object, not the actual object itself, is passed to the function. This copy becomes the parameter in the function, so that a new object comes into existence. When the function terminates, the copy of the argument as well as the parameter is destroyed. An example of Passing-Objects-to-Functions can been seen in Table 1110a.

Table 1110a. Examples of CLASS in DM.
Script
Note
CLASS in DM
Script file

The output of the script is:

To define an object ("Object" after ":" in Line 6), you have to declare a class, GlobalSino, here.

The class has three member methods: InitObject() in Line 9, GetIWant() in Line 16, and PrintOutputFirst() in Line 21. Each method must have "object MySelf" as the first argument. All objects are stored in the variables of type 'object'.

When another method of the class is referred from within the class, e.g. the PrintOutputFirst() method uses the GetIWant() method in Line 23, you have to use the Myself.GetIWant(), the method name, to construct.

Line 26 is used to allocate the "new" object GlobalSino (before the "=" sign) to the class named as "GlobalSino" in Line 6. This takes one argument: the class name you created, followed by the InitObject() function of the GlobalSino class so that you can initialized the member variables.

All the script needs to contain both the class definition (e.g. starting Line 6) and the two lines in Lines 26 and 27.

Class demo
Script file  

The output of the script is:

In the " " in Line 31 has to have something in order to avoid error message in DM

Examples of CLASS in DM
Script file  
The output of the script is:
Examples of CLASS in DM
Script file  

To define an object you have to declare a class (see Lines 9, 11 and 21).

The Popup Window when the script is executed:
popup window class

The outup is:


Script file  

The outup is:

Passing-Objects-to-Functions is used here. The object EMObjectH in Lines 22 and 23 is passed to the function out_i() in Line 16 by out_i(100) in Line 23, while the object EMObject in Lines 25 and 26 is passed to the function set_i() in Line 10.

The value 100 is printed out before the value 30 is printed out since Lines 22 and 23 are listed before Lines 25 and 26, even though the function set_i() in Line 10 is declared before the function out_i() in Line 16.


Script file

◆ This is an example of exception handling.

◆ The output of this script is the same as that of the script above since the "test" IF statement in Line 18 is not true based on the input from Line 34.

◆ The output of the script is:

 

Table 1110b presents some common mistakes in scripts so that the execution of the scripts are failed.

Table 1110b. Failed examples of CLASS in DM.
Script
Note
Failed examples of CLASS in DM
Script file

Output:

Since the value of x is not declared in the script, the default value of x is then zero. Therefore, the value of i in Line 14 is "0" as Output of the script due to the equation in Line 13, instead of the set value "10" for i in Line 19.

Therefore, this script failed passing the value setting, "10", for Object EMObject, in Line 19, to Function set_i() in Line 11.

Failed examples of CLASS in DM
Script file

The type definition in Line 9 cannot be considered as a global variable and used by the function out_i() in Line 11. Therefore, an error message is shown at the script execution:

 

 

 

 

=================================================================================