• 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


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

James C. Fuller


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

James C. Fuller


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

James C. Fuller


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

James C. Fuller


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

James C. Fuller


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

James C. Fuller


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

James C. Fuller


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

James C. Fuller


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

James C. Fuller


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

James C. Fuller


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

James C. Fuller


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

James C. Fuller


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

James C. Fuller


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

James C. Fuller


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