Pages

Thursday, November 15, 2012

INTRODUCYION TO C++


Getting Started
Introducing C++
C++ (pronounced "see plus plus") is a general-purpose, object-oriented, statically typed, free-form, multiparadigm
programming language supporting procedural programming, data abstraction, and generic
programming. During the 1990s, C++ became one of the most popular computer programming languages.
History
Bjarne Stroustrup from Bell Labs was the designer and original implementer of C++ (originally named "C with
Classes") during the 1980s as an enhancement to the C programming language. Enhancements started with the
addition of classes, followed by, among many features, virtual functions, operator overloading, multiple
inheritance, templates, and exception handling, these and other features are covered in detail along this book.
The C++ programming language is a standard recognized by the the ANSI (The American National Standards
Institute), BSI (The British Standards Institute), DIN (The German national standards organization), several other
national standards bodies, and was ratified in 1998 by the ISO (The International Standards Organization) as
ISO/IEC 14882:1998, the current version of which is the 2003 version, ISO/IEC 14882:2003.
The 1998 C++ Standard consists of two parts: the Core Language and the Standard Library; the latter includes
the Standard Template Library and the Standard C Library (ANSI C 89). Many C++ libraries exist which are not
part of the Standard, such as Boost. Also, non-Standard libraries written in C can generally be used by C++
programs.
Features introduced in C++ include declarations as statements, function-like casts, new/delete, bool, reference
types, const, inline functions, default arguments, function overloading, namespaces, classes (including all
class-related features such as inheritance, member functions, virtual functions, abstract classes, and
constructors), operator overloading, templates, the :: operator, exception handling, run-time type identification,
and more type checking in several cases. Comments starting with two slashes ("//") were originally part of
BCPL, and was reintroduced in C++. Several features of C++ were later adopted by C, including const,
inline, declarations in for loops, and C++-style comments (using the // symbol).
C++ source code example
// 'Hello World!' program
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}
Traditionally the first program people write in a new language is called "Hello, World." because all it does is print
the words Hello World. Hello World Explained offers a detailed explanation of this code; the included source
code is to give you an idea of a simple C++ program.
2 of 330
Overview
Before you begin your journey to understand how to write programs using C++, it is important to understand a
few key concepts that you may encounter. These concepts, while not unique to C++, help in understanding
programming in general. Readers who have experience in another programming language may wish to skim
through or skip this section entirely.
There are many different kinds of programs in use today. From the operating system you use that makes sure
everything works as it should, to the video games and music applications you use for fun, programs can fulfill
many different purposes. What all programs (also called software or applications) have in common is that they
all are made up of a sequence of instructions written in some form of programming language. These instructions
tell a computer what to do, and generally how to do it. Programs can contain anything from instructions to solve
math problems or send emails, to how to behave when a video game character is shot in a game. The computer
will follow the instructions of a program one instruction at a time from start to finish.
Why learn C++?
Why not? This is the most clarifying approach to the decission to learn anything, learning is always good,
selecting what to learn is more important as is how to prioritize tasks. Another side of this problem is that you
will be investing some time in getting a new skill set, you have to consider in what ways will this benefit you.
Check your objectives and compare similar projects or see what the programming market is in need. In any case
the more programming languages you know, the better.
If you are approaching the learning process only to add another notch under your belt, that is, willing only to
dedicate enough effort to understand its major quirks and learn something about its dark corners then you should
be best served in learning first two other languages, this will clarify what makes C++ special in its approach to
programming problems. You should select one imperative language and in this C will probably have a better
market value and will have a direct relation to C++ (a good substitute would be ASM) and the second language
should be an Object Oriented language like Java for the same reasons, as there is a close relation between the
tree languages.
If you are willing to dedicate a more than passing interest in C++ then you can even learn C++ as your first
language but dedicate some time understanding the different paradigms and why C++ is a multi-paradigm
language or how some like to call it a hybrid language.
Learning C is not a requirement for understanding C++, but knowing how to use an imperative language is, C++
will not make it easy for you to understand and distinguish some of this deeper concepts, since in C++ you are
free to implement solutions with a great range of freedom. Understanding what options to make will become the
cornerstone of mastering the language.
You should not learn C++ if you are only interested in applying or learning about Object Oriented Programing
since the nomenclature used and some of the approaches C++ takes to the problem will probably increase the
difficulty level in learning and mastering those concepts, if you are truly interested in Object Oriented
programming, the best language for that is Smalltalk.
As with all languages C++ has a specific scope of application, where it can truly shine, and if we take a quick
comparison with the previous mentioned languages, C++ is harder to learn than C and Java but more powerful
than both. C++ enables you to abstract from the little things you have to deal with in C or other lower level
languages but will grant you a bigger control and responsibility than Java, but it will not provide the default
features you can obtain in similar higher level languages. You will have to search and examine several external
implementations of these features and freely select those that best serve your purposes or you may even have to
3 of 330
implement your own solution.
What is a Programming Language?
In the most basic terms, a "programming language" is a means of communication between a human being
(programmer) and a computer. A programmer uses this means of communication in order to give the computer
instructions. These instructions are called "programs".
Like the many languages we use to communicate with each other, there are many languages that a programmer
can use to communicate with a computer. Each language has its own set of words and rules, called semantics. If
you're going to write a program, you have to follow the semantics of the language you're writing in, or you won't
be understood.
Programming languages can basically be divided in to two categories: Low-Level and High-level, next we will
introduce you to these concepts and their relevance to C++.
Low-level Languages
There are two general types of low level "languages".
Machine code (also called binary) is the lowest form of a low-level language. Machine code consists of a string
of 0s and 1s, which combine to form meaningful instructions that computers can take action on. If you look at a
page of binary it becomes apparent why binary is never a practical choice for writing programs; what kind of
person would actually be able to remember what a bunch of strings of 1 and 0 mean ?
Assembly language (also called ASM), is just above machine code on the scale from low level to high level. It is
a human-readable translation of the machine language instructions the computer executes. For example, instead
of referring to processor instructions by their binary representation (0s and 1s), the programmer refers to those
instructions using a more memorable (mnemonic) form. These mnemonics are usually short collections of letters
that symbolize the action of the respective instruction, such as "ADD" for addition, and "MOV" for moving
values from one place to another.
NOTE:
Assembly language is processor specific. This means that a program written in assembly language will
not work on computers with different processor architectures.
You do not have to understand assembly language to program in C++, but it does help to have an idea of what's
going on "behind-the-scenes". Learning about assembly language will also allow you to have more control as a
programmer and help you in debugging and understanding code.
High-level Languages
Higher level languages partially solve the problem of abstraction to the hardware (CPU, co-processors, number
of registers etc...) by providing portability of code. High-level languages do more with less code, although there is
sometimes a loss in performance and less freedom for the programmer. They also attempt to use English
language words in a form which can be read and generally interpreted by the average person with little
experience in them. A program written in one of these languages is sometimes referred to as "human-readable
4 of 330
code". In general, more abstraction makes it easier for a language be learned. No programming language is
written in what one might call "plain English" though, (although BASIC comes close.) Because of this, the text of
a program is sometimes referred to as "code", or more specifically as "source code." This is discussed in more
detail in the C++ Programming Code Section of the book.
Keep in mind that this classification scheme is evolving. C++ is still considered a high-level language, but with
the appearance of newer languages (Java, C#, Ruby etc...), C++ is beginning to be grouped with lower level
languages like C.
Translating Programming Languages
Since a computer is only capable of understanding machine code, human-readable code must be either
interpreted or translated into machine code.
An interpreter is a program (often written in a lower level language) that interprets the instructions of a program
one instruction at a time into commands that are to be carried out by the interpreter as it happens. Typically each
instruction consists of one line of text or provides some other clear means of telling each instruction apart and
the program must be reinterpreted again each time the program is run.
A compiler is a program that translates the instruction of a program one instruction at a time into machine code.
The translation into machine code may involve splitting one instruction understood by the compiler into multiple
machine instructions. The instructions are only translated once and after that the machine can understand and
follow the instructions directly whenever it is instructed to do so. A complete examination is given on the C++
Programming Compiler Section of the book.
The words and statements used to instruct the computer may differ, but no matter what words and statements are
used, just about every programming language will include statements that will accomplish the following:
Input
Input is the act of getting information from a device such as a keyboard or mouse, or sometimes another
program.
Output
Output is the opposite of input; it gives information to the computer monitor or another device or program.
Math/Algorithm
All computer processors (the brain of the computer), have the ability to perform basic mathematical
computation, and every programming language has some way of telling it to do so.
Testing
Testing involves telling the computer to check for a certain condition and to do something when that
condition is true or false. Conditionals are one of the most important concepts in programming, and all
languages have some method of testing conditions.
Repetition
Perform some action repeatedly, usually with some variation.
An further examination is provided on the C++ Programming Statements Section of the book.
Believe it or not, that's pretty much all there is to it. Every program you've ever used, no matter how
complicated, is made up of functions that look more or less like these. Thus, one way to describe programming is
the process of breaking a large, complex task up into smaller and smaller subtasks until eventually the subtasks
5 of 330
are simple enough to be performed with one of these simple functions.
C++ is mostly compiled rather than interpreted (there are some C++ interpreters), and then "executed" later. As
complicated as this may seem, later you will see how easy it really is.
So as we have seen in the Introducing C++ Section, C++ evolved from C by adding some levels of abstraction
(so we can correctly state that C++ is of a higher level than C). We will learn the particulars of those differences
in the Programming Paradigms Section of the book and for some of you that already know some other languages
should look into Programming Languages Comparisons Section.
Programming Paradigms
A programming paradigm is a style or model of programming that affects the way programmers can design,
organize and write programs. A multiparadigm programming language allows programmers to choose from a
number of different programming paradigms. C++ is a multiparadigm programming language.
Procedural Programming
Procedural programming is a programming paradigm based upon the idea of a procedure call. Procedure calls
are modular and are bound by scope. A procedural program is composed of one or more modules. Each module
is composed of one or more subprograms. Modules may consist of procedures, functions, subroutines or
methods, depending on the programming language. Procedural programs may possibly have multiple levels or
scopes, with subprograms defined inside other subprograms. Each scope can contain names which cannot be
seen in outer scopes.
Procedural programming offers many benefits over simple sequential programming since procedural code:
is easier to read and more maintainable
is more flexible
facilitates the practice of good program design
allows modules to be reused in the form of code libraries.
Object-Oriented Programming
Object-oriented programming can be seen as an extension of procedural programming in which programs are
made up of collection of individual units called objects that have a distinct purpose and function with limited or
no dependencies on implementation. For example, a car is like an object; it gets you from point A to point B with
no need to know what type of engine the car uses or how the engine works. Object-oriented languages usually
provide a means of documenting what an object can and cannot do, like instructions for driving a car.
Objects and Classes
An object is composed of members and methods. The members (also called data members, characteristics,
attributes, or properties) describe the object. The methods generally describe the actions associated with a
particular object. Think of an object as a noun, its members as adjectives describing that noun, and its methods as
the verbs that can be performed by or on that noun.
For example, a sports car is an object. Some of its members might be its height, weight, acceleration, and speed.
An object's members just hold data about that object. Some of the methods of the sports car could be "drive",
"park", "race", etc. The methods really don't mean much unless associated with the sports car, and the same goes
6 of 330
for the members.
The blueprint that lets us build our sports car object is called a class. A class doesn't tell us how fast our sports
car goes, or what color it is, but it does tell us that our sports car will have a member representing speed and
color, and that they will be say, a number and a word, respectively. The class also lays out the methods for us,
telling the car how to park and drive, but these methods can't take any action with just the blueprint - they need
an object to have an effect.
Inheritance
This concept describes a relationship between two (or more) types, or classes, of objects in which one is said to
be a "subtype" or "child" of the other, as result the "child" object is said to inherit features of the parent,
allowing for shared functionality, this lets programmers re-use or reduce code and simplifies the development
and maintenance of software.
Inheritance is also commonly held to include subtyping, whereby one type of object is defined to be a more
specialized version of another type (see Liskov substitution principle), though non subtyping inheritance is also
possible.
Inheritance is typically expressed by describing classes of objects arranged in an inheritance hierarchy
reflecting common behavior.
For example, one might create a variable class "Mammal" with features such as eating, reproducing, etc.; then
define a subtype "Cat" that inherits those features without having to explicitly program them, while adding new
features like "chasing mice". This allows commonalities among different kinds of objects to be expressed once
and reused multiple times.
In C++ we can then have classes which are related to other classes (a class can be defined by means of an older,
pre-existing, class ). This leads to a situation in which a new class has all the functionality of the older class,
and additionally introduces its own specific functionality. Instead of composition, where a given class contains
another class, we mean here derivation, where a given class is another class.
This OOP property will be explained further when we talk about Classes (and Structures) inheritance in the
Classes Inheritance Section of the book.
If one wants to use more than one totally orthogonal hierarchy simultaneously, such as allowing "Cat" to inherit
from "Cartoon character" and "Pet" as well as "Mammal" we are using multiple inheritance.
Multiple Inheritance
Multiple inheritance is the process by which one class can inherit the properties of two or more classes (variously
known as its base classes, or parent classes, or ancestor classes, or super classes).
In some similar language, multiple inheritance is restricted in various ways to keep the language simple, such as
by allowing inheritance from only one real class and a number of "interfaces", or by completely disallowing
multiple inheritance. C++ places the full power of multiple inheritance in the hands of programmers, but it is
needed only rarely, and (as with most techniques) can complicate code if used inappropriately. Because of C++'s
approach to multiple inheritance, C++ has no need of separate language facilities for "interfaces"; C++'s classes
can do everything that interfaces do in some related languages.
Polymorphism
7 of 330
Polymorphism allows a single name to be reused for several related but different purposes. The purpose of
polymorphism is to allow one name to be used for a general class. Depending on the type of data, a specific
instance of the general case is executed.
The concept of polymorphism is wider. Polymorphism exists every time we use two functions that have the same
name, but differ in the implementation. They may also differ in their interface, e.g., by taking different
arguments. In that case the choice of which function to make is via overload resolution, and is performed at
compile time, so we refer to static polymorphism.
Dynamic polymorphism will be covered deeply in the Classes Section where we will address its use on redefining
the method in the derived class.
Generic Programming
Generic programming or polymorphism is a programming style that emphasizes techniques that allow one
value to take on different types as long as certain contracts such as subtypes and signature are kept. In simpler
terms generic programming is based in finding the most abstract representations of efficient algorithms.
Templates popularized the notion of generics. Templates allow code to be written without consideration of the
type with which it will eventually be used. Templates are defined in the Standard Template Library (STL), were
generic programming was introduced into C++.
Statically Typed
Typing refers to how a computer language handles its variables. As we will see in depth later, variables are
values that the program uses during execution. These values can change; they are variable, hence their name.
Static typing usually results in compiled code optimizations. Since the compiler knows the exact types that are
in use, it can produce machine code that is optimized for a limited set of types. In C++, variables need to be
defined before they are used so that compilers know what type they are.
Static typing usually finds type errors more reliably at compile time, increasing the reliability of compiled
programs. Simply put, it means that "A round peg won't fit in a square hole", so the compiler will report an error
when a type leads to ambiguity or incompatible usage. However, programmers disagree over how common type
errors are and what proportion of bugs that are written would be caught by static typing. Static typing advocates
believe programs are more reliable when they have been type checked, while dynamic typing advocates point to
distributed code that has proved reliable.
A statically typed system constrains the use of powerful language constructs more than it constrains less
powerful ones. This makes powerful constructs harder to use, and thus places the burden of choosing the "right
tool for the problem" on the shoulders of the programmer, who might otherwise be inclined to use the most
powerful tool available. Choosing overly powerful tools may cause additional performance, reliability or
correctness problems, because there are theoretical limits on the properties that can be expected from powerful
language constructs. For example, indiscriminate use of recursion or global variables may cause
well-documented adverse effects.
Static typing allows construction of libraries which are less likely to be accidentally misused by their users. This
can be used as an additional mechanism for communicating the intentions of the library developer.
Free-form
Free-form refers to how the programmer crafts the code. Basically, there are no rules on how you choose to
8 of 330
write your program, save for the semantic rules of C++. Any C++ program should compile as long as it is legal
C++.
The free-form nature of C++ is used (or abused, depending on your point of view) by some programmers in
crafting obfuscated code, which is code that is purposefully written to be difficult to understand. However,
complicated programming is also a security device, ensuring that the source code is harder to duplicate, short of
using the whole program exactly how it was originally written.
Language Comparisons
There isn't a perfect language. It all depends on the tools and the objective. The optimal language (in terms of
run-time performance) is machine code but machine code (binary) is the least efficient programming language in
terms of coder time. The complexity of writing large systems is enormous with high-level languages, and beyond
human capabilities with machine code. In the next section C++ will be compared with other closely related
languages like C, Java, C# and C++/CLI.
The quote above is shown to indicate that no programming language at present can translate directly concepts or
ideas into useful code, there are solutions that will help. We will cover the use of Computer-aided software
engineering (CASE) tools that will address part of this problem but its use does require planing and some degree
of complexity.
The intention of these sections is not to promote one language above another; each has its applicability. Some are
better in specific tasks, some are simpler to learn, others only provide a better level of control to the programmer.
This all may depend also on the level of control the programmer has of a given language.
Garbage Collection
In C++ garbage collection is optional rather than required. In the Garbage Collection Section of this book we will
cover this issue deeply.
Why doesn't C++ include a finally keyword?
As we will see in the Resource Acquisition Is Initialization (RAII) Section of the book, RAII can be used to
provide a better solution for most issues. When finally is used to clean up, it has to be written by the clients of
a class each time that class is used (for example, clients of a File class have to do I/O in a try/catch/finally
block so that they can guarantee that the File is closed). With RAII, the destructor of the File class can make
that guarantee. Now the cleanup code has to be coded only once — in the destructor of File; the users of the
class don't need to do anything.
Mixing Languages
C 89/99
C was essentially the core language of C++ when Bjarne Stroustrup, decided to create a "better C". Many of the
syntax conventions and rules still hold true and so we can even state that C was a subset of C++, most recent
C++ compilers will also compile C code taking into consideration the small incompatibilities, since C99 and C++
2003 are not compatible any more. You can also check more information about the C language on the C
Programming Wikibook ( http://en.wikibooks.org/wiki/C_Programming ).
9 of 330
C++ as defined by the ANSI standard in 98 (called C++98 at times) is very nearly, but not quite, a superset of
the C language as it was defined by its first ANSI standard in 1989 (known as C89). There are a number of ways
in which C++ is not a strict superset, in the sense that not all valid C89 programs are valid C++ programs, but the
process of converting C code to valid C++ code is fairly trivial (avoiding reserved words, getting around the
stricter C++ type checking with casts, declaring every called function, and so on).
In 1999, C was revised and many new features were added to it. As of 2004, most of these new "C99" features
are not there in C++. Some (including Stroustrup himself) have argued that the changes brought about in C99
have a philosophy distinct from what C++98 adds to C89, and hence these C99 changes are directed towards
increasing incompatibility between C and C++.
The merging of the languages seems a dead issue as coordinated actions by the C and C++ standards committees
leading to a practical results didn't happen and it can be said that the languages started even to diverge.
Some of the differences are:
C++ supports function overloading (absent in C89, allowed only for some standard library code in C99).
C++ supports inheritance and polymorphism.
C++ adds keyword class, but keeps struct from C, with compatible semantics.
C++ supports access control for class members.
C++ supports generic programming through the use of templates.
C++ extends the C89 standard library with its own standard library.
C++ and C99 offer different complex number facilities.
C++ has bool and wchar_t as primitive types, while typedefs in C.
C++ comparison operators return bool, while C returns int.
C++ supports overloading of operators.
C++ character constants have type char, while C character constants have type int.
C++ has additional cast operators (static_cast, dynamic_cast, const_cast and reinterpret_cast).
C++ adds mutable keyword to address the imperfect match between physical and logical constness.
C++ extends the type system with references.
C++ supports member functions, constructors and destructors for user-defined types to establish
invariants and to manage resources.
C++ supports runtime type identification (RTTI), via typeid and dynamic_cast.
C++ includes exception handling.
C++ has std::vector as part of its standard library instead of variable-length arrays as in C.
C++ treats sizeof operator as compile time operation, while C allows it be a runtime operation.
C++ has new and delete operators, while C uses malloc and free library functions exclusively.
C++ supports object-oriented programming without extensions.
C++ does not require use of macros and careful information-hiding and abstraction for code portability.
Java
This is a comparison of the Java programming language with the C++ programming language. C++ and Java
share many common traits. You can get a better understanding of Java in the Java Programming WikiBook.
Java was created initially to support network computing on embedded systems. Java was designed to be
extremely portable, secure, multi-threaded and distributed, none of which were design goals for C++. The syntax
of Java was chosen to be familiar to C programmers, but direct compatibility with C was not maintained. Java
also was specifically designed to be simpler than C++ but it keeps evolving above that simplification.


No comments:

Post a Comment

 

Blogger news

Blogroll

About