Lecture 02 Prog
Lecture 02 Prog
1
Fundamental C++ Objects
C++ has a large number of fundamental or built-in object types
The fundamental object types fall into one of three categories
Integer objects
Floating-point objects
Character objects
4
Character object type
Explicit (literal) characters within single quotes
'a','D','*‘
Special characters - delineated by a backslash \
Two character sequences (escape codes)
Some important special escape codes
\t denotes a tab \n denotes a new line
\\ denotes a backslash \' denotes a single quote
\" denotes a double quote
5
Floating-point object
Floating-point object types represent real numbers
Integer part
Fractional part
The number 108.1517 breaks down into the following parts
108 - integer part
1517 - fractional part
C++ provides three floating-point object types
Float
(often 4 bytes) Declares floating point numbers with up to 7 significant digits
Double
long double
(often 10 bytes) Declares floating point numbers with up to 19 significant digits.
6
Floating-Point Constants
Standard decimal notation
134.123 F or f indicates single precision
0.15F floating point value
7
Memory Concepts
Variable
Variables are names of memory locations
Correspond to actual locations in computer's memory
Every variable has name, type, size and value
When new value placed into variable, overwrites previous value
Reading variables from memory is nondestructive
integer1 45
std::cin >> integer2;
Assume user entered 72
integer2 72
10
C++ key words
C ++ Ke yw o rd s
11
Identifiers
Identifiers are used to name entities in c++
It consists of letters, digits or underscore
Starts with a letter or underscore
Can not start with a digit
Identifiers should be
Short enough to be reasonable to type
Standard abbreviations are fine (but only standard abbreviations)
Examples
Grade
Temperature
CameraAngle
IntegerValue
12
Definitions/declaration
Location in memory
where a value can Examples
be stored for
All objects (or variable) program use Char Response;
that are used in a program int MinElement;
must be defined (declared)
float Score;
An object definition specifies
float Temperature;
Type
int i;
Identifier
int n;
General definition form
char c;
float x;
Known List of one or
type more identifiers
13
Type compatibilities
Rule is to store the values in variables of the same type
This is a type mismatch:
int int_variable;
int_variable = 2.99;
14
Stream extraction and assignment operator
int myVariable;
std::cin >> myVariable;
Waits for user input, then stores input in myVariable
= (assignment operator)
Assigns value to a variable
Binary operator (has two operands)
Example:
sum = variable1 + variable2;
15
A simple program to add two numbers
1 //example
2 // program to add two numbers
3 #include <iostream.h>
4
5 int main()
6 {
7 Use
int integer1, integer2, sum; stream extraction
// declaration
8 operator with standard input
9 stream to//obtain
cout << "Enter first integer\n"; user•input.
prompt Notice how cin is used to get user input.
10 cin >> integer1; Calculations can //be performed
read in output
an General
integer form statements: alternative for
is cin>>identifier;
11 cout << "Enter secondlines 13 and 14:// prompt
integer\n"; •Cin is an I stream object
12 cin >> integer2; •streams input from standard input
// read an integer
13 cout << "Sum is " <<
sum = integer1 + integer2; // integer1
•usesofthesum
+ integer2
assignment >> (input operator)
<< std::endl;
•Note that data entered from the keyboard
14 cout << "Sum is " << sum << endl; // print sum
must be compatible with the data type of the
15 variable
•Variables can be output using cout << variableName.
16 return 0; // form
•Generl indicate endl flushes
that program ended successfully
is cout<<expression; the buffer and prints a
17 } •An expression is any c++ expression(string constant,
newline.
Concatenating, chaining or
identifier, formula or function call) cascading stream insertion
•Cout is an o stream object
•
operations. 16
streams output to standard output
•uses the << (output) operator
Output of program
17
program to find the area of rectangle
18
output
19
Program to find total number of students in all sections
1. //example
2. //to find the total number of students in all sections.
18. return 0;
19. }
20
output
21