Object oriented
programming (OOP) is a avant garde way of approaching the job of
programming. All OOP languages have three things in common:
objects,polymorphism, and inheritance. An object is a logical thing
that contains both data and code that manipulates the data.
Polymorphism provides for a single interface to support multiple
methods. Inheritance is the process in which one object can acquire
the properties and attributes of another object.
In C++ it
is possible to have arrays of objects. It is possible to have pointers
to objects. When a member function is called, it is automatically
passed an argument, the this pointer, that is a pointer to the
object the generated the call. A pointer to a base class may be
used to point to a dervied class. Using a base pointer to
access a derived class will only allow access to members of the
derived class which were imported from the base class.
It is possible to cast a base class pointer into a derived
class pointer to access the derived class member
functions. C++ allows a special pointer which points generically to a
public member of a class. C++ allows the use of a reference
parameter when invoking a function. It is possible to pass a reference
of an object into a function. A function may return a reference, which
allows a function to be used on the left side of an assignment
statement. It is possible to declare a reference to a variable, called
an independant reference. It is not possible to reference
another reference, obtain the address of a reference, create arrays of
references, create a pointer to a reference, or reference a bit-field.
A reference variable myst be initialized when it is declared unless it
is a member of a class, a function parameter, or a return
value.
Function overloading is the process of using the same name for two or
more functions. Each redefinition of a function must use either
different types of parameters or a different amount of parameters. A
constructor function may be overloaded. A function pointer may point
to an overloaded function but the declaration of the function pointer
may only match one and only one of the overloaded function's
declarations.
In C++ operators may be overloaded in order to
perform special operations relative to the class in which the
overloaded operators reside. It is possible to overload an operator
relative to a class by using a friend function. It is
possible to overload new and delete. It is possible to
overload [] () -> but these overloaded operators must be
non-static member functions and can not be friends. It is possible to
overload the comma operator.
A class that is inherited is called a
base class. The class which inherits is called the derived class. It
is possible for a derived class to inherit two or more base classes.
When an object of a derived clas is create, if the base class contains
a constructor, it will be called first, followed by the derived class
constructor. When a derived object is destroyed, its destructor is
called first followed by the base class destructor. In cases of
multiple inheritance: contructors are called in order of derivation,
destructors in reverse order of derivation. It is possible to pass
parameters to a base class constructor by using an expanded form of
the derived base class constructor declaration that passes along
arguments to one or more base class constructors.
A virtual function is a
function that is declared as virtual in a base class and is
redefined by a derived class. When a virtual function is inherited,
it's virtual nature is also inherited. When a derived class fails to
override a virtual function, the virtual function defined by the base
class is used. C++ supports the pure virtual function which is
a virtual function which has no definition in the base class. A class
which contains at least one pure virtual function is called
abstract. Virtual functions are used to achieve late binding.
Abstract classes support run-time polymorphism.
Polymorphism is the ability to have
one interface with multiple methods. C++ supports compile time and run
time polymorphism. Compile time polymorphism is achieved by
overloaded functions and operators. Run time polymorphism is
accomplished by inheritance and virtual functions. When a pointer to a
base object points to a derived object that contains a virtual
function, C++ determines which version of that function to call based
upon the type of object pointed to by the pointer, therefore when
different objects are pointed to, different versions of the virtual
function are invoked.
C++ defines its own object oriented I/O system. C++ I/O operates through
streams. C++ I/O allows the format of I/O operations. Special
functions called manipulators also allow the format of I/O operations.
The << and >> operators are overloaded in C++ to perform I/O
operations on built in types and on user created types. It is possible
to overload the C++ manipulator functions.
Obtaining a stream is a prerequisite to working with files.
Once a stream is created, the stream must be associated with a file.
To read/write to a text file simply requires using >> and << with a
stream
which is associated with a file. A byte may be read or written to
a file. A block of binary data may be also read and written to a file.
It is possible to detect the "end of file". Characters may be ignored in
the input stream. It is possible to obtain the next character in the
input stream without removing the character from the stream. It is
possible
to force the flushing of data from the output buffer to the file. It is
possible to perform random access on a file. The C++ I/O system also
maintains status information about each I/O operation. Because C++ file
streams are equivalent to console streams, it is possible to use the
same
overloaded inserter or extractor functions on file streams.
|