Monday, 6 July 2015

Chapter 4


Defining C++ Classes

  • Defining a class
  • Creating a class instance
  • Accessing class members
  • Encapsulation
  • Constructors and destructors
  • Inline member functions
  • The this pointer
  • Static class members
           This Chapter introduces C++ Classes. When you define a class, you create a new data type, which you can use much like on of the built - in C++ data types. A class, however, contains code as well as data. A class allows you to encapsulate all a window on the screen, a figure that the program draws, a device connected to a techniques for creating and using individual classes. In the next chapter, you'll learn how to define and use hierachies of related classes.


  • Defining a Class
          A C++ class is somewhat similar to a standard C structure, though the features of C++ classes go far beyond those provided by C structures. To understand C++ classes, it's useful to start by considering how structure are used in the C language.

         A C structure allows you to group a set of related data items. As a simple example, if your C program draws a rectangle on the screen, you might find it convenient to store the coodinates of the rectangle within a structure  defined as follows : 

         Struct Rectangle 
        {
            int   Left;
            int   Top;
            int   Right;
            int   Bottom;
          };
              You could then define a function for drawing the rectangle, as in the following example.
             Void Draw Rectangle (Struct Rectangle *Rect)

Chapter 3


Moving to C + +

  • Converting from C to C++
  • New features of C++

  • Converting from C to C++
        If you've written a C program, you've probably already written your first C++ program ! With few excpection, C++ is a superset of C; that is, C++ supports almost all the features of the C language in addition to the many new features that it offers. Therefore, to get started with C++, you can simply compile your existing C programs, as well as the new programs you write, using the C++ compiler rather than the C compiler. You can then gradually begin adding to your code various features that are unique to C++


        To begin using the C++ compiler, you need only name your source file with the .cpp or .cxx extension, rather than the .c extension. There are, however several programming practices permitted in most versions of C that won't compile using the C++ compiler, or that will compile with C++ but with altered meanings. The following is a description of some of the most important C constructs that don't work with C++:


  • C++ has many more reserved keywords than C; these are words that you can't use as identifies in your programs. The following is a list of the reserved key - words used in Visual C++. This list includes the C Keywords, Plus many additional ones unique to  C++:
     auto               else                new              this
     bad-cast           enum                operator         throw
     bad_typeid         except              private          true
     bool               explicit            protected        try
     break              extern              public           type_info
     case               false               register         typedef
     catch              finally             reinterpret_     typeid