Electron microscopy
 
Comparison (Difference) between DM and C++
- Practical Electron Microscopy and Database -
- An Online Book -
Microanalysis | EM Book                                                                                   http://www.globalsino.com/EM/        


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

 

Although all DM scripts are written based on C++ language with many simplifications, there are still some major differences between them, probably due to some simplifications of the software interface behind the scripts. DigitalMicrographâ„¢ (DM) compiler does not fully comply with Standard C++. Therefore, in many cases, direct "translation" from C++ to DM scripts would not work. Due to the limitation of DM, DM cannot be a replacement of C++ compilers. Table 1137a gives some of such examples.

Table 1137a. Mismatches between working DM scripts and direct "translation" from C++. The directly translated scripts cannot be executed in DM.
Working DM scripts
In C++ or direct "translation" from C++
Mismatches
Does not use headers C++ uses headers, e.g. <cstdlib> header  
Can start a newline with or without a semicolon ";" Has to have a semicolon ";" to start a newline  

void MyFunction( number x )
{

number EMSum=0; // initialize sum
number EMi, EMCount
for(EMi=1; EMi<=x; EMi++)

{
EMSum = EMSum + EMi;
for(EMCount=0; EMCount<10; EMCount++)
result ("The current sum is " + EMSum + "\n")
}

}

number EMa = 3
MyFunction( EMa )
Script code

void MyFunction( number x )
{

number EMSum=0; // initialize sum
number EMi, EMCount
for(EMi=1; EMi<=x; EMi++)

{
EMSum = EMSum + EMi;
for(EMCount=0; EMCount<10; EMCount++)
result ("The current sum is " + EMSum + "\n")
}

}

number x = 3
MyFunction( x )

x, EMa

number x
number y

for(x=0; x<=10; ++x)
result (x + "\n")
{
for(y=10; y<8; --y)
result (y + "\n")
}

number x
number y

for(x=0, y=10; x<=10; ++x, --y)

result (x + "\n")
result (y + "\n")

 
number x
x = 1
for(x; x < 10; x++)
result (x + "\n")
Script code
number x
x = 1
for(; x < 10; x++)
result (x + "\n")
In C++, the initialization section can be moved outside of the loop, and the initialization section has been left blank.
return return 0 DM ignores "0" when it does not return a value
Automatically force an expression to be of a specific type Needs casts to force an expression to be of a specific type  

TagGroup GlobalSino, EMItems

GlobalSino = DLGCreateDialog( "Ask for a help?", EMItems )

TagGroup EMVote1 = DLGCreateCheckBox( "First help ", 0)
TagGroup EMVote2 = DLGCreateCheckBox( "Second help", 0)
TagGroup EMVote3 = DLGCreateCheckBox( "Third help ", 0)

EMItems.DLGAddElement( EMVote1 )
EMItems.DLGAddElement( EMVote2 )
EMItems.DLGAddElement( EMVote3 )

if ( !Alloc( UIframe ).Init( GlobalSino ).Pose() ) exit (0)

if ( 1 == EMVote1.DLGGetValue())
{
Result("Help on FOR in DM\n")
}

if ( 1 == EMVote2.DLGGetValue() )
{
Result("Help on IF in DM\n")
}

if ( 1 == EMVote3.DLGGetValue() )
{
Result("Help on SWITCH in DM\n")
}

Result( "\n\n" )
switch statement in DM
Script code

