GENERALITY
GENERALITY
An overview of C++
C++ is the object oriented extension of C. As for C there is an ANSI/ISO standard ( final draft 1998) for the C++ programming language. This will ensure that the C++ code is portable between
computers.
The C++ programming language teach here is the Standard C++. This is the version of C++ created by the ANSI/ISO 2 standardisation committee. The Standard C++ contains several enhancements not
found in the traditional C++. Thus, Standard C++ is a superset of traditional C++.
Standard C++ is the one that is currently accepted by all major compilers. Therefore, you can be confident that what you learn here will also apply in the future.
However, if you are using an older compiler it might not support one or more of the features that are specific to Standard C++. This is important because two recent additions to the C++ language affect
every program you will write. If you are using an older compiler that does not accept these knew features, don’t worry. There is an easy workaround, as you will in a later paragraph.
Since C++ was invented to support object-oriented programming. OOP concepts will be reminded. As you will see, many features of C++ are related to OOP in a way or another. In fact the theory of
OOP permeates C++. However, it is important to understand that C++ can be used to write programs that are and are not object oriented. How you use C++ is completely up to you.
A few comments about the nature and form of C++ are in order. For most part C++ programs look like C programs. Like a C program, a C++ program begins execution at main( ). To include
command-line arguments, C++ uses the same argc, argv convention that C uses. Although C++ defines its own, object-oriented library. It also supports all the functions in the C standard library. C++
uses the same control structures as C. C++ includes all the build-in data types defined by C programming.
Encapsulation
Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps them both safe from outside. In an object-oriented language, code and data can be combined in such a
way that a self-contained ‘black box’ is created. When code and data are link together in this fashion , an object is created:
Within an object, code, data, or both may be private to that object or public.
Private code or data is known to and accessible only by another part of the object (i.e. cannot be accessed by a piece of the program that exists outside the object.
Public code or data can be accessed by other parts of the program even though it is defined within an object.
Public parts of an object are used to provide a controlled interface to the private elements of the object.
Data 2 ANSI: American National Standards Institute ISO: International Standard Organisation
Methods: code OBJECT Standard C++ programming 4
object, you are creating a new data type. Each specific instance of this data type is a compound
variable.
Polymorphism
Polymorphism is the quality that allows one name to be used for two or more related but technically different
purposes.
Polymorphism allows one name to specify a general class of actions. Within a general class of actions, the
specific action to be applied is determined by the type of data. For example, in C, the absolute value action
requires three distinct function names: abs( ) for integer, labs( ) for long integer, and fabs( ) for floating-point
value. However in C++, each function can be called by the same name, such as abs( ). The type of data used to
call the function determines which specific version of the function is actually executed.
In C++ it is possible to use one function name for many different purposes. This type of polymorphism is called
function overloading.
Polymorphism can also be applied to operators. In that case it is called operator overloading.
More generally the concept of polymorphism is characterised by the idea ‘one interface, multiple methods’.
The key point to remember about polymorphism is that it allows you to handle greater complexity by allowing
the creation of standard interfaces to related activities.
Inheritance
Inheritance is the process by which one object can acquire the properties of another. An object can inherit a
general set of properties to which it can add those features that are specific only to itself.
Inheritance is important because it allows an object to support the concept of hierarchical classification. Most
information is made manageable by hierarchical classification.
The child class inherits all those qualities associated with the parent and adds to them its own defining
characteristics.
First, in C, when a function takes no parameters, its prototype has the word void inside its function parameter
list. For example if a function f1( ) takes no parameters (and returns a char), its prototype will look like this:
char f1(void); /* C version */
In C++, the void is optional. Therefore the prototype for f1( ) is usually written as:
char f1( ); //C++ version
this means that the function has no parameters. The use of void in C++ is not illegal; it is just redundant.
Remember these two declarations are equivalent.
Another difference between C and C++ is that in a C++ program, all functions must be prototyped. Remember
in C prototypes are recommended but technically optional. As an example from the previous section show, a
member function’s prototype contained in a class also serves as its general prototype, and no other separate
prototype is required.
A third difference between C and C++ is that in C++, if a function is declared as returning a value, it must return
a value. That is, if a function has a return type other than void, any return statement within the function must
contain a value. In C, a non void function is not required to actually return a value. If it doesn’t, a garbage value
is ‘returned’.
In C++, you must explicitly declare the return type of all functions.
Another difference is that in C, local variables can be declared only at the start of a block, prior to any ‘action’
statement. In C++, local variables can be declared anywhere. Thus, local variables can be declared close to
where they are first use to prevent unwanted side effects.
C++ defines the bool date type, which is used to store Boolean values. C++ also defines the keywords true and
false, which are the only values that a value of type bool can have.
In C, it is not an error to declare a global variable several times, even though it is bad programming practice. In
C++, this is an error.
In C an identifier will have at least 31 significant characters. In C++, all characters are considered significant.
However, from practical point of view, extremely long identifiers are unwieldy and seldom needed.
In C, you can call main( ) from within the program. In C++, this is not allowed..
In C, you cannot take the address of a register variable. In C++, you can.
New headers
Since C++ is build on C, the skeleton should be familiar, but pay attention to the #include statement. This
statement includes the file iostream.h, which provides support for C++’s I/O system. It is to C++ what stdio.h is
to C.
The only difference is that in C or traditional C++, the #include statement includes a file (file-name.h). While
the Standard C++ do not specify filenames. Instead the new style headers simply specify standard identifiers
that might be map to files by the compiler, but they need not be. New headers are abstractions that simply
guaranty that the appropriate prototypes and definitions required by the C++ library have been declared.
Since the new-style header is not a filename, it does not have a .h extension. Such header consists only of the
header name between angle brackets:
< iostream >
< fstream >
< vector >
< string >
Standard C++ supports the entire C function library, it still supports the C-style header files associated with the
library. That is, header files such as stdio.h and ctype.h are still available. However Standard C++ also defines
new-style headers that you can use in place of these header files. For example,
Namespace
When you include a new-style header in your program, the contents of that header are contained in the std
namespace. The namespace is simply a declarative region. The purpose of a namespace is to localise the
names of identifiers to avoid name collision. Traditionally, the names of library functions and other such items
were simply placed into the global namespace (as they are in C). However, the contents of new-style headers
are place in the std namespace. Using the statement,
using namespace std;
brings the std namespace into visibility. After this statement has been compiled, there is no difference working
with an old-style header and a new-style one.
replace: by:
#include < iostream> #include < iostream.h >