CPLUSPLUS UET PESHAWAR BS ELECTRICAL 2ND SEMSTER Lecture02.ppt

abdurrahimk182 8 views 20 slides Mar 04, 2025
Slide 1
Slide 1 of 20
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20

About This Presentation

C++ BASIC


Slide Content

1
Lecture 2

2
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
Integer object type
The basic integer object type is int
The size of an int depends on the machine and the compiler
On PCs it is normally 16 or 32 bits
Other integers object types
short: typically uses less bits (often 2 bytes)
long: typically uses more bits (often 4 bytes)
Different types allow programmers to use resources more efficiently
Standard arithmetic and relational operations are available for these
types
Fundamental C++ Objects

3
Integer constants
Integer constants are positive or negative whole numbers
Integer constant forms
Decimal
Digits 0, 1, 2, 3, 4, 5, 6, 7
Octal (base 8)

Digits 0, 1, 2, 3, 4, 5, 6, 7
Hexadecimal (base 16)

Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a , b, c, d, e, f, A, B, C, D, E, F
Consider
31 oct and 25 dec
Decimal Constants
Examples
97
40000
50000
23a (illegal)
The type of the constant depends on its size, unless the type is
specified

4
Character object type
Char is for specifying character data
char variable may hold only a single lowercase letter, a single
upper case letter, a single digit, or a single special character
like a $, 7, *, etc.
case sensitive, i.e. a and A are not same.
ASCII is the dominant encoding scheme
Examples
 ' ' encoded as 32 '+' encoded as 43
 'A' encoded as 65 'Z' encoded as 90
 'a' encoded as 97 'z' encoded as 122

5
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
'\t' is the explicit tab character, '\n' is the explicit
new line character, and so on

6
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.

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
cin >> integer1;
Assume user entered 45
cin >> integer2;
Assume user entered 72
sum = integer1 + integer2;
integer145
integer272
integer145
sum117
integer272
integer145

8
Names (naming entities)
Used to denote program values or components
A valid name is a sequence of
Letters (upper and lowercase)
A name cannot start with a digit
Names are case sensitive
MyObject is a different name than MYOBJECT
There are two kinds of names
Keywords
Identifiers

9
Keywords
Keywords are words reserved as part of the language
int, return, float, double
They cannot be used by the programmer to name things
They consist of lowercase letters only
They have special meaning to the compiler

10
C++ key words
C++ Keywords
Keywords common to the
C and C++ programming
languages

auto break case char const
continue default do double else
enum extern float for goto
if int long register return
short signed sizeof static struct
switch typedef union unsigned void
volatile while
C++ only keywords
asm bool catch class const_cast
delete dynamic_cast explicit false friend
inline mutable namespace new operator
private protected public reinterpret_cast
static_cast template this throw true
try typeid typename using virtual
wchar_t

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)
Long enough to be understandable

When using multiple word identifiers capitalize the first letter of each
word
Examples
Grade
Temperature
CameraAngle
IntegerValue

12
Definitions/declaration
All objects (or variable)
that are used in a program
must be defined (declared)
An object definition specifies
Type
Identifier
General definition form
Type Id, Id, ..., Id ;
Known
type
List of one or
more identifiers
(Value of an object is whatever is in
its assigned memory location)
Examples
Char Response;
int MinElement;
float Score;
float Temperature;
int i;
int n;
char c;
float x;
Location in memory
where a value can
be stored for
program use

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;
If your compiler allows this, int variable will
most likely contain the value 2, not 2.99

14
Stream extraction and assignment operator
>> (stream extraction operator)
When used with cin, waits for the user to input a value and
stores the value in the variable to the right of the operator
The user types a value, then presses the Enter (Return) key to
send the data to the computer
Example:
int myVariable;
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
5int main()
6{
7 int integer1, integer2, sum; // declaration
8
9 cout << "Enter first integer\n"; // prompt
10 cin >> integer1; // read an integer
11 cout << "Enter second integer\n"; // prompt
12 cin >> integer2; // read an integer
13 sum = integer1 + integer2; // assignment of sum
14 cout << "Sum is " << sum << endl; // print sum
15
16 return 0; // indicate that program ended successfully
17}
•Notice how cin is used to get user input.
General form is cin>>identifier;
•Cin is an I stream object
•streams input from standard input
•uses the >> (input operator)
•Note that data entered from the keyboard
must be compatible with the data type of the
variable
endl flushes the buffer and prints a
newline.
•Variables can be output using cout << variableName.
•Generl form is cout<<expression;
•An expression is any c++ expression(string constant,
identifier, formula or function call)
•Cout is an o stream object
•streams output to standard output
•uses the << (output) operator
Calculations can be performed in output statements: alternative for
lines 13 and 14:
cout << "Sum is " << integer1 + integer2 << std::endl;
Use stream extraction
operator with standard input
stream to obtain user input.
Concatenating, chaining or
cascading stream insertion
operations.

16
Output of program

17
program to find the area of rectangle
Tells the compiler to use names
in iostream in a “standard” way

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.
3.# include <iostream> //preprocessor directive
4.int main()
5.{
6. int number_of_sections, students_per_section; //declaration
7. int total_students;
8. cout<<"enter the number of sections\n"; //prompt to enter total number of
sections
9. cin>>number_of_sections; //reading number of sections
10. cout<<"enter the number of students per section\n"; //prompt to enter number
11. // of students per section

12. cin>>students_per_section; //reading students per section
13.
14. total_students = number_of_sections * students_per_section ; //assignment to total
students
15. cout<<"total number of students in all the sections is\n"; //prompt
16. cout<<total_students; // show the number of
total students
17. return 0;
18.}

20
output
Tags