Intermixing C++ With Pro-SiMPLE


SiMPLE lets you write programs and tasks that contain C++ statements intermixed with SiMPLE statements. When writing such a "hybrid" program, there are three principal rules you must remember:

In addition, if (for whatever reason) you wish to invoke SiMPLE tasks or functions in a C++ statement, there are two additional rules: As an example of using C++, let's write a Pro-SiMPLE program that draws two intersecting circles, and then invokes two functions ("setfillstyle" and "floodfill") in the C++ graphics library (see: Some Useful C++ Functions) to fill each of the resulting regions with a different color pattern:

            Pro-SiMPLE
            Common Int patrn[]={7, 2, 8}
            Common Int color[]={2, 4, 9}
            graphon; dx=25
           +Circle (320-dx_, 240, 50);
            circle (320+dx, 240, 50)
           +for (int i=-1; i<2; i++)
           +{
           +   setfillstyle (patrn_[i+1], color_[i+1]);
           +   floodfill (320+i*(dx_+49), 240, 15);
           +}
            tabxy (34, 11)
           +cout << "Filled Circles" << endl;

Notice the two different ways in which we chose to invoke SiMPLE's Circle task. In line 5 of the program we invoked it the usual way (by using a SiMPLE statement), while in line 4 we invoked it by using a C++ statement (just to be different, and hence the necessity of using an upper case "C" in the name "Circle").

When you run the program, the following image is generated:


Including User-defined C++ Functions

The previous discussion assumed that your hybrid program consisted of only a main program. However, there may be times when you also want to define your own C++ functions along with your main program. When doing so, there is one more additional rule you must remember:

The reason for this is because whenever the SiMPLE translator encounters a line beginning with a plus sign, it simply passes that line to the C++ compiler without trying to interpret the contents of that line. Therefore, unless you use an End statement to explicitly indicate the physical end of your main program's source listing, SiMPLE will think that the subsequent C++ statements that define your functions are all part of the main program!

As an example of how to include a user-defined C++ function, let's write a hybrid program that invokes a user-defined mytask function:

     Pro-SiMPLE
               //The first statement must NOT be C++.
     Int null  //Therefore this is just a do-nothing.

     +void mytask (Text);  //function declaration
     //Task main
       param = "Hello, World"
       +mytask (param_);
       Pause
       Cls; Quit
     End main  //MUST be included here!

     +void mytask (Text string)
     +{
     +  cout << string.get_str() << endl;
     +}

Notice that, even though we must use an End statement to indicate the end of the main program, we do not use a Task main statement to indicate the start of the program. (SiMPLE generates one for you automatically.)



  [ Webmaster | FAQ's | Home Page | Contact Us ]