C++ Chapter1
C++ Chapter1
Software Evolution
Ernest Tello, a well-known writer in the field of artificial intelligence, compared
the evolution of software technology to the growth of a tree. Like a tree, the
software evolution has had distinct phase or “layer” of growth. These layers
were built up one by one over the last five decades with each layer representing
an improvement over the previous one.
Since the invention of the computer, may programming approaches have been
tried. These include techniques such as modular programming, top-down
programming, bottom-up programming and structured programming. The
primary motivation in each has been the concern to handle the increasing
complexity of programs that are reliable and maintainable. These techniques
have become popular among programmers over the last two decades.
What is C++?
The predecessor of C++ is C, which was designed by Dennis Richie at Bell
Labs and first released in 1973. C is a widely used language and was used to
write the early versions of Unix and Windows. Indeed, the libraries and
software-development libraries of many operating systems are still written to
have C interfaces. C is powerful because it can be used to write code that is
compiled to a compact form, it uses a static type system (so the compiler does
the work of type checking), and the types and structures of the language allow
for direct memory access to computer architecture.
Object-Oriented Language
Object-Oriented programming is not the right any particular language. Like
structured programming, OOP concept can be implemented using the language
such as C and Pascal.
The language should support several of the OOP concepts to claims that they
are object-oriented. Depending upon the feature they support, they can be
classified into the following two categories :
1. Object-based Programming language and
2. Object- oriented programming languages
Objects
Objects are the basic run-time entities in an object-oriented system. They may
represent a person, place, a bank account , a table of data or any items that rhe
program has to handle. They may also represent user-defined data such as
vectors, time and lists. Programming problem is analyzed in terms of objects
and the nature of communication between them. Program objects should be
chosen such that they match closely with the real-world objects.
When a program is executed, the object interact by sending messages to one
another.
Example – If “customer” and “account” are two objects in a program, then the
customer object mat send a message to the account object requesting for the
bank balance. Each object contains data, and code to manipulate the data.
Objects can interact without having to know the details of each other’s data or
code.
Classes
The entire set of data and code of an object can be made a user-defined data
types with the help of a class. In-fact, objects are variable of the type class.
Once a class has been defined, we can create any number of objects belonging
to that class. Each object is associated with the data of type class with which
they are created. A class is thus a collection of objects of similar type.
Why Use a Language Like C++?
At its core, a computer is just a processor with some memory, capable of
running tiny instructions like “store 5 in memory location 23459.” Why would
we express a program as a text file in a programming language, instead of
writing processor instructions?
The advantages:
1. Conciseness: programming languages allow us to express common sequences
of commands more concisely. C++ provides some especially powerful
shorthands.
2. Maintainability: modifying code is easier when it entails just a few text edits,
instead of rearranging hundreds of processor instructions. C++ is object oriented
which further improves maintainability.
3. Portability: different processors make different instructions available.
Programs written as text can be translated into instructions for many different
processors; one of C++’s strengths is that it can be used to write programs for
nearly any processor. C++ is a high-level language: when you write a program
in it. C++ does give access to some lower-level functionality than other
languages (e.g. memory addresses)
Source File Object File Compiler Source File Object File Compiler Executable
Linker Libraries Program in Memory OS Object files are intermediate files that
represent an incomplete copy of the program: each source file only expresses a
piece of the program, so when it is compiled into an object file, the object file
has some markers indicating which missing pieces it depends on.
The linker takes those object files and the compiled libraries of predefined code
that they rely on, fills in all the gaps, and spits out the final program, which can
then be run by the operating system (OS).
The compiler and linker are just regular programs. The step in the compilation
process in which the compiler reads the file is called parsing. In C++, all these
steps are performed ahead of time, before you start running a program. In some
languages, they are done during the execution process, which takes time. This is
one of the reasons C++ code runs far faster than code in many more recent
languages. C++ actually adds an extra step to the compilation process: the code
is run through a preprocessor, which applies some modifications to the source
code, before being fed to the compiler. Thus, the modified diagram is:
Basic Program :-
// A Hello World program
# include < iostream >
int main ()
{
std :: cout << " Hello , world !\ n ";
return 0;
}
Explanation :-
1. // indicates that everything following it until the end of the line is a comment:
it is ignored by the compiler. Another way to write a comment is to put it
between /* and */ (e.g. x = 1 + /*sneaky comment here*/ 1;). A comment of this
form may span multiple lines. Comments exist to explain non-obvious things
going on in the code. Use them: document your code well!
2. Lines beginning with # are preprocessor commands, which usually change
what code is actually being compiled. #include tells the preprocessor to dump in
the contents of another file, here the iostream file, which defines the procedures
for input/output.
3. int main() {...} defines the code that should execute when the program starts
up. The curly braces represent grouping of multiple commands into a block.
More about this syntax in the next few lectures.
4. • cout << : This is the syntax for outputting some piece of text to the screen.
• Namespaces: In C++, identifiers can be defined within a context – sort of a
directory of names – called a namespace. When we want to access an identifier
defined in a namespace, we tell the compiler to look for it in that namespace
using the scope resolution operator (::).
Here, we’re telling the compiler to look for cout in the std namespace, in which
many standard C++ identifiers are defined. A cleaner alternative is to add the
following line below line 2:
using namespace std ;
This line tells the compiler that it should look in the std namespace for any
identifier we haven’t defined. If we do this, we can omit the std:: prefix when
writing cout.
• Strings: A sequence of characters such as Hello, world is known as a string. A
string that is specified explicitly in a program is a string literal.
• Escape sequences: The \n indicates a newline character. It is an example of an
escape sequence – a symbol used to represent a special character in a text literal.
Escape Sequence Represented Character
\a System bell (Beep Sound)
\b Backspace
\f Formeed (page Break)
\n Newline (line Break)
\r “Carriage return” (returns cursor to
start of line)
\t Tab
\\ Backslash
\’ Single quote character
\” Double quote character
\some integer x The character represented by x
5. return 0 indicates that the program should tell the operating system it has
completed successfully. This syntax will be explained in the context of
functions; for now, just include it as the last line in the main block.
Tokens
Tokens are the minimals chunk of program that have meaning to the compiler –
the smallest meaningful symbols in the language. Our code displays all 6 kinds
of tokens, though the usual use of operators is not present here:
Token Type Description/Purpose Examples
Keywords Words with special Int, double, for, auto
meaning to the compiler
Identifiers Names of things that are Cout, std, x, myfunction
not built into the
language
Literals Basic constant values “Hello, world!”, 24.3, 0,
whose value is specified ‘c’
directly in the source
code
Operators Mathematical or logical +, -, &&, %, <<
operation
Punctuations/Seperators Punctuation defining the {} () , ;
structure of a program
Whitespace Spaces of various sorts; Spaces, Tabs, newlines,
ignored by the compiler comments
Unsigned : 0 to
4294967295
Bool Boolean ( 1 bytes Just true (1) or
true/false). false (0)
Indicated with the
keywords true
and false
Double “doubly” precise 8 bytes +/-1.7e+/-308(15
floating point digit)
number.
Debugging
There are two kinds of errors you’ll run into when writing C++ programs:
compilation errors and runtime errors. Compilation errors are problems raised
by the compiler, generally resulting from violations of the syntax rules or
misuse of types. These are often caused by typos and the like. Runtime errors
are problems that you only spot when you run the program: you did specify a
legal program, but it doesn’t do what you wanted it to. These are usually more
tricky to catch, since the compiler won’t tell you about them.
Benefits of OOP
OOP offers several benefits to both the program designer and the user. Object
Orientation contributes to the solution of many problems associated with the
development and quality of software products. The new technology promises
greater programmer productivity, better quality of software and lesser
maintenance cost. The principal advantages are:
Through inheritance, we can eliminate redundant code and extend the use
of existing classes.
We can build programs from the standard working modules that
communicate with one another, rather than having to start writing the
code from scratch. This leads to saving of development time and higher
productivity.
The principle of data hiding helps the programmer to build secure
programs that can not be invaded by code in other parts of the program.
It is possible to have multiple instances of an object to co-exist without
any interference.
It is possible to have multiple instances of an object to co-exist without
any interference.
Software complexity can be easily managed.
Object oriented techniques can be easily upgraded from small to large
systems.