Theo's Forum

IT-Consultant: James C. Fuller => bc9Basic => Topic started by: James C. Fuller on March 08, 2016, 12:18:28 PM

Title: bc9Basic Loves C++
Post by: James C. Fuller on March 08, 2016, 12:18:28 PM
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
Title: Re: bc9Basic Loves C++ Chapter 3
Post by: James C. Fuller on March 08, 2016, 01:17:29 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'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!


Title: Re: bc9Basic Loves C++ Chapter 7
Post by: James C. Fuller on March 08, 2016, 01:47:25 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'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
Title: Re: bc9Basic Loves C++ Chapter 8 Example #1
Post by: James C. Fuller on March 08, 2016, 01:55:11 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'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
Title: Re: bc9Basic Loves C++ Chapter 8 Example #2
Post by: James C. Fuller on March 08, 2016, 01:59:44 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'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
Title: Re: bc9Basic Loves C++ Chapter 8 Example #3
Post by: James C. Fuller on March 08, 2016, 02:03:32 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'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
Title: Re: bc9Basic Loves C++ Chapter 9
Post by: James C. Fuller on March 08, 2016, 02:19:10 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'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
Title: Re: bc9Basic Loves C++ Chapter 10
Post by: James C. Fuller on March 08, 2016, 02:59:52 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'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
Title: Re: bc9Basic Loves C++ Chapter 11
Post by: James C. Fuller on March 08, 2016, 03:12:21 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'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
Title: Re: bc9Basic Loves C++ Chapter 12
Post by: James C. Fuller on March 08, 2016, 03:17:19 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'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
Title: Re: bc9Basic Loves C++ Chapter 13
Post by: James C. Fuller on March 08, 2016, 03:22:41 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'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
Title: Re: bc9Basic Loves C++ Chapter 14
Post by: James C. Fuller on March 08, 2016, 03:36:16 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'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)
Title: Re: bc9Basic Loves C++ Chapter 15
Post by: James C. Fuller on March 08, 2016, 03:43:56 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'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
Title: Re: bc9Basic Loves C++ Chapter 16
Post by: James C. Fuller on March 08, 2016, 03:53:16 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'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!
Title: Re: bc9Basic Loves C++ Chapter 17
Post by: James C. Fuller on March 08, 2016, 04:04:34 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'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
Title: Re: bc9Basic Loves C++ Chapter 18
Post by: James C. Fuller on March 08, 2016, 04:17:58 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 18. The CONSTRUCTOR and the DESTRUCTOR can be used to initialise and
'destroy an instance of a class
'Very special and essential methods are the CONSTRUCTOR and DESTRUCTOR. They are
'automatically called whenever an instance of a class is created or destroyed
'(variable declaration, end of program, *new*, *delete*...).
'The constructor will initialize the variables of the instance, do some calculations,
'allocate some memory for the instance, output some text... whatever is needed.
'Here is an example of a class definition with two overloaded constructors
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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"
'==============================================================================
Class x_vector
    public:
        Raw As double x,y
     
        Constructor x_vector()   ' same name as class
               x = 0
               y = 0
        End Constructor
 
        Constructor x_vector(a AS double, b AS double)
               x = a
               y = b
        End Constructor
End Class
'==============================================================================
Function main ()
    Raw As x_vector k         ' x_vector () is called
   
    cout << "x_vector k: " << k.x << ", " << k.y << endl << endl
   
    Raw As x_vector m (45, 2) ' x_vector (double, double) is called
   
    cout << "x_vector m: " << m.x << ", " << m.y << endl << endl
     
    k = x_vector (23, 2)      ' x_vector created, copied to k, then erased
   
    cout << "x_vector k: " << k.x << ", " << k.y << endl << endl
    Pause   
    Function = EXIT_SUCCESS
End Function


