Thursday, February 19, 2015

Header Files in C and C++ :

Header Files Summary

Header files, also called include files, provide function prototype declarations for library functions. Data types and symbolic constants used with the library functions are also defined in them, along with global variables defined by Parijat's C++ and by the library functions. The Parijat's C++ library
follows the ANSI C standard on names of header files and their contents.

Note:    The middle column indicates C++ header files and header files defined by ANSI C.

alloc.h        Declares memory-management functions (allocation, deallocation, and so on). 

assert.h    ANSI C    Defines the assert debugging macro.

bcd.h    C++    Declares the C++ class bcd and the overloaded operators for bcd and bcd math functions.

bios.h        Declares various functions used in calling IBM-PC ROM BIOS routines.

bwcc.h        Defines the Borland Windows Custom Control interface.

checks.h    C++    Defines the class diagnostic macros.

complex.h    C++    Declares the C++ complex math functions.

conio.h        Declares various functions used in calling the operating system console I/O routines.

constrea.h    C++    Defines the conbuf and constream classes.

cstring.h    C++    Defines the string classes.

ctype.h    ANSI C    Contains information used by the character classification and character conversion macros (such as isalpha and

toascii).

date.h    C++    Defines the date class.

_defs.h        Defines the calling conventions for different application types and memory models.

dir.h        Contains structures, macros, and functions for working with directories and path names.

direct.h        Defines structures, macros, and functions for dealing with directories and path names.

dirent.h        Declares functions and structures for POSIX directory operations.

dos.h        Defines various constants and gives declarations needed for DOS and 8086-specific calls. 

errno.h    ANSI C    Defines constant mnemonics for the error codes.

except.h    C++    Declares the exception-handling classes and functions.

excpt.h        Declares C structured exception support.

fcntl.h        Defines symbolic constants used in connection with the library routine open.

file.h    C++    Defines the file class.

float.h    ANSI C    Contains parameters for floating-point routines.

fstream.h    C++    Declares the C++ stream classes that support file input and output.

generic.h    C++    Contains macros for generic class declarations.

io.h        Contains structures and declarations for low-level input/output routines.

iomanip.h    C++    Declares the C++ streams I/O manipulators and contains templates for creating parameterized manipulators.

iostream.h    C++    Declares the basic C++ streams (I/O) routines.

limits.h    ANSI C    Contains environmental parameters, information about compile-time limitations, and ranges of integral quantities.

locale.h    ANSI C    Declares functions that provide country- and language-specific information.

malloc.h        Declares memory-management functions and variables.

math.h    ANSI C    Declares prototypes for the math functions and math error handlers.

mem.h        Declares the memory-manipulation functions. (Many of these are also defined in string.h.)

memory.h        Contains memory-manipulation functions.

new.h    C++    Access to _new_handler, and set_new_handler.

_nfile.h        Defines the maximum number of open files.

_null.h        Defines the value of NULL.

process.h        Contains structures and declarations for the spawn... and exec... functions.

search.h        Declares functions for searching and sorting.

setjmp.h    ANSI C    Declares the functions longjmp and setjmp and defines a type jmp_buf that these functions use.

share.h        Defines parameters used in functions that make use of file-sharing.

signal.h    ANSI C    Defines constants and declarations for use by the signal and raise functions.

stdarg.h    ANSI C    Defines macros used for reading the argument list in functions declared to accept a variable number of arguments

(such as vprintf, vscanf, and so on).

stddef.h    ANSI C    Defines several common data types and macros.

stdio.h    ANSI C    Defines types and macros needed for the standard I/O package defined in Kernighan and Ritchie and extended

under UNIX System V. Defines the standard I/O predefined streams stdin, stdout, stdprn, and stderr and declares

stream-level I/O routines.

stdiostr.h    C++    Declares the C++ (version 2.0) stream classes for use with stdio FILE structures. You should use iostream.h for

new code.

stdlib.h    ANSI C    Declares several commonly used routines such as conversion routines and search/sort routines.

string.h    ANSI C    Declares several string-manipulation and memory-manipulation routines.

strstrea.h    C++    Declares the C++ stream classes for use with byte arrays in memory.

sys\locking.h        Contains definitions for mode parameter of locking function.

sys\stat.h        Defines symbolic constants used for opening and creating files.

sys\timeb.h        Declares the function ftime and the structure timeb that ftime returns.

sys\types.h        Declares the type time_t used with time functions.

thread.h    C++    Defines the thread classes.

time.h    ANSI C    Defines a structure filled in by the time-conversion routines asctime, localtime, and gmtime, and a type used by the

routines ctime, difftime, gmtime, localtime, and stime. It also provides prototypes for these routines.

typeinfo.h    C++    Declares the run-time type information classes.

utime.h        Declares the utime function and the utimbuf struct that it returns.

values.h        Defines important constants, including machine dependencies; provided for UNIX System V compatibility.

varargs.h        Definitions for accessing parameters in functions that accept a variable number of arguments. Provided for UNIX