Number a;
Result ("Ask for a help?\n";
Result("10. Help on FOR in DM\n")
Result("20. Help on IF in DM\n")
Result("30. Help on SWITCH in DM\n")
... "Enter choice (10, 20, or 30): ";
switch (a)
{
case 1:
Result ("for is versatile");
break;
case 2:
Result ("IF is conditional");
break;
case 3:
Result ("switch is multiway");
break;
default:
result ("You must enter a number from 10, 20, and 30.\n");
}

Here presents DM functions similar to that of switch statement in C++.

In DM, there is no switch statement.

For instance, the working DM script shown here is a replacement of switch statement in C++. This script creates a simple "help" system that describes the meaning of the for, if, and switch statements. It first displays the help topics and then waits for the user to enter a choice.

number EMi

for(EMi=0; EMi<4; EMi++)
{

if ( 1 == EMi)
{
Result("Help on FOR in DM\n")
}

if ( 2 == EMi)
{
Result("Help on IF in DM\n")
}

if ( 3 == EMi)
{
Result("Help on SWITCH in DM\n")
}

Result( "\n\n" )

}
Script code

 

 

Table 1137b. C++ functions, in its library, which are missing on DM.
C++
Examples of alternative solution in DM
Function
Note
Function
Note
rand() Computes a random value in the range [0,1) random() Computes a random value in the range [0,1)
kbhit( ) This function returns false if no key has been pressed or true if a key has been struck. The kbhit( ) function is not defined by Standard C++, but is a common extension that is provided by most compilers. !SpaceDown() Stop the process if space-bar on the keyboard is pressed. Example
goto statement No such statements in DM
typedef statement

do {
statements;
} while(expression)

DM does not support do-while loop.
Constructors and destructors can take both 'self' object and variables as arguments Constructors and destructors can only take the 'self' object as its argument
struct

DM does not have this keyword which tells the compiler that a structure declaration, e.g. structure array, is beginning

strcpy( )
DM does not have such String Library Function
strcat( )
strlen( )
strcmp( )
pointer: type *var-name (with a "*" sign) DM does not support such pointer variables
atof( ) DM does not have this function
malloc( )
free( )
'\n' (newline character) "\n"
argv parameter DM does not have this pointer
argc parameter DM does not have this parameter
reverse( ) DM does not have this function
labs( ) DM does not have this function, which C++ returns the absolute
value of a long
fabs( ) DM does not have this function, which C++ returns the absolute
value of a double
nothrow DM does not have nothrow alternative
reference DM does not have such implicit pointers
const DM does not have this qualifier
swap( ) DM does not have this function, which is related to pointers
factr( ) DM does not have this function -- it is a classic example of recursion. However, this function can be built in DM (page1125)
Exception handling: try, catch and throw The applications of "exception handling: try, catch and throw" in DM are narrower than those in C++. For instance, catch cannot be used to catch Class Types in DM.
Operator overloading DM does not allow operator overloading
Enumerations/enum Not in DM
auto
extern
register variables
static variables
mutable
DM does not have such Class Specifiers
independent reference No "independent reference" in DM
Class inheritance No class inheritance in DM
Virtual functions DM does not support virtual functions
template function DM does not allow users to use "template" as a keyword to create their own functions
template class DM does not allow users to use "template" as a keyword to create their own classes
Parameterized constructor DM does not allow parameterized constructors

& can be associated with the type name rather (e.g. int& x) or the variable name (e.g. int &x) when a reference rarameter is declared

& can only be associated with the variable name when a reference rarameter is declared (page1122)
:: DM does not have such scope resolution operator
>> (shift right) DM does not have the Shift Operators
<< (shift left )
sizeof compile-time operator

There are 63 keywords currently defined for Standard C++ and C++ is a case-sensitive language, for instance, "return" is not the same as "RETURN". You cannot use any of the C++ keywords, in Table 1137c, as identifier names. On the other hand, you should not use the name of any standard function, such as abs, for an identifier.

Table 1137c. C++ and DM keywords. Part one lists the common keywords used in both C++ and DM, while Part two lists the keywords used in C++ only.

Part one: used in both C++ and DM

for if while else
return continue void break
Exception handling
try catch throw  
Compound data types
class      
 
Part two: used in C++ only
Compound data types
array struct (for structure) union  
Functions
friend      
       
public inline this template
new delete    
auto enum operator switch
asm using virtual mutable
bool explicit private true
double export protected volatile
case extern signed typedef
dynamic_cast false register typeid
char float reinterpret_cast typename
do namespace sizeof int
const default short unsigned
const_cast goto long wchar_t
static static_cast    

 

Table 1137d. Comparison of stream in DM and C++.
 
Function C++ DM
Availability to programers
Predefined streams
cin The stream is associated with standard input Yes No
cerr

The stream is associated with standard output. The difference between these two streams is that clog is buffered, but cerr is not. Therefore, any output sent to cerr is immediately output, but output to clog is written only when a buffer is full. cerr and clog are typically streams to which program debugging or error information is written.

clog
Wide (16-bit) character versions of standard streams
wcin These streams exist to support all languages, such as Chinese, that require large character sets. Yes No
wcout
wcerr
wclog
Template class hierarchies
basic_ios (e.g. ios_base)

This is the most commonly used class hierarchy. This is a high-level I/O class that provides formatting, error checking, and status information related to stream I/O. The ios_base, defines several non-template traits used by basic_ios. basic_ios is used as a base for several derived classes, including basic_istream, basic_ostream, and basic_iostream. These classes are used to create streams capable of input, output, and input/output, respectively.

Yes No
basic_streambuf

This class is derived from the low-level I/O class, and supplies the basic, low-level input and output operations, and provides the underlying support for the entire C++ I/O system. Unless you are doing advanced I/O programming,
this class is not directly needed.

Yes No

 

 

 

 

 

 

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