Result:
vector k: 0, 0
vector m: 45, 2
vector k: 23, 2
Title: Re: bc9Basic Loves C++ Chapter 19
Post by: James C. Fuller on March 08, 2016, 05:07:21 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 19. Complex classes need the COPY CONSTRUCTOR and an overload of the = operator
'Note the tilde "~" for the DESTRUCTOR
'If you cast an object like a bcx_vector, everything will happen correctly.
'For example, if bcx_vector *k* contains *(4, 7)*, after the cast *m = k* the
'bcx_vector *m* will contain *(4, 7)* too. The values of k.x and k.y have simply
'been copied to m.x and m.y.
'Now suppose you're playing with objects like the person class below. Those objects
'contain a pointer to a character string. If you cast the person object by writing
'*p = r* it is necesary that some function does the work to make *p* be a correct
'copy of *r*. Otherwise, p.name will point to the same physical character string as
'r.name. What's more, the former character string pointed to by p.name is lost and
'becomes a memory zombie. The result will be catastrophic: a mess of pointers and
'lost data. The methods that will do the job are the COPY CONSTRUCTOR and an overload
' of 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"
'==============================================================================

Class person
    public:
        Raw As char Ptr name
        Raw As int age
 
        Constructor person (n = "no name" AS const char Ptr, a = 0 As int)
            name = new char[100]
            name$ = n$
            age = a
        End Constructor
       
        Constructor person (s As const person &) ' The COPY CONSTRUCTOR
            name = new char[100]
            name$ = s.name$
            age = s.age
        End Constructor
       
        Function operator= (s As const person &) As person&  ' overload of =
            name$ = s.name$
            age = s.age
            Function = *this
        End Function
       
        Destructor ~person ()
            delete [] name
        End Destructor
End Class
'==============================================================================
Sub modify_person (h As person&)
    h.age += 7
End Sub
 '==============================================================================
Function compute_person (h As person) As person
    h.age += 7
    Function = h
End Function
'==============================================================================
Function main () As int
    Raw As person p
   
    cout << p.name << ", age " << p.age << endl << endl
   
    ' output: no name, age 0
   
    Raw As person k ("John", 56)
    cout << k.name << ", age " << k.age << endl << endl
    ' output: John, age 56
   
    p = k
    cout << p.name << ", age " << p.age << endl << endl
    ' output: John, age 56
   
    p = person ("Bob", 10)
    cout << p.name << ", age " << p.age << endl << endl
    ' output: Bob, age 10
   
    ' Neither the copy constructor nor the overload
    ' of = are needed for this operation that modifies
    ' p since just the reference towards p is passed to
    ' the function modify_person: 
    modify_person (p)
    cout << p.name << ", age " << p.age << endl << endl
    ' output: Bob, age 17
   
    ' The copy constructor is called to pass a complete
    ' copy of p to the function compute_person. The
    ' function uses that copy to make its computations
    ' then a copy of that modified copy is made to
    ' return the result. Finaly the overload of = is
    ' called to paste that second copy inside k:
    k = compute_person (p)
    cout << p.name << ", age " << p.age << endl << endl
    ' output: Bob, age 17
    cout << k.name << ", age " << k.age << endl << endl
    ' output: Bob, age 24
    Pause
    Function = EXIT_SUCCESS
End Function


Result:
no name, age 0

John, age 56

John, age 56

Bob, age 10

Bob, age 17

Bob, age 17

Bob, age 24
Title: Re: bc9Basic Loves C++ Chapter 20
Post by: James C. Fuller on March 08, 2016, 05:12:46 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 20. The method bodies can be defined below the class definition
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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"
'==============================================================================
Class x_vector
    public:
        Raw As double x,y
        Dim Function surface() as double
End Class
'------------------------------------------------------------------------------
Function x_vector::surface() AS double
    Raw As double s = 0

    xFor double i = 0 While i < x By i++
        s = s + y
    xNext

    Function = s