compatibility; you should use stdarg.h for new code.

Virtual Functions in C Language :

Virtual Functions :

Virtual functions let derived classes provide different versions of a base class function.
You can declare a virtual function in a base class , then redefine it in any derived class, even if the number and type of arguments are the same . The redefined function overrides the base class function.
You can also declare the functions

  int Base::Function(int)

and

  int Derived::Function(int)

even when they are not virtual.
When you declare virtual functions, keep these guidelines in mind:

    They can only be member functions.
    They can be declared a friend in another class.
    They cannot be a static member.

The base class version is available to derived class objects via scope override. If they are virtual, only the function associated with the actual type of
the object is available.
If two functions with the same name and have different arguments, C++ considers them different, and the virtual function mechanism is ignored.

virtual void gork(void) = 0;

is a pure virtual function. This makes the class an abstract type. gork() must be defined by derived classes or re-declared as pure.
Generally, when redefining a virtual function, you cannot change just the function return type. To redefine a virtual function, the new definition (in some
derived class) must exactly match the return type and formal parameters of the initial declaration. If two functions with the same name have different
formal parameters, C++ considers them different, and the virtual function mechanism is ignored.
However, for certain virtual functions in a base class, their overriding version in a derived class can have a return type that is different from the
overridden function. This is possible only when both of the following conditions are met:

    The overridden virtual function returns a pointer or reference to the base class.
    The overriding function returns a pointer or reference to the derived class.

If a base class B and class D (derived publicly from B) each contain a virtual function vf, then if vf is called for an object d of D, the call made is
D::vf(), even when the access is via a pointer or reference to B. For example,

struct X {};          // Base class.
struct Y : X {};      // Derived class.

struct B {
   virtual void vf1();
   virtual void vf2();
   virtual void vf3();
   void f();
   virtual X* pf();   /* Return type is a pointer to base. */
                      /* This can be overridden. */
   };

class D : public B {
public:
   virtual void vf1(); /* Virtual specifier is legal but redundant. */
   void vf2(int);      /* Not virtual, since it's using a different */
                       /* arg list. This hides B::vf2(). */

   // char vf3();
   // Illegal: return-type-only change!
   void f();
   Y*   pf();          /* Overriding function differs only  */
                       /* in return type. Returns a pointer */
                       /* to the derived class.             */
   };

    void extf() {
    D d;            /* Instantiate D */
    B* bp = &d;     /* Standard conversion from D* to B*             */
                    /* Initialize bp with the table of functions     */
                    /* provided for object d. If there is no entry   */

                    /* for a function in thed-table, use the         */
                    /* function in the B-table.                      */
    bp->vf1();      /* Calls D::vf1 */
    bp->vf2();      /* Calls B::vf2 since D's vf2 has different args */
    bp->f();        /* Calls B::f (not virtual)                      */

          X* xptr = bp->pf();   /* Calls D::pf() and converts the result
                                   to a pointer to X. */

       D* dptr = &d;
       Y* yptr = dptr->pf();  /* Calls D::pf() and initializes yptr. */

                              /* No further conversion is done.      */
       }

The overriding function vf1 in D is automatically virtual . The virtual specifier can be used with an overriding function declaration in the derived class. If other classes will be derived from D, the virtual keyword is required . If no further classes will be derived from D, these of virtual is redundant.
The interpretation of a virtual function call depends on the type of the object it is called for; with nonvirtual function calls, the interpretation depends only on the type of the pointer or reference denoting the object it is called for .
virtual functions exact a price for their versatility: each object in the. derived class needs to carry a pointer to a table of functions in order to select the correct one at run time (late binding) .


Learning C Functions : delay ( Dos Only )

C Programming :
Function
:
delay

Syntax


#include <dos.h>
void delay(unsigned milliseconds);

Description

With a call to delay, the current program is suspended from execution for the number of milliseconds specified by the argument milliseconds. It is no
longer necessary to make a calibration call to delay before using it. delay is accurate to a millisecond.

Return Value

None.

Example :
/* delay example */

/* emits a 440-Hz tone for 500 milliseconds */
#include <dos.h>

int main(void)
{
   sound(440);
   delay(500);
   nosound();
   return 0;
}

Learning C Functions : coreleft ( Dos Only )

C Programming :
Function
:
coreleft

Syntax
#include <alloc.h>

In the tiny, small, and medium models:

unsigned coreleft(void);

In the compact, large, and huge models:

unsigned long coreleft(void);

Description

coreleft returns a measure of RAM memory not in use. It gives a different measurement value, depending on whether the memory model is of the small
data group or the large data group .

Return Value

In the small data models, coreleft returns the amount of unused memory between the top of the heap and the stack. In the large data models, coreleft
returns the amount of memory between the highest allocated block and the end of available memory.

Learning C Functions : brk ( Dos Only )

C Programming :
Function
:
brk

Syntax

