C++ 2
C++ 2
C++ Basics
– Structure of C++ Program
A C++ program has the following structure
[Comments]
[Preprocessor directives]
[Global variable declarations]
[Prototypes of functions]
[Definitions of functions]
1
ASTU
– C++ IDE
• The complete development cycle in C++ is:
Write the program,
compile the source code,
link the program,
and run it.
Writing a Program
To write a source code, your compiler may have its own built-in
text editor, or you may be using a commercial text editor or
word processor that can produce text file.
The important thing is that whatever you write your program
in, it must save simple, plain-text files, with no word processing
commands embedded in the text.
2
ASTU
3
ASTU
Compiling
Your source code file can't be executed, or run, as
a program can.
To turn your source code into a program, you use
a compiler.
In Borland’s turbo C++ you pick the RUN menu
command or type
4
ASTU
Tc <filename>
From the command line, where <filename> is the
name of your source code file (for example, tex-
t.cpp).
Other compilers may do things slightly differently
After your source is compiled an object file is
produced. This file is often named with the
extension .OBJ.
this is still not an executable program, however. To
turn this into an executable program, you must run
your linker.
5
ASTU
Linking
C++ programs are typically created by linking
together one or more OBJ files with one or more libraries.
A library is a collection of linkable files that were
supplied with your compiler
All C++ compilers come with a library of useful
functions (or procedures) and classes that you can
include in your program.
A function is a block of code that performs a service,
such as adding two numbers or printing to the screen.
A class is a collection of data and related functions.
6
Summary
ASTU
7
Showing Sample program
ASTU
9
ASTU
The final two characters, \n, tell cout to put a new line af-
ter the words Hello World!
All ANSI-compliant programs declare main() to return an
int. This value is "returned" to the operating system
when your program completes. Some programmers signal
an error by returning the value 1.
The main() function ends on line 7 with the
closing brace.
13
Basic Elements
ASTU
14
ASTU
wchar_t
15
Identifiers
ASTU
17
ASTU
18
Literals
ASTU
Literals are constant values which can be a number, a character
of a string.
For example the number 129.005, the character ‘A’ and the string
“hello world” are all literals.
There is no identifier that identifies them.
Comments
A comment is a piece of descriptive text which explains some
aspect of a program.
Program comments are totally ignored by the compiler and are
only intended for human readers.
C++ provides two types of comment
Anything after // (until the end of the line on which it appears) is
considered a comment.
Anything enclosed by the pair /* and */ is considered a comment.
19
Data Types, Variables, and Constants
ASTU
Variables
A variable is a symbolic name for a memory location in which
data can be stored and subsequently recalled.
Variables are used for holding data values so that they can be
utilized in various computations in a program. All variables
have two important attributes:
A type, which is, established when the variable is defined
(e.g., integer, float, character). Once defined, the type of a C++
variable cannot be changed.
A value, which can be changed by assigning a new value to the
variable. The kind of values a variable can assume depends on
its type. For example, an integer variable can only take integer
values (e.g., 2, 100, -12) not real numbers like 0.123.
20
Variable Declaration
ASTU
21
ASTU
Legal variable names include x, J23qrsnf, and myAge.
Good variable names tell you what the variables are for;
using good names makes it easier to understand the flow of
your program.
The following statement defines an integer variable called
myAge:
int myAge;
IMPORTANT- Variables must be declared before used!
As a general programming practice, avoid such horrific
names as J23qrsnf, and restrict single-letter variable names
C++ is case-sensitive. In other words, uppercase and lower-
case letters are considered to be different. A variable named
age is different from Age, which is different from AGE.
22
Creating More Than One Variable at a Time
ASTU
23
Assigning Values to Your Variables
ASTU
24
ASTU
26
ASTU
27
ASTU
28
The data types used in C++ programs ASTU
30
A demonstration of the use of variables.
ASTU
#include <iostream.h>
int main()
{
unsigned short int Width = 5,Length;
Length = 10;
unsigned short int Area = Width * Length;
cout << "Width:" << Width << "\n";
cout << "Length: " << Length << endl;
cout << "Area: " << Area << endl;
return 0;
}
Output:
Width:5
Length: 10
Area: 50
31
Characters
ASTU
32
ASTU
33
Operators
ASTU
34
Assignment Operators
ASTU
35
ASTU
+= n += 25 n = n + 25
-= n -= 25 n = n – 25
*= n *= 25 n = n * 25
/= n /= 25 n = n / 25
%= n %= 25 n = n % 25
36
ASTU
% Remainder 13 % 3 //gives 1
Arithmetic operators.
38
ASTU
For example:
9 / 2 // gives 4, not 4.5!
-9 / 2 // gives -5, not -4!
Unintended integer divisions are a common
source of programming errors.
To obtain a real division when both operands are inte-
gers, you should cast one of the operands to be real:
int cost = 100;
int volume = 80;
double unitPrice = cost / (double) volume; // gives 1.25
40
ASTU
41
ASTU
42
ASTU
Parameter Result
Header File Function Type(s) Type Result
43
Relational Operators
ASTU
For example,
the expression "HELLO" < "BYE" causes the
address of "HELLO" to be compared to the
address of "BYE".
As these addresses are determined by the
compiler (in a machine-dependent manner),
the outcome may be 0 or 1, and is therefore
undefined.
C++ provides library functions (e.g., strcmp)
for the lexicographic comparison of string.
46
Logical Operators
ASTU
Logical operators
47
ASTU
48
ASTU
51
The sizeof() Operator
ASTU
53
ASTU
54
ASTU
#include <iostream>
Using namespace std;
int main ()
{ int count = 10, dec=5;
cout <<"count = "<< count << endl; //displays 10
cout <<"dec = "<< dec << endl; //displays 5
cout<< "count = "<< ++count << endl; //displays 11 (prefix)
cout<< "dec = "<< --dec << endl; //displays 4 (prefix)
cout<<"count = " << count << endl; //displays 11
cout<<"dec = " << dec<< endl; //displays 4
cout<<"count = "<< count++<<endl; //displays 11 (postfix)
cout<<" dec = "<< dec--<<endl; //displays 4 (postfix)
cout<<"count = "<<count << endl; //displays 12
cout<<"dec = "<<dec << endl; //displays 3
} 55
Precedence of Operators ASTU
a==b+c*d
c * d is evaluated first because * has a higher prece-
dence than + and = = The result is then added to b be-
cause + has a higher precedence than = = And then ==
is evaluated.
Precedence rules can be overridden by using brackets.
E.g. rewriting the above expression as:
a = = (b + c) * d causes + to be evaluated before *.
Operators with the same precedence level are evaluated
in the order specified by the column on the table of
precedence rule.
E.g. a = b += c the evaluation order is right to left, so the
first b += c is evaluated followed by a = b.
57
Simple Type Conversion
ASTU
58
Exercises
ASTU
59