End Function
'==============================================================================
Function main () As int
    Raw As x_vector k

    k.x = 4
    k.y = 5

    cout << "Surface: " << k.surface() << endl
    Pause

End Function



Result:
Surface: 20
Title: Re: bc9Basic Loves C++ Chapter 21
Post by: James C. Fuller on March 08, 2016, 05:17:50 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 21. The keyword this is a pointer to the instance a method is acting upon
'When a method is applied to an instance, that method may use the instance's
'variables, modify them... But sometimes it is necessary to know the address of
'the instance. No problem, the keyword *this* is intended for that purpose.
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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"
'==============================================================================
Class x_vector
    Public:
        Raw As double x,y

        Constructor x_vector (a = 0 As double, b = 0 As double)
            x = a
            y = b
        End Constructor

        Function module() AS double
            Function = sqrt (x * x + y * y)
        End Function

        Sub set_length (a = 1 AS double)
            Raw As double length
            length = this->module()
            x = x / length * a
            y = y / length * a
        End Sub
End Class
'==============================================================================
Function main () As int
    Raw As x_vector c (3, 5)

    cout << "The module of x_vector c: " << c.module() << endl

    c.set_length(2)    ' Transforms c in a x_vector of size 2

    cout << "The module of x_vector c: " << c.module() << endl

    c.set_length()      ' Transforms b in an unitary x_vector.

    cout << "The module of x_vector c: " << c.module() << endl
    Pause
    Function = EXIT_SUCCESS
End Function


Result:
The module of vector c: 5.83095
The module of vector c: 2
The module of vector c: 1
Title: Re: bc9Basic Loves C++ Chapter 22
Post by: James C. Fuller on March 08, 2016, 05:23:08 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 22. Arrays of instances can be declared
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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"
'==============================================================================
Class x_vector
    public:
        Raw As double x,y

        Constructor x_vector (a = 0 AS double, b = 0 AS double)
            x = a
            y = b
        End Constructor   

        Function module () AS double
            Function = sqrt (x * x + y * y)
        End Function
End Class
'==============================================================================
Function main () As int
   
    Raw As x_vector s[1000]
    Raw As x_vector t[3] = {x_vector(4, 5), x_vector(5, 5), x_vector(2, 4)}

    s[23] = t[2]

    cout << t[0].module() << endl
   
    Pause
    Function = EXIT_SUCCESS
End Function


Result:
6.40312
Title: Re: bc9Basic Loves C++ Chapter 23
Post by: James C. Fuller on March 08, 2016, 05:36:31 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 23. An example of a complete class declaration
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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"
'==============================================================================
Class x_vector
    public:
        Raw As double x,y
        Dim Constructor x_vector (a = 0 As double, b = 0 As double)
        Dim Constructor x_vector ()

        Dim Function operator + (a As x_vector) As x_vector
        Dim Function operator - (a As x_vector) As x_vector
        Dim Function operator - () As x_vector
        Dim Function operator * (a As double) As x_vector
        Dim Function module() As double
        Dim SUB set_length (A  As double = 1)
End Class
'------------------------------------------------------------------------------
Constructor x_vector::x_vector (a As double, b As double)
    x = a
    y = b
End Constructor
'------------------------------------------------------------------------------
Constructor x_vector::x_vector ()
    x = 0
    y = 0
End Constructor
'------------------------------------------------------------------------------
Function x_vector::operator + (a As x_vector) As x_vector
    Function = x_vector (x + a.x, y + a.y)
End Function
'------------------------------------------------------------------------------
Function x_vector::operator - (a As x_vector) As x_vector
    Function = x_vector (x - a.x, y - a.y)
End Function
'------------------------------------------------------------------------------
Function x_vector::operator - () As x_vector
    Function = x_vector (-x, -y)
End Function
'------------------------------------------------------------------------------
Function x_vector::operator * (a As double) As x_vector
    Function = x_vector (x * a, y * a)
End Function
'------------------------------------------------------------------------------
Function x_vector::module() As double
    Function = sqrt (x * x + y * y)
