0% found this document useful (0 votes)
3 views

Lecture 3(Basics)

Uploaded by

Abdul Basit AB
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 3(Basics)

Uploaded by

Abdul Basit AB
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 41

C++ data types

Simp Structur
le ed
arra stru unio class
floatin y ct n
g
float doubl long
e double
enum

integr Addres
al s

charshort int long bool pointe refere


r nce

unsign
ed
cin - some facts ...
Possible formats of input:
Example: 1) 101112
int a, b, c;
cin >> a >> b >> c; 2) 10    11    12
3) 10  11
 = <enter> key 12
 = space

4) 10  11

12
5) 10
11
12
Character Objects
• A variable of type char can store any single
character value.
• Character data in a C++ program is represented
by writing the appropriate symbol between single
quote signs
Ex: 'a ' '#' 'Z '
• Declaring and initializing a variable of type
character.
char letter = 'a' ;
Character Objects
Ex: char grade;
cout << "Enter your grade: ";
cin >> grade;
cout << "You received a grade of " << grade
<< endl;

•Whitespace characters = characters that


produce empty(white) space when output,
such as blanks, new line character, and
horizontal tabs.
Character Objects ….
• Assume i, j are int variables, and ch is a char
variable.

What will be the contents of i , j, and ch


after input?

Statement Data
i j
1. cin >> i; 32

2. cin >> i >> j; 460


Character Objects ….
3. cin >> i >> ch; 25  A i ch j

4. cin >> i >> ch; 25


A
5. cin >> i >> ch; 25A

6. cin >> i >> j >> ch; 1  2  8

7. cin >> i >> ch; 46  32 


Escape Character
• If want the single quote (') character need to
express it as \'
Ex: const char QUOTE = ' \ ' ';
\ (known as the backslash character) acts as an
escape character in C++
• More common uses of the escape character:
In program Effect
\' '
\\ \
\n new line
\t tab character
\a bell character
Escape character ….
Example 1: If you wanted to output
She replied, "You can quote me. "

What is the corresponding cout command?


Escape character ….
Example 2: If you wanted to output

Read the file c:\windows\readme.txt

What is the corresponding cout


command?
Escape character ….
• What will the following code fragment output?

cout << "This code\nuses the\n";


cout << "newline character\n ";
cout << "and makes the speaker beep\a. ";

• To begin a new line, use \n , when it can be made the last


character of a string.
• To begin a new line, use endl if the last output of a line is a
value.
cout << a << endl;
Input whitespace Characters
• There are times when you do not want to skip over
whitespace.

• cin.get(…) (instead of cin >>..) reads


the next input character, whether the next character
is whitespace or not.

Example:
char nextCh;
cin.get(nextCh); // will read any character
Input whitespace Characters …
Example
What is the input in each case?
char ch1, ch2, ch3;
ABCD 

Case 1: Case 2:
cin.get(ch1); AB  cin >> ch1; AB 
cin.get(ch2); CD  cin >> ch2; CD 
cin.get(ch3); cin >> ch3;

c c c ch1 ch2 ch3


h h h
1 2 3
Last look at output with int type
• cout can be used to output variables of type char, int
and double.
• cout with char outputs just that character.
• cout with int displays just value of integer, using least
amount of space.
• Can add whitespace to output by writing setw(n)
(which is read “set width to n”).
NOTE: It only affects the next item!
Last look at output with int type ….
Ex: int first=1, last = 9999;
cout << setw(6) << first << last;
Output: 19999

(where  stands for a blank)


Last look at output with int type ….

How would I generate the following output using


a single cout statement?
Number of quarters = 45
Number of dimes = 107
Number of nickels = 3
Number of pennies = 24
Last look at output with int type
….
• To be able to use setw need to include the file
iomanip.
• Need directive #include <iomanip> at the
beginning of your program.
• Your programs should begin with

#include <iostream> // for cout,cin


#include <iomanip> // for setw
using namespace std;
Real Numbers …
Two ways of representing real number

1) Fixed point = whole part & fraction


part

Example:
Number Whole Fraction
-12.352 -12 .352
123.4 123 .4
Real Numbers …

2) Floating point = scientific


notation
<mantissa>.<exponent>

Example 1:
Number Mantissa Exponent
-5.07e15 -5.07 15

means -5.07 x 1015 =


-5070000000000000. (13 zeroes)
Real Numbers …
2) Floating point = scientific notation
<mantissa>.<exponent>

Example 2:
Number Mantissa Exponent
1.23e-5 1.23 5
means 1.23 x 10-5 = 0.0000123

• Real number standard operations, +, -, *, /,


and
negation.
• Cannot use the % operator with real
numbers.
Real Numbers …
What output do you think is generated by the following?

void main()
{
cout << 4.5 << endl; 4.5
cout << -.375 << endl; -0.375
cout << 10.6666666 << endl; 10.6667
cout << 67800000.0 << endl; 6.78e+007
cout << 523.0 << endl; 523
cout << 523000000000. << endl; 5.23e+011
cout << 0.000098 << endl; 9.8e-005
}
Output Format for Real Numbers
• There are three modes for output of real
values:
- Default : depending on size of number,
default
chooses between, scientific and
fixed.
- Scientific: for very small or very large
numbers.
- Fixed: which is the way we are used to
seeing
real numbers.
Manipulators

