|
C++ has proven to be a robust language for object-oriented programming, much of its longevity owed to its stability on Win32 platforms and its maturity in producing comprehensive operating systems and end-user applications rich with features. However, developers have come to rely on drafting techniques to make memory use more efficient. While some programmable logic devices are NVRAM (e.g., they retain their memory after power less) memory allocation is still at a premium. This led to the necessity of EC++ being a true but condensed subset of the full C++ library (see part 1 of 2 in this series for details on what didn't make the cut). Let's look at one very simple example. Here's a simple output statement in a standard C++ program:
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int num1 = 6;
cout << num1 << endl;
return 0;
}
The first line is a preprocessor directive ordering inclusion of the input/output stream header. The second and third lines declare using the standard stream output (cout) and end line (endl) operators of the std namespace. The int main() is simply a call to the main function, indicative of beginning program execution, and the assignment of 6 to an integer variable. The cout is followed by the stream insertion operator "<<", a string statement in quotes, another stream insertion operator, and finally the end of the line. The return statement is simply to mark the end of program execution and indicate the program returns no values.
Now, let's look at an identical program as would be seen in EC++:
#include <stream.h>
int main()
{
int num1=6;
cout << num1 <<"\n";
}
Notice several things. No "using" statements are necessary, neither are we using the iostream header, instead utilizing the stream header as a matter of expediency. Also note the removal of the endl; in favor of the "\n", the newline string with an escape character (the backslash). The newline character is also available in standard C++ but should not necessarily be viewed as serving the same purpose in the case of EC++.
Another common technique involves operator overloading, which simply put is a way of declaring a same function name to execute the same ops on different data types multiple times. This allows developers to "destroy" temporary objects once they are processed and utilized rather than keeping them reserved in memory until the program fully executes. Here is a simple example:
#include <defs.h>
inline float max(float val1, float val2)
{
return val1 > val2 ? val1:val2;
}
inline double max(double val3, doubleval4)
{
return val3 < val4 ? val3:val4;
}
In this case, we have quickly utilized the inline max() functions of the definitions header (really just a "dummy" include file) to compare and return whether it is true that value one is greater than value two. In the first function the values are floats, but in the second notice they are now doubles. Same function, different data types. Neat! You have now seen just a couple of small ways EC++ is used in implementation and the differences to standard C++. For further reading, two texts are recommended: "Embedded Software: The Works" by Colin Walls, and an O'Reilly text that's considered a classic on the subject, "Programming Embedded Systems in C and C++" by Michael Barr. Stay tuned for Saturday's topic of interest!
Editor's Note: Click here for Part 1 of this series.
|