home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / PCACHSRC.ZIP / OOP.CPP < prev    next >
C/C++ Source or Header  |  1991-09-11  |  3KB  |  87 lines

  1. //simple C++ demo.
  2. //OOP.CPP   demo OOP program, links C++ to .ASM member-function.
  3. //to compile this file, called OOP.CPP, using Borland C++:
  4. //  BCC OOP.CPP MEMBER.ASM
  5. //or can do it in steps....
  6. // BCC -c OOP.CPP
  7. // TASM /ml MEMBER
  8. // TLINK OOP MEMBER
  9. //*****note that this .CPP file first had to be compiled to produce .ASM
  10. //     output... see end of file.******
  11.  
  12. //.......................................................................
  13. class box                //declaring a class called box.
  14. {
  15.     int row;            //giving it some data.
  16.     int col;
  17. public:
  18.     virtual void place(int ,int);    //declaring a method called place().
  19. };
  20.  
  21. class circle                //declaring a class called circle.
  22. {          int row,col;            //giving it some data.
  23. public:
  24.     virtual void place(int,int);    //declaring a method called place()
  25.     virtual void draw();        //another method called draw()
  26. };
  27.  
  28. //..........................................................................
  29. void box::place(int r,int c)    //definition of place() belonging to box.
  30. {
  31. //    row = r;        //these two lines can also be done in
  32. //    col = c;        //asm, as below....
  33.     asm {
  34.     mov si,this        //"this" equates to [bp+4]
  35.     mov [si].row,r        //in-line assembly, can use all labels
  36.     mov [si].col,c         //from the C++ program.
  37.                 //"this" is the addr of current object.
  38.         }
  39. }
  40.  
  41. void circle::draw(void)        //definition of draw() belonging to circle.
  42. {                //does nothing.
  43. }
  44. //.........................................................................
  45.     box box1;        //instances.  By declaring them here I am
  46.     box box2;        //making them static, and they will be
  47.     circle circle1;        //created in the data segment.
  48.                 //if i had declared them within main() they
  49.                 //would be automatic -- created on the
  50.                 //stack only (see below)...
  51.  
  52. //.......................................................................
  53. main()
  54. {
  55.   box box3;            //instance created in stack segment.
  56.  
  57.   box1.place(3,5);        //calls place() belonging to box class,
  58.                 //with "this" set to box1.
  59.  
  60.   box *ptr;            //same thing using a pointer.
  61.   ptr = &box1;            //required if we want this line to call
  62.   ptr -> place(3,5);        //various place()'s depending on entry point.
  63.  
  64.   circle1.place(1,2);        //call the .asm function.
  65. }
  66.  
  67. //........................................................................
  68. //the following is anly a stub, to get the skeleton for a .asm module.
  69. //Compile to produce .asm output, then remove this stub....
  70. //See MEMBER.ASM for notes on this.
  71.  
  72. void circle::place(int r,int c)    //definition of place() belonging to circle.
  73. //let's say that i want this function to be written in asm, not inline.
  74. //furthermore, the asm program must have access to other C++ functions,
  75. //and the data-members of its current instance....
  76. //Firstly, bit of a skeleton at the C++ level....
  77. {
  78.     row=r; col=c;        //getting at current object's data.
  79.     box1.place(1,2);    //calling another function, another object.
  80.     this->draw();          //calling a function,same object.
  81. }
  82.  
  83. // unfortunately i was unable to access row and col by name from MEMBER.ASM.
  84. // ... this would have been nice.  However by specifying them in the above
  85. // stub, you will find the appropriate code generated in the .ASM file.
  86. // (My .ASM module is able to access C++ member-functions by their
  87. // "mangled" names, so why not data-members by name?)