home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / OVERVIEW.ZIP / OVERVIEW.TXT
Encoding:
Text File  |  1996-04-26  |  11.1 KB  |  695 lines

  1.  
  2.  
  3. C++ Overview
  4.  
  5.  
  6. by John Tal
  7.  
  8.  
  9.  
  10. 1.0.C++ and C
  11.  
  12.  
  13. C++ is an object-oriented extension to the C language.  C++ can be
  14.  
  15. used to compile C programs.  Everything you can do in C, you can also
  16.  
  17. do in C++.  C++ programs and their extensions cannot be compiled under 
  18.  
  19. a C only compiler.
  20.  
  21.  
  22. 2.0.Object-Oriented Programming
  23.  
  24.  
  25. 2.1.Class
  26.  
  27.  
  28. The class concept is the fundamental building block for all
  29.  
  30. objected-oriented programming systems (OOPS).  A class consists of an
  31.  
  32. object (or group of objects) and the function(s) which operate on the
  33.  
  34. obects(s).  In C++, a class is defined in a similar way to a
  35.  
  36. structure, except the keyword class is used:
  37.  
  38.  
  39. ..class FILE_C;     // forward reference to a class
  40.  
  41.  
  42.  
  43. 2.2.Class Components
  44.  
  45.  
  46. A class definition contains declarations for variables and functions. 
  47.  
  48. A class definition also provides different security areas for
  49.  
  50. variables and functions.
  51.  
  52.  
  53. ..class FILE_C
  54.  
  55. ..{
  56.  
  57. ..  private:
  58.  
  59. ..  .long.ptr;..  // file pointer
  60.  
  61. ...char.name[FNAME_LEN];  // file name
  62.  
  63. ...short.state;..  // file state
  64.  
  65. ...short.mode;..  // file mode
  66.  
  67. ..  public:
  68.  
  69. ...FILE_C(void);..// class constructor
  70.  
  71. ...~FILE_C(void);.        // class destructor
  72.  
  73. ...short.Open(char *,short);  // open function
  74.  
  75. .      ..short.Close(void);.     // close function
  76.  
  77. ...short.Read(char *,short);  // read function
  78.  
  79. ...short.Write(char *,short); // write function
  80.  
  81. ..};
  82.  
  83.  
  84. The above FILE_C class has four private data items (objects) and six
  85.  
  86. functions which operate on those objects.  Any access to the class
  87.  
  88. objects must occur through a class function.  The private keyword
  89.  
  90. enforces this data hiding by preventing the application using the
  91.  
  92. class from having access to anything in the class private area.  Only
  93.  
  94. the class functions themselves can access private objects.
  95.  
  96.  
  97. 2.3.Class Constructors And Destructors
  98.  
  99.  
  100. The FILE_C class has a FILE_C() function as a constructor and a
  101.  
  102. ~FILE_C() function as a destructor.  The constructor is a function
  103.  
  104. with the same name as the class which is invoked at the creation of
  105.  
  106. an instance of the class and is intended to provide any
  107.  
  108. initialization the class requires.   The destructor function also 
  109.  
  110. has the same name as the class but with a '~' (tilde) character in 
  111.  
  112. front of it.  A destructor is provided for any cleanup work to occur 
  113.  
  114. within the class at the termination of the class instance (such as 
  115.  
  116. making sure the file is closed).
  117.  
  118.  
  119. A class instance is created in two different ways.  The first is by
  120.  
  121. declaring a class instance in the same way a standard C language
  122.  
  123. variable is created.
  124.  
  125.  
  126. .short.x;         //  create a variable named x
  127.  
  128. .FILE_C  LogFile;   // create a unique instance of the FILE_C class
  129.  
  130.  
  131.  
  132. The second method of creating a class instance is by allocating a new
  133.  
  134. instance through a pointer and the new keyword.
  135.  
  136.  
  137. .FILE_C * pLogFile;.// create a pointer to a FILE_C class
  138.  
  139.  
  140. .pLogFile = new FILE_C;.// create an instance and assign ptr
  141.  
  142.  
  143. The new keyword is provided as an improved calloc or malloc.  The new
  144.  
  145. keyword calculates the size of the memory block to be allocated to
  146.  
  147. the item being created.
  148.  
  149.  
  150. 2.3.1.Constructors With Parameters
  151.  
  152.  
  153. Like any function, a constructor can take parameters.   The
  154.  
  155. parameters would be supplied at the time of the creation of a new
  156.  
  157. class instance.
  158.  
  159.  
  160. .class FILE_C
  161.  
  162. .{
  163.  
  164. .....
  165.  
  166. .  public:
  167.  
  168. ..FILE_C(char *pFileName);
  169.  
  170. .};
  171.  
  172.  
  173. .FILE_C.LogFile("LogFileName");  // constructor with parm
  174.  
  175.  
  176.  
  177. 2.4.Class Function Calling
  178.  
  179.  
  180. Class member functions are called like normal C functions.  The
  181.  
  182. difference is that they use syntax similar to that used for structure
  183.  
  184. membership.
  185.  
  186.  
  187. .
  188.  
  189. .FILE_C.LogFile;.// create instance of FILE_C
  190.  
  191. .FILE_C * pInputFile;.// create ptr to instance of FILE_C
  192.  
  193.  
  194. .pInputFile = new FILE_C;   // create instance of FILE_C
  195.  
  196.  
  197. .LogFile.Open("LogFile",O_APPEND);    // open LogFile
  198.  
  199.  
  200. .InputFile -> Open("InputDat",O_READONLY);  // open InputFile
  201.  
  202.  
  203. .InputFile -> Read(buffer,sizeof(buffer));  // read InputFile
  204.  
  205.  
  206.  
  207. From the above example, a file pointer is never sent to the FILE_C
  208.  
  209. functions as would be in standard C file functions.  This is because
  210.  
  211. each instance of the FILE_C maintains its own control information
  212.  
  213. internal to the class itself in its own private area.  C++ usually
  214.  
  215. simplifies interfaces between classes and applications because
  216.  
  217. classes are complete in themselves.  They contain all the attributes
  218.  
  219. and/or objects that describe the class within.
  220.  
  221.  
  222.  
  223. 2.5.Class Function Declaration
  224.  
  225.  
  226. A class definition (such as class FILE_C ...) would occur in an 
  227.  
  228. include file.  The actual functions of the class would be declared in
  229.  
  230. a C++ source file.   Each class function is prefixed with the class
  231.  
  232. name to which it belongs and the symbol '::'.
  233.  
  234.  
  235. .short FILE_C::Open(char * pFileName,short mode)
  236.  
  237. .{
  238.  
  239. ..mode = mode;   // referencing private data item
  240.  
  241.  
  242. ..strcpy(name,pFileName);
  243.  
  244.  
  245. ..//  perform open
  246.  
  247.  
  248. . .return (status);
  249.  
  250. .}
  251.  
  252.  
  253.  
  254. 2.6.Inline Functions
  255.  
  256.  
  257. If a class function is performing a very simple task, it can be
  258.  
  259. declared an an inline function.  An inline function is an expanded
  260.  
  261. version of the function declaration within the class with begin and
  262.  
  263. end braces surronding the inline statement(s).
  264.  
  265.  
  266. .class FILE_C
  267.  
  268. .{
  269.  
  270. .  private:
  271.  
  272. .    char.name[FNAME_LEN];  // file name
  273.  
  274. .....
  275.  
  276. .  public:
  277.  
  278. .     FILE_C(char *pFileName) { strcpy(name,pFileName); }
  279.  
  280. .....
  281.  
  282. .};
  283.  
  284.  
  285.  
  286. The above example shows the FILE_C constructor implemented as an
  287.  
  288. inline function.  Inline functions should be limited to functions
  289.  
  290. having only a few (preferrably one) statement(s).
  291.  
  292.  
  293.  
  294. 3.0.Derived Classes
  295.  
  296.  
  297. One of C++' most powerful features is to use classes as building
  298.  
  299. blocks in creating entirely new classes.
  300.  
  301.  
  302. .class BROWSE_C : FILE_C   // browse derived from file
  303.  
  304. .{.      
  305.  
  306. .  private:
  307.  
  308. .    short  curline;
  309.  
  310. .    ...
  311.  
  312. .  public:
  313.  
  314. .    BROWSE_C(void);
  315.  
  316. .    ~BROWSE_C(void);
  317.  
  318. .    OpenFile(char *);
  319.  
  320. .};
  321.  
  322.  
  323.  
  324. From the above example, the BROWSE_C class will have access not only
  325.  
  326. to all of its own member data/objects, but also to all FILE_C class
  327.  
  328. member functions which were declared as public or protected in
  329.  
  330. FILE_C.  The following table breaks down class security areas for
  331.  
  332. immediate classes and derived classes.
  333.  
  334.  
  335.  
  336. .Immediate..Derived
  337.  
  338. .----------..---------
  339.  
  340. .Private...Not-visible in derived class
  341.  
  342. .Protected..Visible as Private in derived
  343.  
  344. .Public...Visible as Protected in derived
  345.  
  346.  
  347. From the above example, the BROWSE_C class would be able to access
  348.  
  349. any data and functions which were defined as protected or public in
  350.  
  351. the FILE_C class.  The application would not be able to access any of
  352.  
  353. the data or functions of the FILE_C class without going through a
  354.  
  355. public member function of the BROWSE_C class.   These are the default
  356.  
  357. security inheritance protocols for classes.
  358.  
  359.  
  360. 3.1.Customizing Class Inheritance
  361.  
  362.  
  363. The default security inheritance can be overridden when defining
  364.  
  365. the derived class:
  366.  
  367.  
  368. .class BROWSE_C : public FILE_C   // browse derived from file
  369.  
  370. .{.      
  371.  
  372. .  private:
  373.  
  374. .    short  curline;
  375.  
  376. .    ...
  377.  
  378. .  public:
  379.  
  380. .    BROWSE_C(void);
  381.  
  382. .    ~BROWSE_C(void);
  383.  
  384. .    OpenFile(char *);
  385.  
  386. .                       
  387.  
  388. .};
  389.  
  390.  
  391. From the above example, all public functions of FILE_C class are also
  392.  
  393. public to applications using the BROWSE_C class.
  394.  
  395. .
  396.  
  397. 3.2.Container Classes
  398.  
  399.  
  400. Container classes are classes which contain other classes.  An example
  401.  
  402. would be a class to implement a binary tree:
  403.  
  404.  
  405. .class TREE_C
  406.  
  407. .{
  408.  
  409.  
  410. .   private:
  411.  
  412.  
  413.  
  414. .    struct TNODE_S. // the contained class
  415.  
  416. .    {
  417.  
  418. .      PVOID          pvData;
  419.  
  420. .      struct TNODE_S *pstLeft;
  421.  
  422. .      struct TNODE_S *pstRight;
  423.  
  424. .    };
  425.  
  426. .    typedef struct TNODE_S TNODE_T;
  427.  
  428. .    typedef TNODE_T *TNODE_P;
  429.  
  430. .    typedef TNODE_T **TNODE_PP;
  431.  
  432.  
  433. .    TNODE_P  pstHead;
  434.  
  435. .    TNODE_P  pstNode;
  436.  
  437. .....
  438.  
  439. .
  440.  
  441. .public:
  442.  
  443. .
  444.  
  445. .    TREE_C(VOID);
  446.  
  447. .    ~TREE_C(VOID);
  448.  
  449. .    SHORT    Delete(PVOID);          // Remove entry
  450.  
  451. .    SHORT    Find(PVOID,PPVOID);     // Find entry
  452.  
  453. .    SHORT    Insert(PVOID);          // Insert entry
  454.  
  455. .....
  456.  
  457. .};
  458.  
  459. .
  460.  
  461. .typedef TREE_C * TREE_CP;
  462.  
  463. .typedef TREE_C ** TREE_CPP;
  464.  
  465.  
  466. In the binary tree example, it was not desirable for each node of the
  467.  
  468. tree to be an instance of the TREE_C class.  So each node is
  469.  
  470. contained in the TREE_C class and the TREE_C class operates on all
  471.  
  472. the TNODE_S structures/classes contained within it.
  473.  
  474.  
  475. 3.3.Virtual Functions
  476.  
  477.  
  478. Virtual functions provide a way for a base class function to take on
  479.  
  480. the characteristics or behavior appropriate to the current derived
  481.  
  482. class.
  483.  
  484.  
  485. .class FILE_C
  486.  
  487. .{
  488.  
  489. .  private:
  490.  
  491. .    char.name[FNAME_LEN];  // file name
  492.  
  493. .....
  494.  
  495. .  public:
  496.  
  497. .     FILE_C(char *pFileName) { strcpy(name,pFileName); }
  498.  
  499. .     virtual short Reset(void);  
  500.  
  501. .};
  502.  
  503.  
  504. .class BROWSE_C : FILE_C   // browse derived from file
  505.  
  506. .{.      
  507.  
  508. .  private:
  509.  
  510. .    short  curline;
  511.  
  512. .    ...
  513.  
  514. .  public:
  515.  
  516. .    BROWSE_C(void);
  517.  
  518. .    ~BROWSE_C(void);
  519.  
  520. .    OpenFile(char *);
  521.  
  522. .    short Reset(void);
  523.  
  524. .};
  525.  
  526.  
  527. .short BROWSE_C::Reset(void)
  528.  
  529. .{
  530.  
  531. .    FILE_C::Reset();
  532.  
  533. .    curline = 0;
  534.  
  535.         }
  536.  
  537.  
  538.  
  539.  
  540.  
  541. 4.0.Operator Overloading
  542.  
  543.  
  544. Since C++ classes define in detail the characters of and operations
  545.  
  546. upon the class instance, C++ allows all standard operators (i.e. '+',
  547.  
  548. '-', '*', '/', '++', '--') to be redefined or overloaded for the
  549.  
  550. current class.
  551.  
  552.  
  553. .class STRING_C
  554.  
  555. .{
  556.  
  557. .   private:
  558.  
  559. .     char * pChar;
  560.  
  561. .     int  len;
  562.  
  563. .....
  564.  
  565. .   public:
  566.  
  567. .     STRING_C(const char * = 0);  // provide default value
  568.  
  569. .    ~STRING_C(delete pChar);
  570.  
  571. .     void operator+(char *)
  572.  
  573.       .};
  574.  
  575.  
  576. .STRING_C::operator+(char *pC)
  577.  
  578. .{
  579.  
  580. .   char * pBuf;
  581.  
  582. .   pBuf = new char[len=strlen(pC)+len];
  583.  
  584.            strcpy(pBuf,pChar);
  585.  
  586. .   strcat(pBuf,pC);
  587.  
  588.        .   delete pChar;
  589.  
  590. .   pChar = pBuf;
  591.  
  592. .}
  593.  
  594.  
  595.  
  596. .STRING_C  Str("ABC");
  597.  
  598.  
  599. .Str + "DEF";.   // internal pChar now = 'ABCDEF'
  600.  
  601.  
  602. .
  603.  
  604. Operator overloading still involves functions to simulate the
  605.  
  606. operator being overloaded.  Overloading simply provides a mechanism
  607.  
  608. for abbreviating the operations.
  609.  
  610.  
  611.  
  612. 5.0.C++ Input/Output
  613.  
  614.  
  615. C++ output is simplified over that of C printf family functions.  C++
  616.  
  617. defines the keywords cout and cin and the stdout and stdin devices. 
  618.  
  619. C++ automatically formats the output based on the current variable
  620.  
  621. type.
  622.  
  623.  
  624.  
  625. ./*  In C */
  626.  
  627.  
  628. .printf("%s = %d\n", szRate, sRate);
  629.  
  630.  
  631. .//  In C++
  632.  
  633.  
  634. .cout << szRate << " = " << sRate << "\n";
  635.  
  636.  
  637.  
  638. .//  Another C++ alternative
  639.  
  640.  
  641. .cout << form("%s = %d\n", szRate,sRate);
  642.  
  643.  
  644.  
  645. ./*  In C */
  646.  
  647.  
  648. .scanf("%d",&sRate);
  649.  
  650.  
  651. .//  In C++
  652.  
  653.  
  654. .cin >> sRate;
  655.  
  656.  
  657.  
  658. 7.0.Reference Variables
  659.  
  660.  
  661. C++ provides a syntax which allows programmers who are not
  662.  
  663. comfortable with C pointer syntax to more easily write applications
  664.  
  665. which use pointers.
  666.  
  667.  
  668.  
  669. ./*   In C */
  670.  
  671.  
  672. .short Afunc(short * psShort)
  673.  
  674. .{
  675.  
  676. .   *psShort++;
  677.  
  678. .}
  679.  
  680.  
  681.  
  682. .//   In C++
  683.  
  684.  
  685. .short Afunc(short & Short)
  686.  
  687. .{
  688.  
  689. .   Short++;
  690.  
  691. .}
  692.  
  693.  
  694.