#include <alloc.h>
int brk(void *addr);

Description

brk dynamically changes the amount of space allocated to the calling program's heap. The change is made by resetting the program's break value,
which is the address of the first location beyond the end of the data segment. The amount of allocated space increases as the break value increases.
brk sets the break value to addr and changes the allocated space accordingly.
This function will fail without making any change in the allocated space if such a change would allocate more space than is allowable.

Return Value

Upon successful completion, brk returns a value of 0. On failure, this function returns a value of -1 and the global variable errno is set to

ENOMEM    Not enough memory

Example :
/* brk example */

#include <stdio.h>
#include <alloc.h>

int main(void)
{
   char *ptr;

   printf("Changing allocation with brk()\n");
   ptr = (char *) malloc(1);
   printf("Before brk() call: %lu bytes free\n", coreleft());
   brk(ptr+1000);
   printf(" After brk() call: %lu bytes free\n", coreleft());
   return 0;
}

Learning C Functions : biosprint ( Dos Only )

C Programming :
Function
:
biosprint

Example    Portability

Syntax

#include <bios.h>
int biosprint(int cmd, int abyte, int port);

Description

biosprint performs various printer functions on the printer identified by the parameter port using BIOS interrupt 0x17.
A port value of 0 corresponds to LPT1; a port value of 1 corresponds to LPT2; and so on.
The value of cmd can be one of the following:

0    Prints the character in abyte.
1    Initializes the printer port.
2    Reads the printer status.

The value of abyte can be 0 to 255.

Return Value

The value returned from any of these operations is the current printer status, which is obtained by ORing these bit values together:

Bit 0    0x01    Device time out
Bit 3    0x08     I/O error
Bit 4    0x10     Selected
Bit 5    0x20     Out of paper
Bit 6    0x40     Acknowledge
Bit 7    0x80     Not busy

Example :
/* biosprint example */

#include <stdio.h>
#include <conio.h>
#include <bios.h>

int main(void)
{
   #define STATUS  2    /* printer status command */
   #define PORTNUM 0    /* port number for LPT1 */

   int status, abyte=0;

   printf("Please turn off your printer. Press any key to continue\n");
   getch();
   status = biosprint(STATUS, abyte, PORTNUM);
   if (status & 0x01)
      printf("Device time out.\n");
   if (status & 0x08)
      printf("I/O error.\n");

   if (status & 0x10)
      printf("Selected.\n");
   if (status & 0x20)
      printf("Out of paper.\n");

   if (status & 0x40)
      printf("Acknowledge.\n");
   if (status & 0x80)
      printf("Not busy.\n");

   return 0;
}

Learning C Functions : bioskey ( Dos Only )

C Programming :
Function
:
bioskey

Syntax

#include <bios.h>
int bioskey(int cmd);

Description

bioskey performs various keyboard operations using BIOS interrupt 0x16. The parameter cmd determines the exact operation.

Return Value

The value returned by bioskey depends on the task it performs, determined by the value of cmd:

0    If the lower 8 bits are nonzero, bioskey returns the ASCII character for the next keystroke waiting in the queue or the next key pressed at the keyboard. If the lower 8 bits are zero, the upper 8 bits are the extended keyboard codes defined in the IBM PC Technical Reference Manual.
1    This tests whether a keystroke is available to be read. A return value of zero means no key is available. The return value is 0xFFFFF (-1) if Ctrl-Brk
has been pressed. Otherwise, the value of the next keystroke is returned. The keystroke itself is kept to be returned by the next call to bioskey that has a cmd value of zero.
2    Requests the current shift key status. The value is obtained by ORing the following values together:

Bit 7    0x80    Insert on
Bit 6    0x40    Caps on
Bit 5    0x20    Num Lock on
Bit 4    0x10    Scroll Lock on
Bit 3    0x08    Alt pressed
Bit 2    0x04    Ctrl pressed
Bit 1    0x02    Left arrow + Shift pressed
Bit 0    0x01    Right arrow + Shift pressed
 
Example :

/* bioskey example */

#include <stdio.h>
#include <bios.h>
#include <ctype.h>

#define RIGHT  0x01
#define LEFT   0x02
#define CTRL   0x04
#define ALT    0x08

int main(void)
{
   int key, modifiers;

   /* function 1 returns 0 until a key is pressed */
   while (bioskey(1) == 0);

   /* function 0 returns the key that is waiting */
   key = bioskey(0);

   /* use function 2 to determine if shift keys were used */
   modifiers = bioskey(2);
   if (modifiers)
   {
      printf("[");
      if (modifiers & RIGHT) printf("RIGHT");
      if (modifiers & LEFT)  printf("LEFT");
      if (modifiers & CTRL)  printf("CTRL");
      if (modifiers & ALT)   printf("ALT");
      printf("]");
   }
   /* print out the character read */
   if (isalnum(key & 0xFF))
      printf("'%c'\n", key);
   else
      printf("%#02x\n", key);
   return 0;
}