• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

bc9Basic Loves C++

Started by James C. Fuller, March 08, 2016, 12:18:28 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

James C. Fuller

bc9Basic Loves c++
One of my primary interests with bc9Basic is to increase my knowledge of c++.
To that end I have added or tweaked a number of items to help produce modern c++ code.
In the help file, with the permission of the original author Eric Brasseurthere, is an abridged C++ Tutorial for bc9Basic/BCX Users: "The C++ tutorial for C users"
I will start with these entries and then add my own additions.

The three c++ compilers that I use and test.
Visual Studio 2015 Commnity: https://www.visualstudio.com/en-us/products/visual-studio-community-vs.aspx
TDM-GCC                    : http://tdm-gcc.tdragon.net/
Nuwen                      : http://nuwen.net/mingw.html

The comments "'Translate with ..." in the examples refer to the pulldown options in the bc9Adp2 Ide.
Using the included unicode lexer app, ULEX, the demos can be translated to source for compiling as a Unicode application.

The $NOMAIN directive tells the translator we will be providing our own main() function. This is my preferred method.

There are a number of ways to dimension variables that you may notice in the demos. While the translator supports them, I do not use suffixes.
Local may be substituted for Dim
Dim As Integer i
Dim i As Integer
Dim As Integer i,j,k
These all initialize the variable(s) to {0} in the c++ source: int i = {0};

If you substitute Raw for Dim there is no initialization you get: int i;
You can also assign a value in the Dim/Local/Raw:
Dim As Integer i = 7 -> int i = 7;

See the help file for more information on DIM

James

Edit: added zip of examples

James C. Fuller

#1

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 3. Console input and output streams
'Input from the keyboard and output to the screen can be performed
'through cin >> and cout <<.
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'==============================================================================
Function main()
    Dim As int a
    Dim As char s[100]
   
    cout << "This is a sample program." << endl
   
    cout << endl    ' Just a line feed (end of line)
   
    cout << "Type your age : "
    cin >> a
   
    cout << "Type your name: "
    cin >> s
   
    cout << endl
   
    cout << "Hello " << s << " you're " << a << " years old." << endl
    cout << endl << endl << "Bye!" << endl

    Pause
    Function = EXIT_SUCCESS
End Function
'==================================================================================


This is a sample program.

Type your age : 12
Type your name: BCX

Hello BCX you're 12 years old.


Bye!



James C. Fuller

#2

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 7. Global variables can be accessed even if a local variables have the same name
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*

$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
''For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"


Dim As double a = 128

Function main ()
    Raw As double a = 256

    cout << "Local a:  " << a   << endl
    cout << "Global a: " << ::a << endl
    Pause
End Function


Result:
Local a:  256
Global a: 128

James C. Fuller

#3

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 8. It is possible to declare a REFERENCE to another variable
'Example #1
'bc9Basic has the key word Ref to be used the same as Ptr
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
''For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'==============================================================================
Function main ()
   Dim As double a = 3.1415927

   Dim As double Ref b = a          ' b is a

   b = 89

   cout << "a contains: " << a << endl   ' Displays 89
   Pause
   Function = EXIT_SUCCESS
End Function


Result:
a contains: 89

James C. Fuller

#4

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 8. It is possible to declare a REFERENCE to another variable
'Example #2
'bc9Basic has the key word Ref to be used the same way as Ptr
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'==============================================================================
Sub change (r As double Ref, s As double)
   r = 100
   s = 200
End Sub
'==============================================================================
Function main ()
   Dim As double k, m

   k = 3
   m = 4

   change (k, m)

   cout << k << ", " << m << endl        ' Displays 100, 4
   
   Pause
   Function = EXIT_SUCCESS
End Function


Result:
100, 4

James C. Fuller

#5

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 8. It is possible to declare a REFERENCE to another variable
'Example #3
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'==============================================================================
Function biggest (r As double &, s As double &) As double &
   If r > s Then
       Function = r
   End If
   Function = s
End Function
'==============================================================================
Function main () As int
   Dim As double k = 3
   Dim As double m = 7

   cout << "k: " << k << endl                ' Displays  3
   cout << "m: " << m << endl                ' Displays  7
   cout << endl

   biggest (k, m) = 10

   cout << "k: " << k << endl                ' Displays  3
   cout << "m: " << m << endl                ' Displays 10
    cout << endl

   biggest (k, m) ++

   cout << "k: " << k << endl                ' Displays  3
   cout << "m: " << m << endl                ' Displays 11
   cout << endl
   Pause
   Function = EXIT_SUCCESS
End Function


Result:
k: 3
m: 7

k: 3
m: 10

k: 3
m: 11

