CS101
Computer programming and utilization
Dr Deepak B PhatakSubrao Nilekani Chair Professor
Department of CSE, Kanwal Rekhi Building
IIT Bombay
Lecture 3, Basic features of C++
Tuesday, 2 August 2011
And Wednesday, 3 August 2011
IIT BOMBAY
Dr Deepak B Phatak 1Lecture 3 Basic features of C++
IIT BOMBAY
Lecture 3 Basic features of C++
Overview
• C++ Program structure
• C++ rules of computing
• Naming Conventions
• Data Types
• Expression evaluation
• Programming Examples
• Quiz
Dr Deepak B Phatak 2
IIT BOMBAY
• All statements in a C++ program must be written in
accordance with rules of syntax
• The entire program is usually stored in a single file
• File name of your choice, extension .cpp
Examples: P1.cpp AddNumbers.cpp
mid_sem_marks_analysis.cpp
Dr Deepak B Phatak 3Lecture 3 Basic features of C++
C++ program structure
IIT BOMBAY
• A C++ program contains certain compiler directives
• These statement provide information to compiler
used during the translation
#include <iostream>
• This directive tells the compiler to include
instructions which will enable input and output to
be handled properly
Dr Deepak B Phatak 4Lecture 3 Basic features of C++
Compiler Directives
IIT BOMBAY
• Remember that the input values which we will give
will be in the form of ASCII codes of digits, which
have to be converted to internal binary form
• Similarly, C++ converts internal binary numbers to a
sequence of ASCII bytes for output
• Input/output handled as „streams‟ of bytes
• cin and cout work because of this directive
Dr Deepak B Phatak 5Lecture 3 Basic features of C++
Input-Output in C++
IIT BOMBAY
Dr Deepak B Phatak 6Lecture 3 Basic features of C++
Conversion of stream elements into numbers
IIT BOMBAY
Using namespace std;
• C++ permits names used in our programs, to be
grouped and declared in different „namespaces‟
• Each such namespace itself can be given a name of
our choice, say Xspace. Names declared within a
namespace must be qualified when used.
• Thus if a, b are int objects
Xspace::a = 5; Xspace::b = 4*xspace::a;
• Useful for large programs, headache otherwise
• This statement directs the C++ compiler to use a
“global” standard namespace called std
• Makes life simpler
Dr Deepak B Phatak 7Lecture 3 Basic features of C++
Compiler Directives …
IIT BOMBAY
• In C++, statements can be grouped by enclosing
them within a pair of braces, „{‟ and „}‟
• Each such group is treated as a single statement
• Each program is treated as a „function‟. Functions
can be invoked by other programs
• The „main‟ program which we write is treated as a
function, which is called by the Operating System
Dr Deepak B Phatak 8Lecture 3 Basic features of C++
Components of a C++ program
IIT BOMBAY
• When we begin executing a program, the OS passes
control to the main function
• OS can pass one or more parameters to it
• When our program completes its execution, it
passes the control back to the OS
• This requires execution of a „return‟ statement
Dr Deepak B Phatak 9Lecture 3 Basic features of C++
Components ...
IIT BOMBAY
• A program will use several computational objects.
Their names and associated data types must be
declared in our program
• The declarations can appear anywhere, but must
occur before the objects are first used
• Put them together at the beginning
• Comments can be inserted anywhere in a program
Dr Deepak B Phatak 10Lecture 3 Basic features of C++
Components of a C++ program ...
IIT BOMBAY
• Ignored completely by the compiler
• Different parts of the program should be written
with appropriate „indentation‟
• Use a tab, or a few blank spaces at the beginning
of each line within an identifiable group
Dr Deepak B Phatak 11Lecture 3 Basic features of C++
Components ...
IIT BOMBAY
// file add_two_numbers.cpp
#include <iostream>
using namespace std;
// this program reads two integers
// and calculates the sum
int main() {
int A, B, C;
cout << “Give two numbers”;
cin >> A >> B;
C = A + B;
cout << “Sum is” << C;
return 0;
}
Dr Deepak B Phatak 12Lecture 3 Basic features of C++
Organization of a C++ program
IIT BOMBAY
// file add_two_numbers.cpp
#include <iostream>
using namespace std;
// this program reads two integers
// and calculates the sum
int main() {
int A, B, C;
cout << “Give two numbers”;
cin >> A >> B;
C = A + B;
cout << “Sum is” << C;
return 0;
}
Dr Deepak B Phatak 13Lecture 3 Basic features of C++
Organization of a C++ program ...
Compiler Directives
Main program
Begins here
Stop program execution
and return to OS
IIT BOMBAY
Names
• Names (called identifiers) can consist of letters,
digits, and underscore (_)
• A name must not contain a blank or tab
• It must begin with a letter or underscore
• Names can be of any length
• first 31 symbols should be unique
• Keywords in C++ cannot be used for our names
using, return, int, …
• Meaningful names must be chosen by us.
Dr Deepak B Phatak 14Lecture 3 Basic features of C++
IIT BOMBAY
What is there in a name?
Dr Deepak B Phatak 15Lecture 3 Basic features of C++
• Roll number of a student
• Value of temperature in degree Centigrade
• Marks obtained in a quiz
IIT BOMBAY
Names …
Dr Deepak B Phatak 16Lecture 3 Basic features of C++
• Marks obtained in the semester-end examination
IIT BOMBAY
Objects in C++
Dr Deepak B Phatak 17Lecture 3 Basic features of C++
• In C++, names are used to represent „objects‟
• Values associated with named objects can change
• Each object can have a value of certain type
• Thus every name must have an associated type
• Constant values used in the program are also
objects of a certain type
IIT BOMBAY
Types of values in C++
• C provides for several types of values
char, int, float, double, void (valueless)
bool, wchar_t
• we will first consider integer and floating point
• int type is used to represent integer numbers
• float type used to represent fractional
numbers
• Also used to represent very large values
Dr Deepak B Phatak 18Lecture 3 Basic features of C++
IIT BOMBAY
Numerical values
• We write numerical values in a standard manner.
For example:
25 -7389 1240000000
87.669 3.14159 -0.0000123
• Very large and very small values are written in
exponential notation [a mantissa and an exponent]
1.24E9, -1.23E-5, 6.023E23, 124.0E7, 0.124E10, etc.
• C++ permits us to write such constant values in our
programs, and also accepts these as input.
Dr Deepak B Phatak 19Lecture 3 Basic features of C++
IIT BOMBAY
Declarations of object names
• Class or type of name for any object must be
explicitly declared in a program
• The compiler will use such declaration to allocate
appropriate memory locations based on the type
int i, j, Count, NumberOfTanks,
• There exist other „qualifiers‟ to int type
• short (2 bytes), unsigned (no sign bit),
• long (4 or 8 bytes)
Dr Deepak B Phatak 20Lecture 3 Basic features of C++
IIT BOMBAY
Integer Values
• Decimal numbers
• Must begin with a nonzero digit, except when
value is 0
5 0 253 -1415261
• The value must be within the prescribed range
• 0ctal (base 8) numbers
• written beginning with 0
• 045 0100 07775
• Hexadecimal (base 16) numbers
• Written beginning with 0x
• 0x10 0x2b9 0xAbF 0x100
Dr Deepak B Phatak 21Lecture 3 Basic features of C++
IIT BOMBAY
Floating point values
• Standard decimal notation
4.7816 -0.00046
• exponential notation with mantissa and exponent
1.2E9 -2E20
Dr Deepak B Phatak 22Lecture 3 Basic features of C++
IIT BOMBAY
Floating point values ...
Dr Deepak B Phatak 23Lecture 3 Basic features of C++
• Mantissa precision is 6 digits minimum
• exponent range is -37 to +37 minimum
• We use decimal notation while writing these
numbers in our programs, or while giving input
• Such values are always converted to internal
binary representation
IIT BOMBAY
Floating point objects
Dr Deepak B Phatak 24Lecture 3 Basic features of C++
• Defined using keyword float
float x, y, val, radius, circle_area;
• 4 bytes are allocated
• 24 bits used for mantissa, 8 used for exponent
• „double‟ is like a long float
• 8 bytes, with larger mantissa and exponent
• Example:
double CircleArea, Savings_Account_Balance;
IIT BOMBAY
Character objects
Dr Deepak B Phatak 25Lecture 3 Basic features of C++
• Character values are written as
„P‟ „*‟ „ ‟
„\t‟ (tab character)
„\n‟ (New Line character)
„\‟‟ (A single quote character)
• There exists a type char for character objects
(one byte – stores ASCII codes)
• Declaration of character objects
int sym, first_letter_of_name;
IIT BOMBAY
Char objects
Dr Deepak B Phatak 26Lecture 3 Basic features of C++
• Char object names can be assigned character values
int sym;
sym = „P‟;
• C++ treats char object as an integer values
• Can participate in integer expressions
IIT BOMBAY
Character objects ...
Dr Deepak B Phatak 27Lecture 3 Basic features of C++
• We note that our program itself is written as a
sequence of „characters‟
• These are analyzed by the compiler to
understand and translate our instructions in
machine language
• The input values which we give are also given as
a stream of characters
• These are interpreted by the „cin‟ command,
and converted to the internal representation
• Similarly, „cout‟, takes an internal value, and
converts it to a stream of symbols which
appear as a decimal value to us
IIT BOMBAY
Assignment operation
int m;
m = -35;
• Symbol „=„ is called the assignment operator
• the value on the right hand side (RHS) of this
operator is „evaluated‟
• In the example, it is simply 35
• It is then stored in the location for the object m
named on the left hand side (lhs)
• Previous value associated with object m is lost
Dr Deepak B Phatak 28Lecture 3 Basic features of C++
IIT BOMBAY
Expression
• RHS need not be a single value, can be expression
int m, n;
n = 3;
m = 35 * n - 167;
• Expression on RHS is evaluated. The resulting single
value is called value of the expression.
• This value is assigned to the object on LHS
LHS must be an object to which value can be assigned
Dr Deepak B Phatak 29Lecture 3 Basic features of C++
IIT BOMBAY
Expression evaluation
• Arithmetic operators
- + * / % (modulo operator, it gives remainder)
• Precedence rule
* / % have higher precedence than - +
For example: a+b*c
[b*c calculated first, result added to a]
• Associative rule
• Within the same precedence, associativity is left to right
x*y/z
[x*y calculated first, result divided by z ]
• Parentheses are used to override the precedence
Dr Deepak B Phatak 30Lecture 3 Basic features of C++
IIT BOMBAY
Example of expression evaluation
Dr Deepak B Phatak 31Lecture 3 Basic features of C++
IIT BOMBAY
Operands may be objects with different types
Dr Deepak B Phatak 32Lecture 3 Basic features of C++
• If both operands are of same type
• result value is of the same type
9.0/2.0 will result in 4.5
9/2 will result in 4
• If the operands are of a different type
• int is converted to float
• The result value is of type float
IIT BOMBAY
Expression evaluation ...
Dr Deepak B Phatak 33Lecture 3 Basic features of C++
• possible problems in expression evaluations
• while two values participating in an operation can
individually be within the stipulated range, the
result may not be in the range
• In such a case, an erroneous value will result
IIT BOMBAY
Type conversion during assignment
int i, j; float x, y;
i=-25; x=2.147;
j=3.2; // float values converted to int
// (fractional part is truncated)
j=1.24E30;
Complete loss of precision, value too large
Dr Deepak B Phatak 34Lecture 3 Basic features of C++
IIT BOMBAY
Type conversion …
Dr Deepak B Phatak 35Lecture 3 Basic features of C++
y=29;
value converted to float, equivalent to .29E2
y=123456789;
value converted to float with loss of precision
.123457E9
IIT BOMBAY
Examples
• Eexpressions and their C++ equivalent
(a+b)x
1
x + 1
x + 1
Dr Deepak B Phatak 36Lecture 3 Basic features of C++
IIT BOMBAY
Assignment statement revisited
• general form of an assignment operation is
name = expression;
y = 3.14159*r*r+h*w -2*3.14159*rdash;
• The entire assignment operation is also treated
logically as an expression, whose value is same as
the one finally assigned to the name on lhs
• Thus m = n =25; is a valid statement
m = (n = 25);
• m=n=p=10;
• This is same as m=(n=(p=10));
Dr Deepak B Phatak 37Lecture 3 Basic features of C++
IIT BOMBAY
Reassignment
Dr Deepak B Phatak 38Lecture 3 Basic features of C++
• Object on LHS may appear in the expression on RHS
n = 3*m + 2*n;
• The RHS is evaluated using existing value of n
• The result of expression is assigned to n
• Which becomes the new value of n
IIT BOMBAY
Dr Deepak B Phatak 39Lecture 3 Basic features of C++
IIT BOMBAY
Quiz
• What will be the values printed by the following
program?
int main (){
int m, n; float x, y;
m=x=n=y=8.79;
cout << m << x << n << y;
Dr Deepak B Phatak 40Lecture 3 Basic features of C++
IIT BOMBAY
Quiz
• What is the value output by this program?
float x, y, avogadro;
avogadro = 6.023E23; y = 7.3463;
x=y-8.6597+avogadro;
Dr Deepak B Phatak 41Lecture 3 Basic features of C++
IIT BOMBAY
Quiz
Dr Deepak B Phatak 42Lecture 3 Basic features of C++
• Write a program to calculate the value of y, for any
given value of x, if x and y are related by
y = mx + c
IIT BOMBAY
C++ Program
Dr Deepak B Phatak 43Lecture 3 Basic features of C++
IIT BOMBAY
Dr Deepak B Phatak 44Lecture 3 Basic features of C++
IIT BOMBAY
Take home problem
Dr Deepak B Phatak 45Lecture 3 Basic features of C++
• Starting from a point, a person walks at a steady
speed of 4 Km per hour for 3 hours. The person then
starts running at a speed of 8 Km per hour. He stops
after traveling for 5 hours
• Write a program to determine the distance
traveled by the person in some given time t
IIT BOMBAY
Dr Deepak B Phatak 46Lecture 3 Basic features of C++