End Function
'------------------------------------------------------------------------------
Sub x_vector::set_length (a As double)
    Raw As double length = this->module()

    x = x / length * a
    y = y / length * a
End Sub
'==============================================================================
Function operator << (o As ostream&, a As x_vector) As ostream&
    o << "(" << a.x << ", " << a.y << ")";
    Function = o
End Function
'==============================================================================
Function main ()
    Raw As x_vector a,b,c (3, 5)

    a = c * 3
    a = b + c
    c = b - c + a + (b - a) * 7
    c = -c

    cout << "The module of x_vector c: " << c.module() << endl

    cout << "The content of x_vector a: " << a << endl
    cout << "The oposite of x_vector a: " << -a << endl

    c.set_length(2)         ' Transforms c in a x_vector of size 2.

    a = x_vector (56, -3)
    b = x_vector (7, c.y)

    b.set_length()          ' Transforms b in an unitary x_vector.

    cout << "The content of x_vector b: " << b << endl

    Dim As double k

    k = x_vector(1, 1).module()   ' k will contain 1.4142.
    cout << "k contains: " << k << endl

    Pause
    Function = EXIT_SUCCESS
End Function


Result:
The module of bcx_vector c: 40.8167
The content of bcx_vector a: (3, 5)
The opposite of bcx_vector a: (-3, -5)
The content of bcx_vector b: (0.971275, 0.23796)
k contains: 1.41421
Title: Re: bc9Basic Loves C++ Chapter 24
Post by: James C. Fuller on March 08, 2016, 05:42:43 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 24. static variables inside a class definition
'One or more variables in a class can be declared *static*. In which case,
'only one instance of those variables exist, shared by all instances of the class.
'It must be initialised outside the class declaration.
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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"
'==============================================================================
Class x_vector
    public:
        Raw As double x,y
        static As int count

        Constructor x_vector (a = 0 AS double, b = 0 AS double)
            x = a
            y = b
            count++
        End Constructor

        Destructor ~x_vector()
            count--
        End Destructor
End Class
'==============================================================================
Raw As int x_vector::count = 0
'==============================================================================

Function main () As int
    cout << "Number of x_vectors:" << endl
   
    Raw As x_vector a
    cout << x_vector::count << endl
   
    Raw As x_vector b
    cout << x_vector::count  << endl
   
    Raw As x_vector PTR r, u
   
    r = new x_vector
    cout << x_vector::count << endl
   
    u = new x_vector
    cout << a.count << endl
   
    delete r
    cout << x_vector::count << endl
   
    delete u
    cout << b.count << endl
    Pause
    Function = EXIT_SUCCESS
End Function


Result:
1
2
3
4
3
2
Title: Re: bc9Basic Loves C++
Post by: James C. Fuller on March 08, 2016, 05:54:04 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 25. const variables inside a class definition
'this is not allowed with floats,doubles in vc++  but ok with g++
' const is not allowed with c++(11) you need constexpr (now supported by bc9)
'example modified for c++11 using a double
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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"
'==============================================================================
Class x_vector
    public:
        Raw As double x,y
        constexpr static double pi = 3.1415927

        Constructor x_vector (a = 0 AS double, b = 0 AS double)
            x = a
            y = b
        End Constructor

        Function cilinder_volume () As double
            Function = x * x / 4 * pi * y
        End Function
End Class
'==============================================================================
Function main() As int
   
    cout << "The value of pi: " << x_vector::pi << endl << endl

    x_vector k (3, 4)

    cout << "Result: " << k.cilinder_volume() << endl

    Pause
    Function = EXIT_SUCCESS
   
End Function

Result:
The value of pi: 3.14159

Result: 28.2743
Title: Re: bc9Basic Loves C++ Chapter 26
Post by: James C. Fuller on March 08, 2016, 05:59:03 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 26. A class can be DERIVED from another class.
'A class can be DERIVED from another class. The new class INHERITS the variables
'and methods of the BASE CLASS. Additional variables and/or methods can be added.
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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"
'==============================================================================