James C. Fuller


'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 9. Namespaces can be declared
'The variables declared within a namespace can be used thanks to the :: operator
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'==============================================================================
Namespace first
    Raw As int a,b
End Namespace
'==============================================================================
Namespace second
    Raw As double a,b
End Namespace
'==============================================================================
Function main ()
   
    first::a = 2
    first::b = 5
   
    second::a = 6.453
    second::b = 4.1e4
   
    cout << first::a + second::a << endl 'result -> 8.453
    cout << first::b + second::b << endl 'result -> 41005
    Pause
    Function = EXIT_SUCCESS
End Function



Result:

8.453
41005

James C. Fuller


'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 10. A function can be declared inline
'If a function contains just simple lines of code, doesn't use for loops or the
'like, it can be declared inline. This means its code will be inserted everywhere
'the function is used. That's somewhat like a macro. The main advantage is the
'program will be faster. A small drawback is it will be bigger, because the full
'code of the function was inserted everywhere it is used.
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'==============================================================================
inline FUNCTION hypothenuse (a AS double, b AS double) AS double
    FUNCTION = sqrt (a * a + b * b)
END FUNCTION
'==============================================================================
FUNCTION main ()

    Dim As double k = 6, m = 9

    ' Next two lines produce exactly the same code:

    cout << hypothenuse (k, m) << endl        'result -> 10.8167
    cout << sqrt (k * k + m * m) << endl    'result -> 10.8167
    Pause
    Function = EXIT_SUCCESS
End Function


Result:

10.8167
10.8167

James C. Fuller


'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 11. The exception structure has been added
'------------------------------------------------------------------------------
' You know the classical control structures of C: *for*, *if*, *do*,
' *while*, *switch*... C++ adds one more control structure named EXCEPTION:
'------------------------------------------------------------------------------
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'==============================================================================
Function main () As int
    Dim As int  a,b, c

    cout << "Type a number: "
    cin >> a
    cout << endl
   
    Try
        If a > 100 Then
            Throw 100
        End If
       
        If a < 10 Then
            Throw 10
        End If
       
        c = a/3
        Throw  c
    Catch (result As int)
        cout << "Result is: " << result << endl
        b = result + 1
    End Try
   
    cout << "b contains: " << b << endl
   
    cout << endl
    Pause
   
    ' another example of exception use:
   
    Raw As char zero[]     = "zero"
    Raw As char even[]     = "even"
    Raw As char notprime[] = "not prime"
    Raw As char prime[]    = "prime"
   
    Try
        If a = 0 Then
            Throw zero
        End If
       
        If (a / 2) * 2 = a Then
             Throw even
        End If
       
        xFor int i = 3 While i <= sqrt ((float)a) By i++
            cout << "Testing " << i << endl
            If (a / i) * i = a Then
                Throw notprime
            End If
         xNext
         Throw prime
    Catch (conclusion AS const char Ptr)
          cout << "The number you typed is "<< conclusion << endl
    End Try
   
    cout << endl
    Pause
    Function = EXIT_SUCCESS
End Function


Result:
Type a number: 5

Result is: 10
b contains: 11

The number you typed is prime

James C. Fuller


'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 12. A function can have default parameters
'It is possible to define default parameters for functions.
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"

'==============================================================================
Function test (a As double, b = 7 As double) As double
   Function = a - b
End Function
'==============================================================================
Function main ()
    cout << test (14, 5) << endl   ' Displays 14 - 5 -> 9
    cout << test (14) << endl      ' Displays 14 - 7 -> 7
    Pause
End Function



Result:
9
7

James C. Fuller


'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 13. FUNCTION OVERLOAD: several functions can be declared with the
' same name provided there is a difference in their parameter list.
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'==============================================================================
Function test (a As double, b As double) As double
    Function = a + b
END FUNCTION
'==============================================================================
Function test (a As int, b As int) As int
    Function = a - b
End Function
'==============================================================================
Function main ()
    Dim As double   m = 7,  n = 4
    Dim As int      k = 5,  p = 3

    cout << test(m, n) << " , " << test(k, p) << endl
    'Result -> 11 , 2
    Pause
End Function


Result:
11, 2

James C. Fuller


'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 14. The symbolic operators (+ - * / ...) can be defined for new data types
'OPERATOR OVERLOADING can be used to redefine the basic symbolic operators for new kinds of parameters
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'==============================================================================
Type bcx_vector
   Dim As double x
   Dim As  double y
End Type

Function operator*(a As double, b As bcx_vector) As bcx_vector
    Dim As bcx_vector r

    r.x = a * b.x
    r.y = a * b.y

    Function = r