• endl*, fixed, showpoint, setw*, and setprecision are


manipulators that can be used to control output
format.

• endl is used to terminate the current output line,


and create blank lines in output.

* Have already seen with int type


Manipulators …

• Use the following statement to specify that (for output


sent to the cout stream) decimal format (not scientific
notation) be used, and that a decimal point be
included (even for floating values with 0 as fractional
part)

cout << fixed << showpoint ;


Manipulators …
• Setprecision(n)
- requires #include <iomanip> and appears in an
expression using insertion operator (<<)

- If fixed has already been specified, argument n


determines the number of places displayed after the
decimal point for floating point values.

- Remains in effect until explicitly changed by another call


to setprecision
Output Format for Real Numbers …
Example 1: What output is generated by the following program
segment?

double a = 123.545;
cout << fixed << showpoint;
cout << setw(10) <<setprecision(3) << a
<< endl;

cout << setw(7) <<setprecision(2) << a


<< endl;
1)   123.545
2) 123.54
Output Format for Real Numbers …
Example 1: ……
double a = 123.545;
cout << fixed << showpoint;

cout << setw(2) << setprecision(1) << a


<< endl;

123.5
Output Format for Real Numbers …
Example 2: What output is generated by the following
program segment?
double a = 123.545;
cout << fixed; // showpoint removed
cout << setw(8) << setprecision(3) << a
<< endl;

123.545
Output Format for Real Numbers …
Example 2: …….
double a = 123.545;
cout << fixed; // showpoint removed
cout << setw(6) << setprecision(2) << a
<< endl;

cout << setw(2) << setprecision(1) << a


<< endl;

1) 123.54
2) 123.5
Output Format for Real Numbers …
Example 2: ……
double a = 123.545;
cout << fixed; // showpoint removed
cout << setw(2) << setprecision(2) << a
<< endl;

cout << setw(5) << setprecision(0) << a


<< endl;
1) 123.54
2)   123
Manipulators/Header files
HEADER MANIPULATOR ARGUMENT
EFFECT
FILE TYPE

<iostream> endl none terminates output line

<iostream> showpoint none displays decimal point

<iostream> fixed none suppresses scientific notation

<iomanip> setw(n) int sets fieldwidth to n positions

<iomanip> setprecision(n) int sets precision to n digits


Implicit Type Conversion
• Convert less complex type to more complex when
apply operator.
- integer operator short ==> integer
- integer operator real ==> real
- char operator integer ==> integer
Example 1:
What will be stored in r?
double r;
int a = 4, b= 2;
r = a/b;
Implicit Type Conversion …

Example 2
double r = 2.0, s;
int a = 5, b = 2, c;
c = a/b;
s = a/b;
s = a/r;
c = a/r; //warning
Implicit Type Conversion …
Example 3
char c = 'a'; //ascii code = 97

int a = 16;
double d = 1.5;
cout << (c + a + d) << endl;
// Result will be 114.5. How?
Explicit Type Conversion

Involves attaching the type to the


object.
- char(70) converts int 70 to char ‘F’

- double(5) converts int 5 to double


5.0

- int(1.75) converts double 1.75 to int


1
Explicit Type Conversion

Example 1: Value in each variable?


int a = 11, b;
double d = 1.23, f;
b = a / 5;
f = a / 5;
// How do I store 2.2 in f?

13.53

// What is stored in f & b? 13

f = d * a;
b = d * a;
Explicit Type Conversion …
Exercise
Each statement uses type conversion. Give
the value assigned to x.
a) int x; b) int x, y=20; 76

x = int(3.8); x = int(3.8 * y);

c) char x;
d) int x, y =20;
x = char(65);
x = int(3.8) * y; 60

f) char ch =‘F’;
int x;
x = ch;
Arithmetic Assignment
Each of the numerical operators +, - *, /
and % can be combined with the
assignment operator to create an
arithmetic assignment operator.
• total_tax += state_tax; is equivalent
to
total_tax = total_tax + state_tax;
• count = count - 1; can be written as
count -= 1;
• twice = twice * 2; can be written as
twice *= 2;
Arithmetic Assignment …

Valid or invalid? Explain


salary =+bonus;

salary + = bonus;
Arithmetic Assignment …
Fill in the values for the objects a and b.

#include <iostream>
using namespace std;
void main()
{
int a = 20, b;
a -= 4; // a = ______

a = 20;
b = 4;
a += b; // a = ______
b *= a; // b = ______
a %= b + 6; // a = ______
}
Practical Work
C++ systems provide a header file climits, which
contains declarations of constants related to the
specific computer and machine on which you are
working. Two of these constants are INT_MAX and
INT_MIN, the largest and smallest int values for your
particular computer. Write a proram to print out the
values of INT_MAX and INT_MIN. Be sure to include
appropriate comments in your program, and use
indentation.
Practical Work
• Write a C++ program that converts a Celsius temperature
to its Fahrenheit equivalent. The formula is
Fahrenheit =9/5 Celsius +32
Make the Celsius temperature a named constant so that
its value can be changed easily. The program should print
both the values of Celsius temperature and its equivalent
Fahrenheit, with appropriate identifying messages. Be
sure to include comments in your program, choose
meaningful identifiers, and use indentation.

You might also like