Class x_vector
    public:
        Raw As double x,y

        Constructor x_vector (a = 0 As double, b = 0 As double)
            x = a
            y = b
        End Constructor

        Function module() AS double
            Function = sqrt (x*x + y*y)
        End Function

        Function surface() AS double
            Function = x * y
        End Function
End Class
'==============================================================================
Class trivector Using public x_vector    ' trivector is derived from x_vector
    public:
        Raw As double z       ' added to x and y from x_vector

        Constructor trivector (m=0 As double, n=0 As double, p=0 As double) Using x_vector (m, n)
            z = p           ' x_vector constructor will
        End Constructor     ' be called before trivector
                            ' constructor, with parameters
                             ' m and n.

        Constructor trivector (a As x_vector) ' What to do if a x_vector is cast to a trivector
            x = a.x
            y = a.y
            z = 0
        End Constructor

        Function module () As double      ' define module() for trivector
            Function = sqrt (x*x + y*y + z*z)
        End Function

        Function volume () As double
            Function = this->surface() * z    ' or x * y * z
        End Function
End Class
'==============================================================================
Function main () As int
    Raw As x_vector a (4, 5)
    Raw As trivector b (1, 2, 3)

    cout << "a (4, 5)    b (1, 2, 3)    *r = b" << endl << endl
   
    cout << "Surface of a: " << a.surface() << endl
    cout << "Volume of b: " << b.volume() << endl
    cout << "Surface of base of b: " << b.surface() << endl
   
    cout << "Module of a: " << a.module() << endl
    cout << "Module of b: " << b.module() << endl
    cout << "Module of base of b: " << b.x_vector::module() << endl
   
    Raw As trivector k
    k = a                          'thanksto trivector(x_vector) definition
                             ' copy of x and y,       k.z = 0
    Raw As  x_vector j
    j = b                          'copyof x and y.       b.z leaved out
   
    Raw As x_vector Ptr r
    r = &b
   
    cout << "Surface of r: " << r->surface() << endl
    cout << "Module of r: " << r->module() << endl
   
    Pause
    Function = EXIT_SUCCESS
End Function


Result:
a (4, 5)    b (1, 2, 3)    *r = b

Surface of a: 20
Volume of b: 6
Surface of base of b: 2
Module of a: 6.40312
Module of b: 3.74166
Module of base of b: 2.23607
Surface of r: 2
Module of r: 2.23607
Title: Re: bc9Basic Loves C++ Chapter 27
Post by: James C. Fuller on March 08, 2016, 06:08:57 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 27. If a method is declared virtual the program will always check the
'type of the instance that is pointed to and will use the appropriate method.
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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"
'==============================================================================
Class x_vector
    public:
        Raw As double x,y

        Constructor x_vector (a = 0 As double, b = 0 As double)
            x = a
            y = b
        End Constructor

        Function module() As virtual double
            Function = sqrt (x*x + y*y)
        End Function
End Class
'==============================================================================
Class trivector Using Public x_vector
    public:
        Raw As double z

        Constructor trivector (m = 0 As double, n = 0 As double, p = 0 As double)
            x = m                   ' Just for the game,
            y = n                   ' here I do not call the x_vector
            z = p                   ' constructor and I make the
        End Constructor             ' trivector constructor do the whole job. Same result.

        Function module () As double
            Function = sqrt (x*x + y*y + z*z)
        End Function
End Class
'==============================================================================
Sub test (k As x_vector &)
    cout << "Test result:          " << k.module() << endl