End Function
'==============================================================================
Function main ()
    Dim As bcx_vector k, m      ' No need to type "struct bcx_vector"

    k.x =  2                ' To be able to write
    k.y = -1                ' k = bcx_vector (2, -1)

    m = 3.1415927 * k       ' Magic!

    cout << "(" << m.x << ", " << m.y << ")" << endl
   
    'Result -> (6.28319, -3.14159)

    Pause
    Function = EXIT_SUCCESS

End Function

Result:

(6.28319, -3.14159)

James C. Fuller


'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 15. Different functions for different data types will automatically be
'generated provided you define a template function.
'Tired of defining the same function five times? One definition for *int* type parameters,
'one definition for *double* type parameters, one definition for *float* type parameters...
'Didn't you forget one type? What if a new data type is used? No problem: the C++ compiler
'can automatically generate every version of the function that is necessary!
'Just tell it how the function looks like by declaring a *template* function
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'==============================================================================
Template <class ttype>
    Function minimum (a As ttype, b As ttype) As ttype
        Raw As ttype r
   
        r = a
        If b < a Then
            r = b
        End If
   
        Function = r
    End Function
End Template

Function main()
   
    Raw As int i1, i2, i3
    i1 = 34
    i2 = 6
    i3 = minimum (i1, i2)
    cout << "Most little: " << i3 << endl

    Raw As  double d1, d2, d3
    d1 = 7.9
    d2 = 32.1
    d3 = minimum (d1, d2)
    cout << "Most little: " << d3 << endl   
    cout << "Most little: " << minimum (d3, 3.5) << endl
   
    Pause
    Function = EXIT_SUCCESS
End Function

Result
Most little: 6
Most little: 7.9
Most little: 3.5

James C. Fuller


'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 16. The keywords new and delete are much better to allocate and deallocate memory
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'==============================================================================
Function main ()
    Raw As double Ptr d   ' d is a variable whose purpose
                         ' is to contain the address of a
                         ' zone where a double is located
    d = new double       ' new allocates a zone of memory
                         ' large enough to contain a double
                         ' and returns its address.
                         ' That address is stored in d.
    *d = 45.3            ' The number 45.3 is stored
                         ' inside the memory zone
                         ' whose address is given by d.
   
    cout << "Type a number: "
    cin >> *d
    *d = *d + 5
    cout << "Result: " << *d << endl
   
    delete d             ' delete deallocates the
                         ' zone of memory whose address
                         ' is given by pointer d.
                         ' Now we can no more use that zone.
   
   
    d = new double[15]   ' allocates a zone for an array
                         ' of 15 doubles. Note each 15
                         ' double will be constructed.
                         ' This is pointless here but it
                         ' is vital when using a data type
                         ' that needs its constructor be
                         ' used for each instance.
   
    d[0] = 4456
    d[1] = d[0] + 567
   
    cout << "Content of d[1]: " << d[1] << endl
   
    delete [] d          ' delete [] will deallocate the
                         ' memory zone. Note each 15
                         ' double will be destructed.
                         ' This is pointless here but it
                         ' is vital when using a data type
                         ' that needs its destructor be
                         ' used for each instance (the ~
                         ' method). Using delete without
                         ' the [] would deallocate the
                         ' memory zone without destructing
                         ' each of the 15 instances. That
                         ' would cause memory leakage.
   
    Raw As int n = 30
    d = new double[n]    ' new can be used to allocate an
                         ' array of random size.
    xFor int i = 0 While i < n By i++
       d[i] = i
    XNext
    delete [] d
   
    Dim As char Ptr s
    s = new char[100]
    s$ = "Hello!"
    cout << s << endl
    delete [] s
    Pause
End Function

Result:
Type a number: 6
Result: 11
Content of d[1]: 5023
Hello!

James C. Fuller


'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 17. You can add METHODS To a class or struct.
'In standard C a struct contains only data. In C++ a struct definition can also
'include functions. Those functions are owned by the struct and are meant to
'operate on the data of the struct. Those functions are called METHODS. The example
'below defines the method surface() on the struct bcx_vector.
'a bc8Basic PPTYPE is a standard c++ struct
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'==============================================================================
PPType bcx_vector
    Dim As double x
    Dim As double y

    Function surface() As double
         Raw As double s
         s = x * y
         If s < 0 Then
             s = -s
         End If
         Function = s
    End Function
End PPType
'==============================================================================
Function main ()
    Raw As bcx_vector a
   
    a.x = 3
    a.y = 4
   
    cout << "The surface of a: " << a.surface() << endl

    Pause
    Function = EXIT_SUCCESS
End Function


Result:
The surface of a: 12