These lecture notes are designed for an
introductory course on programming, using the imperative core of C++,
and given to beginners to Advance level programers
For more info
For more info
1. Introducing C++
1.2 The Origins of C++
C++ was developed by Bjarne Stroustrup of AT&T Bell Laboratories
in the early 1980's, and is based on the C language. The name is a pun -
"++" is a syntactic construct used in C (to increment a variable), and
C++ is intended as an incremental improvement of C. Most of C is a
subset of C++, so that most C programs can be compiled (i.e. converted
into a series of low-level instructions that the computer can execute
directly) using a C++ compiler.
C is in many ways hard to categorise. Compared to assembly language
it is high-level, but it nevertheless includes many low-level facilities
to directly manipulate the computer's memory. It is therefore an
excellent language for writing efficient "systems" programs. But for
other types of programs, C code can be hard to understand, and C
programs can therefore be particularly prone to certain types of error.
The extra object-oriented facilities in C++ are partly included to
overcome these shortcomings.
1.3The C++ Programming Environment
The best way to learn a programming language
is to try writing programs and test them on a compter! To do this, we
need several pieces of software:
- An editor with which to write and modify the C++ program components or source code,
- A compiler with which to convert the source code into machine instructions which can be executed by the computer directly,
- A linking program with which to link the compiled program components with each other and with a selection of routines from existing libraries of computer code, in order to form the complete machine-executable object program,
- A debugger to help diagnose problems, either in compiling programs in the first place, or if the object program runs but gives unintended results.
1.4 An Example C++ Program
Now here i will give you an example of c++ program and then i will explain the context of program.
#include <iostream>
using namespace std;
int main()
{
int year_now, age_now, another_year, another_age;
cout << "Enter the current year then press RETURN.\n";
cin >> year_now;
cout << "Enter your current age in years.\n";
cin >> age_now;
cout << "Enter the year for which you wish to know your age.\n";
cin >> another_year;
another_age = another_year - (year_now - age_now);
if (another_age >= 0) {
cout << "Your age in " << another_year << ": ";
cout << another_age << "\n";
} else {
cout << "You weren't even born in ";
cout << another_year << "!\n";
}
return 0;
}
This program illustrates several general features of all C++ programs. It
begins (after the comment lines) with the statement
#include <iostream>
This statement is called an include directive. It tells the
compiler and the linker that the program will need to be linked to a
library of routines that handle input from the keyboard and output to
the screen (specifically the cin and cout statements
that appear later). The header file "iostream" contains basic
information about this library. You will learn much more about
libraries of code later in this course. After the include directive is the line:
using namespace std;
This statement is called a using directive. The latest versions
of the C++ standard divide names (e.g. cin and cout)
into subcollections of names called namespaces. This particular
using directive says the program will be using names that have
a meaning defined for them in the std namespace (in this case
the iostream header defines meanings for cout and
cin in the std namespace).Some older C++ compilers do not support namespaces. In this case you can use the older form of the include directive (that does not require a using directive, and places all names in a single global namespace):
#include <iostream.h>
Some of the
legacy code you encounter in industry may be written using this older
style for headers.Because the program is short, it is easily packaged up into a single list of program statements and commands. After the include and using directives, the basic structure of the program is:
int main()
{
First statement;
...
...
Last statement;
return 0;
}
All C++ programs have this basic "top-level" structure. Notice
that each statement in the body of the program ends with a
semicolon. In a well-designed large program, many of these statements
will include references or calls to sub-programs, listed after the
main program or in a separate file. These sub-programs have roughly
the same outline structure as the program here, but there is always
exactly one such structure called main. Again, you will learn
more about sub-programs later in the course. When at the end of the main program, the line
return 0;
means "return the value 0 to the computer's operating system to
signal that the program has completed successfully". More generally, return statements
signal that the particular sub-program has finished, and return a
value, along with the flow of control, to the program level above. More
about this later.Our example program uses four variables:
year_now,
age_now,
another_year and
another_age
Program variables are not like variables in mathematics. They are
more like symbolic names for "pockets of computer memory" which can be
used to store different values at different times during the program
execution. These variables are first introduced in our program in the
variable declarationint year_now, age_now, another_year, another_age;which signals to the compiler that it should set aside enough memory to store four variables of type "int" (integer) during the rest of the program execution. Hence variables should always be declared before being used in a program. Indeed, it is considered good style and practice to declare all the variables to be used in a program or sub-program at the beginning. Variables can be one of several different types in C++, and we will discuss variables and types at some length later.
1.5 Simple Flow of Control
The last few lines of our example program (other than "return 0") are: if (another_age >= 0) {
cout << "Your age in " << another_year << ": ";
cout << another_age << "\n";
}
else {
cout << "You weren't even born in ";
cout << another_year << "!\n";
}
The "if ... else ..." branching mechanism is a familiar construct in
many procedural programming languages. In C++, it is simply called an if statement, and the general syntax is if (condition) {
Statement1;
...
...
StatementN;
} else {
StatementN+1;
...
...
StatementN+M;
}
The "else" part of an "if statement" may be omitted, and furthermore, if there is just one Statement after the "if (condition)", it may be simply written asif (condition) Statement;
It is quite common to find "if statements" strung together in programs, as follows:
...
...
if (total_test_score < 50)
cout << "You are a failure. You must study much harder.\n";
else if (total_test_score < 65)
cout << "You have just scraped through the test.\n";
else if (total_test_score < 80)
cout << "You have done quite well.\n";
else if (total_test_score < 95)
cout << "Your score is excellent. Well done.\n";
else {
cout << "You cheated!\n";
total_test_score = 0;
}
...
...
This program fragment has quite a complicated logical
structure, but we can confirm that it is legal in C++ by referring to
the syntax diagram for "if statements". In such diagrams, the terms
enclosed in ovals or circles refer to program components that
literally appear in programs. Terms enclosed in boxes refer to program
components that require further definition, perhaps with another
syntax diagram. A collection of such diagrams can serve as a formal
definition of a programming language's syntax (although they do not
help distinguish between good and bad programming style!).Below is the syntax diagram for an "if statement". It is best understood in conjunction with the syntax diagram for a "statement". In particular, notice that the diagram doesn't explicitly include the ";" or "{}" delimiters, since these are built into the definition (syntax diagram) of "statement".
1.5.1 Syntax diagram for an If Statement
...
...
if (total_test_score < 50)
cout << "You are a failure. You must study much harder.\n";
else if (total_test_score < 65)
cout << "You have just scraped through the test.\n";
else if (total_test_score < 80)
cout << "You have done quite well.\n";
else if (total_test_score < 95)
cout << "Your score is excellent. Well done.\n";
else {
cout << "You cheated!\n";
total_test_score = 0;
}
...
...
as the single statement which must follow the first else.2. Variables, Types and Expressions
As we have seen, C++ programs can be
written using many English words. It is useful to think of words found in a
program as being one of three types:
- Reserved Words. These are words such as if, int and else, which have a predefined meaning that cannot be changed.
- Library Identifiers. These words are supplied default meanings by the programming environment, and should only have their meanings changed if the programmer has strong reasons for doing so. Examples are cin, cout and sqrt (square root).
- Programmer-supplied Identifiers. These words are "created" by the programmer, and are typically variable names, such as year_now and another_age.
An identifier cannot be any sequence
of symbols. A valid identifier must start with a letter of the alphabet or an
underscore ("_")
and must consist only of letters, digits, and underscores.
2.2 Data Types
Integers
C++ requires that all variables used in a program be given a data type. We have already seen the data type int. Variables of this type are used to represent integers (whole numbers). Declaring a variable to be of type int signals to the compiler that it must associate enough memory with the variable's identifier to store an integer value or integer values as the program executes. But there is a (system dependent) limit on the largest and smallest integers that can be stored. Hence C++ also supports the data types short int and long int which represent, respectively, a smaller and a larger range of integer values than int. Adding the prefix unsigned to any of these types means that you wish to represent non-negative integers only. For example, the declarationunsigned short int year_now, age_now, another_year, another_age;reserves memory for representing four relatively small non-negative integers.
Some rules have to be observed when writing integer values in programs:
- Decimal points cannot be used; although 26 and 26.0 have the same value, "26.0" is not of type "int".
- Commas cannot be used in integers, so that (for example) 23,897 has to be written as "23897".
- Integers cannot be written with leading zeros. The compiler will, for example, interpret "011" as an octal (base 8) number, with value 9.
Real numbers
Variables of type "float" are used to store real numbers. Plus and minus signs for data of type "float" are treated exactly as with integers, and trailing zeros to the right of the decimal point are ignored. Hence "+523.5", "523.5" and "523.500" all represent the same value. The computer also accepts real numbers in floating-point form (or "scientific notation"). Hence 523.5 could be written as "5.235e+02" (i.e. 5.235 x 10 x 10), and -0.0034 as "-3.4e-03". In addition to "float", C++ supports the types "double" and "long double", which give increasingly precise representation of real numbers, but at the cost of more computer memory.
Type Casting
Sometimes it is important to guarantee that a value is stored as a real number, even if it is in fact a whole number. A common example is where an arithmetic expression involves division. When applied to two values of type int, the division operator "/" signifies integer division, so that (for example) 7/2 evaluates to 3. In this case, if we want an answer of 3.5, we can simply add a decimal point and zero to one or both numbers - "7.0/2", "7/2.0" and "7.0/2.0" all give the desired result. However, if both the numerator and the divisor are variables, this trick is not possible. Instead, we have to use a type cast. For example, we can convert "7" to a value of type double using the expression "static_cast<double>(7)". Hence in the expression
answer = static_cast<double>(numerator) / denominatorthe "/" will always be interpreted as real-number division, even when both "numerator" and "denominator" have integer values. Other type names can also be used for type casting. For example, "static_cast<int>(14.35)" has an integer value of 14.
Characters
Variables of type "char" are used to store character data. In standard C++, data of type "char" can only be a single character (which could be a blank space). These characters come from an available character set which can differ from computer to computer. However, it always includes upper and lower case letters of the alphabet, the digits 0, ... , 9, and some special symbols such as #, £, !, +, -, etc. Perhaps the most common collection of characters is the ASCII character set.
Character constants of type "char" must be enclosed in single quotation marks when used in a program, otherwise they will be misinterpreted and may cause a compilation error or unexpected program behaviour. For example, "'A'" is a character constant, but "A" will be interpreted as a program variable. Similarly, "'9'" is a character, but "9" is an integer.
There is, however, an important (and perhaps somewhat confusing) technical point concerning data of type "char". Characters are represented as integers inside the computer. Hence the data type "char" is simply a subset of the data type "int". We can even do arithmetic with characters. For example, the following expression is evaluated as true on any computer using the ASCII character set:
'9' - '0' == 57 - 48 == 9
The ASCII code for the character '9' is decimal 57 (hexadecimal 39) and the ASCII code for the character '0' is decimal 48 (hexadecimal 30) so this equation is stating that
57(dec) - 48(dec) == 39(hex) - 30(hex) == 9
It is often regarded as better to use the ASCII codes in their hexadecimal form.
However, declaring a variable to be of type "char" rather than type "int" makes an important difference as regards the type of input the program expects, and the format of the output it produces. For example, the program
#include <iostream>
using namespace std;
int main()
{
int number;
char character;
cout << "Type in a character:\n";
cin >> character;
number = character;
cout << "The character '" << character;
cout << "' is represented as the number ";
cout << number << " in the computer.\n";
return 0;
}
0 comments:
Post a Comment