End Sub
'==============================================================================
Function main () As int
    Raw As x_vector a (4, 5)
    Raw As trivector b (1, 2, 3)

    cout << "a (4, 5)    b (1, 2, 3)" << endl << endl

    Raw As x_vector Ptr r

    r = &a
    cout << "module of x_vector a: " << r->module() << endl

    r = &b
    cout << "module of trivector b: " << r->module() << endl

    test (a)

    test (b)

    Raw As x_vector &s = b

    cout << "module of trivector b: " << s.module() << endl

    Pause
    Function = EXIT_SUCCESS
End Function


Result:
a (4, 5)    b (1, 2, 3)

module of vector a: 6.40312
module of trivector b: 3.74166
Test result:          6.40312
Test result:          3.74166
module of trivector b: 3.74166
Title: Re: bc9Basic Loves C++ Chapter 28
Post by: James C. Fuller on March 08, 2016, 06:12:58 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 28. A class can be derived from more than one base class.
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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"
'==============================================================================
Class x_vector
    public:
        Raw As double x,y

        Constructor x_vector (a = 0 As double, b = 0 As double)
            x = a
            y = b
        End Constructor

        Function surface() As double
            Function = fabs (x * y)
        End Function
End Class
'==============================================================================
Class number
    public:
        Raw As double z

        Constructor number(a As double)
            z = a
        End Constructor

        Function is_negative() As int
            If z < 0 Then
                Function = 1
            Else
                Function = 0
            End If
        End Function
End Class
'==============================================================================
Class trivector Using public x_vector, public number
    public:

        Constructor trivector(a=0 As double, b=0 As double, c=0 As double) Using x_vector(a,b), number(c)
        End Constructor    ' The trivector constructor calls the x_vector
                           ' constructor, then the number constructor,
                           ' and in this example does nothing more.

        Function volume() As double
            Function = fabs(x * y * z)
        End Function
End Class
'==============================================================================
Function main() As int
    Raw As trivector a(2, 3, -4)

    cout << a.volume() << endl
    cout << a.surface() << endl
    cout << a.is_negative() << endl
    Pause
    Function = EXIT_SUCCESS
End Function

Result:
24
6
1
Title: Re: bc9Basic Loves C++ Chapter 29
Post by: James C. Fuller on March 08, 2016, 06:20:04 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 29. Class derivation allows you to write generic methods
'Class derivation allows you to construct more complex classes built from base classes.
'There is another application of class derivation: allowing the programmer to write
'generic functions.
'Suppose you define a base class with no variables. It makes no sense to use instances
'of that class inside your program. But then you write a function whose purpose
'it is to sort instances of that class. That function will be able to sort any type
'of object provided it belongs to a class derived from that base class! The only
'condition is that inside of each derived class definition, all methods that the
'sort function needs are correctly defined.
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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"
'==============================================================================
Class octopus
    public:
 
        IMPFUNCTION module() = 0 AS virtual double
                                   ' = 0 implies function is not
                                   ' defined. This makes instances
                                   ' of this CLASS cannot be declared.
End Class
'==============================================================================
Function biggest_module (a As octopus &, b  As octopus &, c  As octopus &) As double
    Raw As double r = a.module()
    If b.module() > r Then
        r = b.module()
    End If
    If c.module() > r Then
        r = c.module()       
   End If
   Function = r
End Function
'==============================================================================
Class x_vector Using public octopus
    public:
        Raw As double x,y

        Constructor x_vector (a = 0 As double, b = 0 As double)
            x = a
            y = b
        End Constructor

        Function module() As double
            Function = sqrt (x * x + y * y)
        End Function
End Class
'==============================================================================
Class number Using public octopus
    public:
        Raw As double n

        Constructor number (a = 0 As double)
            n = a
        End Constructor

        Function module() As double
            IF n >= 0 THEN
                 Function = n
            End If
            Function = -n
        End Function
End Class
'==============================================================================
Function main () As int
    Raw As x_vector k (1,2), m (6,7), n (100, 0)
    Raw As number p (5),   q (-3),  r (-150)
   
    cout << biggest_module (k, m, n) << endl
    cout << biggest_module (p, q, r) << endl
   
    cout << biggest_module (p, q, n) << endl
    Pause
    Function = EXIT_SUCCESS
