Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ Language Reference - structs and classes in c++ _ http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
                        Structs and Classes in C++                    _
   The  C++  struct  is  an extension of the C struct.  It  has  all  the
   attributes  of  a  C  struct,  but has  been  expanded  to  allow  the
   definition of abstract data types. C++ has a new type of struct called
   a class. In C++ structs and classes have a number of attributes.  They
   can  contain  a  collection of data members which may  be  basic  data
   types,  or may themselves be unions, structs or classes. A  struct  or
   class  may  also  contain  member functions  which  define  a  set  of
   operations which can be carried out on these data items. Finally a C++
   class  has facilities for access control, restricting the  ability  of
   functions  to use or modify members of the class, through the  use  of
   the private, public, and protected keywords.

   Every struct or class has a unique type name or tag and  can
   be used anywhere that a built-in type can be used. They are subject to
   the  same  vigorous type checking that applies to  the  built-in  data
   types.  In  what  follows  most of the  discussion,  except  where  it
   concerns  access  control, applies equally well to  both  structs  and
   classes.  Most of the explanations will refer to the new  struct  type
   which is central to object-oriented programming with C++  the class.

                            The Class Definition
   A class definition consists of two parts. The first part is the  class
   head  which consists of the keyword class followed by the  class  name
   (tag). The second part is the class body which is surrounded by braces
   and  followed by either a semicolon, or a list declaring data  objects
   of the class type.

   class Test
   //class head
   {
   // class body
   } test1, test2;
   //declaration list

   A   typical  class  definition  consists  of  data   members,   access
   specifiers,  and  member  function  declarations.

                                Data Members
   Class  data  members are declared in the same way that  variables  are
   normally declared in C and C++, except that an explicit initializer is
   NOT  allowed,  i.e.  int num = 1; is not allowed  in  a  class  member
   declaration.  As in normal variable declarations, data members  having
   the same type can be combined in one declaration such as float f1, f2,
   f3;.

   If an object of another class is to be used as a data member its class
   definition  must already have been seen by the compiler,  otherwise  a
   type  error  will occur. It is, however, possible to use  pointers  or
   references  to the class object by supplying a forward declaration  of
   the class, thus:

   class Forward;

   class Test
   {
   Forward *next;
   };
   A  forward  declaration of the class allows the use  of  pointers  and
   references  to objects of the class without the need first to see  the
   full class definition.

   A class is not considered to be defined until the closing brace of the
   class  body has been seen. This means that a class CANNOT  contain  an
   object of its own class as a data member. A class is considered to  be
   declared  after the class head has been seen. This means that a  class
   CAN declare pointer and reference data members of its own class.


Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson