Using BaCon's new CLASS command (in the beta version)
Mar 19, 2019 17:36:38 GMT 1
Post by bigbass on Mar 19, 2019 17:36:38 GMT 1
We have a new command CLASS ENDCLASS
I will demo several ways to use this command
and even how to port a code example
from a custom class not related to any GUI toolkit
first some documentation with many details if you need it
if not you can skip it and focus on the code below
www.tutorialspoint.com/cplusplus/cpp_classes_objects.htm
if confronted with this example in C++
and you want to port it let's go
I removed the using namespace std;
then you prefix the cout commands with std::
we don't need to let that get in the way for now
STEP 1
now the simple port just to get it to compile in BaCon
and see if it works correctly
changed the word breadth to width
I commented out the fltk dependency
to show its not needed here
STEP2 we start to convert code to use BaCon syntax for the PRINT
STEP 3 we also need to know how to convert .member syntax
to arrow operator -> syntax because this is widely used in C++
another way to think about this is everything points back to the CLASS
the CLASS is your customised TYPE that you created all neat in one place
I will demo several ways to use this command
and even how to port a code example
from a custom class not related to any GUI toolkit
first some documentation with many details if you need it
if not you can skip it and focus on the code below
www.tutorialspoint.com/cplusplus/cpp_classes_objects.htm
if confronted with this example in C++
and you want to port it let's go
I removed the using namespace std;
then you prefix the cout commands with std::
we don't need to let that get in the way for now
#include <iostream>
// call it class1demo.cxx then compile and run
// g++ -O -Wall class1demo.cxx -o app
// ./app
//using namespace std;
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main() {
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
std::cout << "Volume of Box1 : " << volume <<std::endl;
// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
std::cout << "Volume of Box2 : " << volume <<std::endl;
return 0;
}
STEP 1
now the simple port just to get it to compile in BaCon
and see if it works correctly
changed the word breadth to width
I commented out the fltk dependency
to show its not needed here
'---PRAGMA LDFLAGS -lfltk -L.
PRAGMA LDFLAGS -lbacon++
PRAGMA COMPILER g++
PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -Wno-permissive
OPTION PARSE FALSE
'--- Show the use of custom class.
'--- using BaCons new CLASS ENDCLASS commmand
PRAGMA INCLUDE <stdio.h>
PRAGMA INCLUDE <string.h>
PRAGMA INCLUDE <stdlib.h>
PRAGMA INCLUDE <iostream>
CLASS Box
public:
double length; // Length of a box
double width; // Breadth of a box
double height; // Height of a box
ENDCLASS
'--- Declare Box1 of type Box
'--- Declare Box2 of type Box
DECLARE Box1 TYPE Box
DECLARE Box2 TYPE Box
'--- Store the volume of a box here
DECLARE volume TYPE double
volume = 0.0
'--- box 1 set values
Box1.height = 5.0
Box1.length = 6.0
Box1.width = 7.0
'--- box 2 set values
Box2.height = 10.0
Box2.length = 12.0
Box2.width = 13.0
// volume of box 1
volume = Box1.height * Box1.length * Box1.width
std::cout << "Volume of Box1 : " << volume <<std::endl
// volume of box 2
volume = Box2.height * Box2.length * Box2.width
std::cout << "Volume of Box2 : " << volume <<std::endl
STEP2 we start to convert code to use BaCon syntax for the PRINT
'---PRAGMA LDFLAGS -lfltk -L.
PRAGMA LDFLAGS -lbacon++
PRAGMA COMPILER g++
PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -Wno-permissive
OPTION PARSE FALSE
'--- Show the use of custom class.
'--- using BaCons new CLASS ENDCLASS commmand
PRAGMA INCLUDE <stdio.h>
PRAGMA INCLUDE <string.h>
PRAGMA INCLUDE <stdlib.h>
PRAGMA INCLUDE <iostream>
CLASS Box
public:
double length; // Length of a box
double width; // Breadth of a box
double height; // Height of a box
ENDCLASS
'--- Declare Box1 of type Box
'--- Declare Box2 of type Box
DECLARE Box1 TYPE Box
DECLARE Box2 TYPE Box
'--- Store the volume of a box here
DECLARE volume TYPE double
volume = 0.0
'--- box 1 set values
Box1.height = 5.0
Box1.length = 6.0
Box1.width = 7.0
'--- box 2 set values
Box2.height = 10.0
Box2.length = 12.0
Box2.width = 13.0
'--- volume of box 1
volume = Box1.height * Box1.length * Box1.width
PRINT "Volume of Box1 : " , volume
'--- volume of box 2
volume = Box2.height * Box2.length * Box2.width
PRINT "Volume of Box2 : " , volume
STEP 3 we also need to know how to convert .member syntax
to arrow operator -> syntax because this is widely used in C++
another way to think about this is everything points back to the CLASS
the CLASS is your customised TYPE that you created all neat in one place
'---PRAGMA LDFLAGS -lfltk -L.
PRAGMA LDFLAGS -lbacon++
PRAGMA COMPILER g++
PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -Wno-permissive
OPTION PARSE FALSE
'--- Show the use of custom class.
'--- using BaCons new CLASS ENDCLASS commmand
'--- if you want to use C code printf
PRAGMA INCLUDE <stdio.h>
PRAGMA INCLUDE <string.h>
PRAGMA INCLUDE <stdlib.h>
'--- C++ style cout
PRAGMA INCLUDE <iostream>
CLASS Box
public:
double length; // Length of a box
double width; // Breadth of a box
double height; // Height of a box
ENDCLASS
'--- Declare Box1 of type Box
'--- Declare Box2 of type Box
DECLARE Box1 TYPE Box*
DECLARE Box2 TYPE Box*
'--- Store the volume of a box here
DECLARE volume TYPE double
volume = 0.0
'--- we have a simple way to do memory management
'--- using low level memory
'--- most GUI C++ code uses this type of syntax
Box1 = new Box
Box2 = new Box
'--- box 1 set values
Box1->height = 5.0
Box1->length = 6.0
Box1->width = 7.0
'--- box 2 set values
Box2->height = 10.0
Box2->length = 12.0
Box2->width = 13.0
'--- volume of box 1 so you don't confuse pointers with multiply
volume = Box1->height * Box1->length * Box1->width
PRINT "Volume of Box1 : " , volume
'--- volume of box 2
volume = Box2->height * Box2->length * Box2->width
PRINT "Volume of Box2 : " , volume