End Function


Result:
100
150
100
Title: Re: bc9Basic Loves C++ Chapter 30.1
Post by: James C. Fuller on March 08, 2016, 06:38:02 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 30_1. ENCAPSULATION: public, protected and private
'The *PUBLIC:* directive means the variables or the methods below can be accessed
'and used everywhere in the program.
'If you want the variables and methods to be accessible only to methods of the class
'AND to methods of derived classes, then you must put the keyword *protected:* before them.
'If you want variables or methods to be accessible ONLY to methods of the class,
'then you must put the keyword *private:* before them.
'The fact that variables or methods are declared private or protected means that
'nothing external to the class can access or use them. That's ENCAPSULATION.
'(If you want to give a specific function the right to access those variables and methods,
'then you must include that function's prototype inside the class definition, preceded by
'the keyword *friend*.)
'Good practice is to encapsulate all the variables of a class. This can sound strange
'if you're used to structs in C. Indeed a struct only makes sense if you can access its data...
'In C++ you have to create methods to access the data inside a class. The example
'below uses the basic example of chapter 17, yet declares the class data to be protected.
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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"
'==============================================================================
Class x_vector
    Protected:
        Raw As double x,y
    Public:
'------------------------------------------------------------------------------   
        Sub set_x (n As int)
            x = n
        End Sub
'------------------------------------------------------------------------------
        Sub set_y (n As int)
            y = n
        End Sub
'------------------------------------------------------------------------------       
        Function surface () As double
            Raw As double s
            s = x * y
            If s < 0 Then
                s = -s
            End If
            Function = s
        End Function
'------------------------------------------------------------------------------
End Class   
'==============================================================================   
Function main () As int
    Raw As x_vector a

    a.set_x (3)
    a.set_y (4)

    cout << "The surface of a: " << a.surface() << endl
    Pause
    Function = EXIT_SUCCESS
End Function

Result:
The surface of a: 12
Title: Re: bc9Basic Loves C++ Chapter 30.2
Post by: James C. Fuller on March 08, 2016, 06:42:52 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 30_2. ENCAPSULATION: public, protected and private
'The example C30_1 is a bit odd since the class data x and y can be set but they
'cannot be read back. Any attempt in function main () to read a.x or a.y will result
'in a compilation error. In this example, x and y can be read back.
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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"
'==============================================================================
Class x_vector
    protected:
         Raw As double x,y
    public:
'------------------------------------------------------------------------------   
        Sub set_x (n As int)
            x = n
        End Sub
'------------------------------------------------------------------------------
        Sub set_y (n As int)
            y = n
        End Sub
'------------------------------------------------------------------------------       
        Function get_x () As double
            Function = x
        End Function
'------------------------------------------------------------------------------       
        Function get_y () As double
            Function = y
        End Function
'------------------------------------------------------------------------------       
        Function surface () As double
            Raw As double s
            s = x * y
            If s < 0 Then
                s = -s
            End If
            Function = s
        End Function
'------------------------------------------------------------------------------       
End Class
'==============================================================================
Function main () As int
    Raw As x_vector a

    a.set_x (3)
    a.set_y (4)
   
    cout << "The surface of a: " << a.surface() << endl
    cout << "The width of a:   " << a.get_x() << endl
    cout << "The height of a:  " << a.get_y() << endl
    Pause
    Function = EXIT_SUCCESS
End Function
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*

Result:
The surface of a: 12
The width of a:   3
The height of a:  4
Title: Re: bc9Basic Loves C++ Chapter 31.1
Post by: James C. Fuller on March 08, 2016, 06:50:37 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 31_01. Brief examples of file I/O
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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
    Raw As fstream f
 
    f.open("test.txt", ios::out)
    f << "This is a text output to a file." << endl
   
    Raw As double a = 345
   
    f  << "A number: " << a << endl
    f.close()
    cout << "test.txt created" << endl
    Pause
    Function = EXIT_SUCCESS
