SlideShare a Scribd company logo
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;
integer1 45
integer2 72
integer1 45
sum 117
integer2 72
integer1 45
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 integern"; // prompt
10 cin >> integer1; // read an integer
11 cout << "Enter second integern"; // 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 sectionsn"; //prompt to enter total number of
sections
9. cin>>number_of_sections; //reading number of sections
10. cout<<"enter the number of students per sectionn"; //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 isn"; //prompt
16. cout<<total_students; // show the number of
total students
17. return 0;
18. }
20
output

More Related Content

PPT
02a fundamental c++ types, arithmetic
PDF
BASIC C++ PROGRAMMING
PPT
Chapter02-S11.ppt
PPTX
#Code2 create c++ for beginners
PPT
Chapter 2 Introduction to C++
PPT
Basics of c++ Programming Language
PPT
programming week 2.ppt
02a fundamental c++ types, arithmetic
BASIC C++ PROGRAMMING
Chapter02-S11.ppt
#Code2 create c++ for beginners
Chapter 2 Introduction to C++
Basics of c++ Programming Language
programming week 2.ppt

Similar to CPLUSPLUS UET PESHAWAR BS ELECTRICAL 2ND SEMSTER Lecture02.ppt (20)

PPT
Pengaturcaraan asas
PDF
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
PDF
Lecture # 1 introduction revision - 1
PPT
Lecture#2 Computer languages computer system and Programming EC-105
PDF
Chap 2 c++
PPTX
Lecture # 1 - Introduction Revision - 1 OOPS.pptx
PDF
Week 02_Development Environment of C++.pdf
PDF
C++ L01-Variables
PPT
Elementary_Of_C++_Programming_Language.ppt
PPT
C++ basic.ppt
PPT
C++ Overview
PPTX
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
PPTX
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
PDF
C++ Chapter I
PPTX
Chapter1.pptx
PPT
Chapter2
PDF
Basic Elements of C++
PDF
1-19 CPP Slides 2022-02-28 18_22_ 05.pdf
PPTX
C++ lectures all chapters in one slide.pptx
PPT
2.overview of c++ ________lecture2
Pengaturcaraan asas
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
Lecture # 1 introduction revision - 1
Lecture#2 Computer languages computer system and Programming EC-105
Chap 2 c++
Lecture # 1 - Introduction Revision - 1 OOPS.pptx
Week 02_Development Environment of C++.pdf
C++ L01-Variables
Elementary_Of_C++_Programming_Language.ppt
C++ basic.ppt
C++ Overview
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
C++ Chapter I
Chapter1.pptx
Chapter2
Basic Elements of C++
1-19 CPP Slides 2022-02-28 18_22_ 05.pdf
C++ lectures all chapters in one slide.pptx
2.overview of c++ ________lecture2
Ad

Recently uploaded (20)

PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
top salesforce developer skills in 2025.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
history of c programming in notes for students .pptx
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Cost to Outsource Software Development in 2025
PPTX
Transform Your Business with a Software ERP System
PPTX
assetexplorer- product-overview - presentation
PPTX
Odoo POS Development Services by CandidRoot Solutions
Design an Analysis of Algorithms I-SECS-1021-03
wealthsignaloriginal-com-DS-text-... (1).pdf
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
top salesforce developer skills in 2025.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
history of c programming in notes for students .pptx
Digital Systems & Binary Numbers (comprehensive )
Wondershare Filmora 15 Crack With Activation Key [2025
Design an Analysis of Algorithms II-SECS-1021-03
Softaken Excel to vCard Converter Software.pdf
Understanding Forklifts - TECH EHS Solution
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Upgrade and Innovation Strategies for SAP ERP Customers
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Cost to Outsource Software Development in 2025
Transform Your Business with a Software ERP System
assetexplorer- product-overview - presentation
Odoo POS Development Services by CandidRoot Solutions
Ad

CPLUSPLUS UET PESHAWAR BS ELECTRICAL 2ND SEMSTER Lecture02.ppt

  • 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. 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. 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. 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. 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. 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; integer1 45 integer2 72 integer1 45 sum 117 integer2 72 integer1 45
  • 8. 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. 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. 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. 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. 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. 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. 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. 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 integern"; // prompt 10 cin >> integer1; // read an integer 11 cout << "Enter second integern"; // 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.
  • 17. 17 program to find the area of rectangle Tells the compiler to use names in iostream in a “standard” way
  • 19. 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 sectionsn"; //prompt to enter total number of sections 9. cin>>number_of_sections; //reading number of sections 10. cout<<"enter the number of students per sectionn"; //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 isn"; //prompt 16. cout<<total_students; // show the number of total students 17. return 0; 18. }