End Function
 

Result
test.txt created

The content of file test.txt should be:
This is a text output to a file.
A number: 345
Title: Re: bc9Basic Loves C++ Chapter 31.2
Post by: James C. Fuller on March 08, 2016, 06:53:53 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 31_02. Brief examples of file I/O
'Example #2
'Before you use Example C31_02 be sure to create the test.txt file with Example C31_01
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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
    Raw As fstream f
    Raw As char c
 
    cout << "What's inside the test.txt file" << endl
       cout << endl
 
       f.open("test.txt", ios::in)
 
    while Not f.eof()
        f.get(c)                    ' Or c = f.get()
        cout << c
    Wend
   
    f.close()
    Pause
    Function = EXIT_SUCCESS
End Function
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*


Result:
What's inside the test.txt file

This is a text output to a file.
A number: 345
Title: Re: bc9Basic Loves C++ Chapter 32.1
Post by: James C. Fuller on March 08, 2016, 06:59:08 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 32. Character arrays can be used like files
'Generally speaking, it is possible to do on character arrays the same operations
'as on files. This is very useful to convert data or manage memory arrays.
'Here is a program that writes inside a character array.
'Example #1
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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"
'==============================================================================
'******************************************************************************
' g++ whines about depreciated header file but this example needs it.
' It is NOT a default #include
'******************************************************************************
#include <strstream>
'------------------------------------------------------------------------------
Function main () As int
    Raw As char a[1024]
    Raw As ostrstream b(a, 1024)
 
    b.seekp(0)                            ' Start from first char.
    b << "2 + 2 = " << 2 + 2 << ends      ' ( ends, not endl )
                                         ' ends is simply the
                                         ' null character   '\0'
    cout << a << endl
   
    Raw As double v = 2
   
    a$ = "A sinus: "
   
    b.seekp(Len (a))
    b << "sin (" << v << ") = " << sin(v) << ends
   
    cout << a << endl
    Pause
End Function

Result:
2 + 2 = 4
A sinus: sin (2) = 0.909297
Title: Re: bc9Basic Loves C++ Chapter 32.2
Post by: James C. Fuller on March 08, 2016, 07:12:17 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 32. Character arrays can be used like files
'Example #2 A program that reads from a character string
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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"

'------------------------------------------------------------------------------
'g++ whines about depreciated but this example needs it. it is NOT a default #include
#include <strstream>
'------------------------------------------------------------------------------
Function main () As int
    Raw As char a[1024]
    Raw As istrstream b(a, 1024)

    a$ = "45.656"

    Raw As double k, p

    b.seekg(0)                           ' Start from first character.
    b >> k

    k = k + 1

    cout << k << endl

    a$ = "444.23 56.89"

    b.seekg(0)
    b >> k >> p

    cout << k << ", " << p + 1 << endl
    Pause
    Function = EXIT_SUCCESS
End Function


Result:
46.656
444.23, 57.89
Title: Re: bc9Basic Loves C++ Chapter 33
Post by: James C. Fuller on March 08, 2016, 07:15:47 PM

'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 33. An example of formatted output
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$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
    Raw As int i
 
    cout << "A list of numbers:" << endl
    xFor i = 1 While i <= 1024 By i *= 2
        cout.width (7)
        cout << i << endl
    xNext
   
    cout << "A table of numbers:" << endl
    xFor i = 0 While i <= 4 By i++
        cout << setw(3) << i << setw(5) << i * i * i << endl
    xNext
   
    Pause
    Function = EXIT_SUCCESS
End Function

Result:
A list of numbers:
      1
      2
      4
      8
     16
     32
     64
    128
    256
    512
   1024
A table of numbers:
  0    0
  1    1
  2    8
  3   27
  4   64