SlideShare a Scribd company logo
Information Technology
III
Introduction to Programming with C++
History of C and C++
 History of C++
 Extension of C
 Early 1980s: Bjarne Stroustrup (Bell Laboratories)
 Provides capabilities for object-oriented programming
Objects: reusable software components
Model items in real world
Object-oriented programs
Easy to understand, correct and modify
 Hybrid language
C-like style
Object-oriented style
Both
2
Basics of a Typical C++ Environment
 C++ systems
 Program-development environment
 Language
 C++ Standard Library
3
Basics of a Typical C++ Environment4
Phases of C++ Programs:
1. Edit
2. Preprocess
3. Compile
4. Link
5. Load
6. Execute
Loader
Primary
Memory
Program is created in
the editor and stored
on disk.
Preprocessor program
processes the code.
Loader puts program
in memory.
CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
executes.
Compiler
Compiler creates
object code and stores
it on disk.
Linker links the object
code with the libraries,
creates a.out and
stores it on disk
Editor
Preprocessor
Linker
CPU
Primary
Memory
.
.
.
.
.
.
.
.
.
.
.
.
Disk
Disk
Disk
Disk
Disk
Hello World
This is a comment line - the line
is a brief description of program
does.
directives for the preprocessor.
They are not executable code
lines but indications for the
compiler.
<iostream.h> tells the compiler's
preprocessor to include the iostream
standard header file.
cout is the standard
output stream
The return instruction causes the main()
function finish and return the code terminating
the program without any errors during its
execution
This line corresponds to the beginning
of the main function declaration. The
main function is the point C++
programs begin their execution. It is
first to be executed when a program
starts.
Basics of a Typical C++ Environment
 Input/output
 cin
Standard input stream
Normally keyboard
 cout
Standard output stream
Normally computer screen
 cerr
Standard error stream
Display error messages
6
Comments
 It is of 2 types:-
 Single line comments //
 example of single line comment
//this is the very simple example of single line comments
 Multi line comments
/*
This is the example of
multi line comment
*/
A Simple Program: Printing a Line of
Text
8
Escape Sequence Description
n Newline. Position the screen cursor to the
beginning of the next line.
t Horizontal tab. Move the screen cursor to the next
tab stop.
r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the
next line.
a Alert. Sound the system bell.
 Backslash. Used to print a backslash character.
" Double quote. Used to print a double quote
character.
Variables
 Variable names
Valid identifier
Series of characters (letters, digits, underscores)
Cannot begin with digit
Case sensitive
length of an identifier is not limited, (but some compilers
only the 32 first characters rest ignore)
Neither spaces nor marked letters can be part of an
identifier
can also begin with an underline character ( _ )
your own identifiers cannot match any key word of the C++
language
9
Variables
 Location in memory where value can be stored
 Common data types
int - integer numbers
char - characters
double - double precision floating point numbers
Float - floating point numbers
Bool - Boolean value (true or false)
10
Signed and Unsigned Integer Types
 For integer data types, there are three sizes:
 Int
 long - larger size of integer,
 short, which are declared as long int and short int.
 The keywords long and short are called sub-type qualifiers.
 The requirement is that short and int must be at least 16 bits, long must be at
least 32 bits,
 short is no longer than int, which is no longer than long.
 Typically, short is 16 bits, long is 32 bits, and int is either 16 or 32 bits.
 all integer data types are signed data types,
 i.e. they have values which can be positive or negative
Declaration of variables
 Declare variables with name and data type before use
int a;
float mynumber;
int MyAccountBalance;
int integer1;
int integer2;
int sum;
int integer1, integer2, sum;
Example
 A program to add two numbers;
#include<iostream>
using namespace std;
int main() {
int a, b; // variable declaration
a = 10;
b= 20;
int addition = a + b;
cout << “answer is:” << addition << endl;
return 0;
}
Scope of variables
Global variables can
be referred to
anywhere in the
code, within any
function, whenever it
is after its declaration.
The scope of the local
variables is limited
to the code level in
which they are
declared.
Constants: Literals
 A constant is any expression that has a fixed value.
 It can be Integer Numbers, Floating-Point Numbers, Characters and
Strings
 Examples
 75 // decimal
 0113 // octal
 0x4b // hexadecimal
 6.02e23 // 6.02 x 10 23
 1.6e-19 // 1.6 x 10 -19
 3.0 // 3.0
 #define PI 3.14159265
 #define NEWLINE 'n'
circle = 2 * PI * r;
cout << NEWLINE;
Defined constants (#define)
 You can define your own names for constants simply by using the
#define preprocessor directive.
 #define identifier value
 Example
 #define PI 3.14159265
 #define NEWLINE 'n'
 #define WIDTH 100
Operators
 Input stream object
 >> (stream extraction operator)
Used with std::cin
Waits for user to input value, then press Enter (Return) key
Stores value in variable to right of operator
Converts value to variable data type
 = (assignment operator)
 Assigns value to variable
 Binary operator (two operands)
 Example:
sum = variable1 + variable2;
17
Arithmetic operators
 The five arithmetical operations
+ addition
- subtraction
* multiplication
/ division
% module
Example
int a, b;
a = 10;
b = 4;
a = b;
b = 7;
a = 2 + (b = 5);
a = b = c = 5;
a -= 5;
a /= b;
a++;
a+=1;
a=a+1;
B=3;
A=++B;
A=B++;
Relational operators
 == Equal
 != Different
 > Greater than
 < Less than
 >= Greater or equal than
 <= Less or equal than
 (7 == 5)
 (5 > 4)
 (3 != 2)
 (6 >= 6)
 Suppose that a=2, b=3and
c=6,
 (a == 5)
 (a*b >= c)
 (b+4 > a*c)
 ((b=2) == a)
Logic operators ( !, &&, || )
 For example:
 ( (5 == 5) && (3 > 6) )
 ( (5 == 5) || (3 > 6))
 Conditional operator ( ? )
 condition ? result1 : result2
 if condition is true the expression will return result1, if not it
will return result2.
 7==5 ? 4 : 3
 7==5+2 ? 4 : 3
 5>3 ? a : b
Precedence of Operators
 Rules of operator precedence
 Operators in parentheses evaluated first
 Nested/embedded parentheses
 Operators in innermost pair first
 Multiplication, division, modulus applied next
 Operators applied from left to right
 Addition, subtraction applied last
 Operators applied from left to right
22
Operator(s) Operation(s) Order of evaluation (precedence)
() Parentheses Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated first. If
there are several pairs of parentheses “on the same level”
(i.e., not nested), they are evaluated left to right.
*, /, or % Multiplication Division
Modulus
Evaluated second. If there are several, they re
evaluated left to right.
+ or - Addition
Subtraction
Evaluated last. If there are several, they are
evaluated left to right.
Types of Errors
 Syntax errors
A syntax error occurs when the programmer fails to obey one of the grammar
rules of the language.
 Runtime errors
A runtime error occurs whenever the program instructs the computer to do
something that it is either incapable or unwilling to do.
 Logic errors
Logic errors are usually the most difficult kind of errors to find and fix, because
there frequently is no obvious indication of the error.
Usually the program runs successfully. It simply doesn't behave as it should.
it doesn't produce the correct answers.
Syntax errors
Syntax Error: undeclared identifer “cout”
Syntax errors
 result = (firstVal - secondVal / factor;
Syntax Error: ’)’ expected
 cout << “Execution Terminated << endl;
Syntax Error: illegal string constant
 double x = 2.0, y = 3.1415, product;
x * y = product;
Syntax Error: not an l-value
Logical errors
 int a, b;
int sum = a + b;
cout << "Enter two numbers to add: ";
cin >> a; cin >> b;
cout << "The sum is: " << sum;
 char done = 'Y';
while (done = 'Y') {
//... cout << "Continue? (Y/N)";
cin >> done;
}

More Related Content

PPTX
Introduction to C Programming
PPT
C language basics
PDF
Constants Variables Datatypes by Mrs. Sowmya Jyothi
PPT
C program
PPT
pointer, structure ,union and intro to file handling
PDF
Programming in C- Introduction
ODP
Basic C Programming language
Introduction to C Programming
C language basics
Constants Variables Datatypes by Mrs. Sowmya Jyothi
C program
pointer, structure ,union and intro to file handling
Programming in C- Introduction
Basic C Programming language

What's hot (20)

PPTX
Structure of c_program_to_input_output
PPT
Introduction to Basic C programming 01
PDF
Introduction to c language
ODP
OpenGurukul : Language : C Programming
PDF
Python Programming
PPT
Basic of c language
PPTX
Fundamentals of c programming
PDF
Learning c - An extensive guide to learn the C Language
PPT
Unit 4 Foc
PPT
C material
PPSX
Complete C programming Language Course
PDF
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
DOC
1. introduction to computer
PDF
C programming language
PPTX
Programming in C Basics
PPTX
Unit ii
PPTX
C Programming basics
PPS
Learn C
ODP
CProgrammingTutorial
PPTX
C tokens
Structure of c_program_to_input_output
Introduction to Basic C programming 01
Introduction to c language
OpenGurukul : Language : C Programming
Python Programming
Basic of c language
Fundamentals of c programming
Learning c - An extensive guide to learn the C Language
Unit 4 Foc
C material
Complete C programming Language Course
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
1. introduction to computer
C programming language
Programming in C Basics
Unit ii
C Programming basics
Learn C
CProgrammingTutorial
C tokens
Ad

Similar to C++ lecture 01 (20)

PDF
Lecture1
PPTX
Lecture 1 Introduction C++
PPT
Lecture#2 Computer languages computer system and Programming EC-105
PPT
Pengaturcaraan asas
PPTX
#Code2 create c++ for beginners
PPTX
lec 2.pptx
PDF
Basic Elements of C++
PDF
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
PPTX
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
PDF
4. programing 101
PPT
02a fundamental c++ types, arithmetic
PPT
Chapter02-S11.ppt
PPSX
Complete C++ programming Language Course
PPT
Savitch ch 02
PPT
Operators_in_C++_advantages_applications.ppt
PPT
Savitch Ch 02
PPT
Savitch Ch 02
PPTX
Chap_________________1_Introduction.pptx
PPT
Chapter 3 Expressions and Inteactivity
Lecture1
Lecture 1 Introduction C++
Lecture#2 Computer languages computer system and Programming EC-105
Pengaturcaraan asas
#Code2 create c++ for beginners
lec 2.pptx
Basic Elements of C++
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
4. programing 101
02a fundamental c++ types, arithmetic
Chapter02-S11.ppt
Complete C++ programming Language Course
Savitch ch 02
Operators_in_C++_advantages_applications.ppt
Savitch Ch 02
Savitch Ch 02
Chap_________________1_Introduction.pptx
Chapter 3 Expressions and Inteactivity
Ad

More from HNDE Labuduwa Galle (8)

PPTX
Lecture 07 networking
PPTX
Lecture 04 networking
PPTX
Lecture 03 networking
PPTX
Lecture 02 networking
PPTX
Lecture 01 networking
PPTX
C++ lecture 04
PPTX
C++ lecture 03
PPTX
C++ lecture 02
Lecture 07 networking
Lecture 04 networking
Lecture 03 networking
Lecture 02 networking
Lecture 01 networking
C++ lecture 04
C++ lecture 03
C++ lecture 02

Recently uploaded (20)

PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
anatomy of limbus and anterior chamber .pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPTX
web development for engineering and engineering
PDF
composite construction of structures.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
DOCX
573137875-Attendance-Management-System-original
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
PDF
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
PPT
Project quality management in manufacturing
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
anatomy of limbus and anterior chamber .pptx
Operating System & Kernel Study Guide-1 - converted.pdf
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
web development for engineering and engineering
composite construction of structures.pdf
OOP with Java - Java Introduction (Basics)
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
573137875-Attendance-Management-System-original
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
Project quality management in manufacturing

C++ lecture 01

  • 2. History of C and C++  History of C++  Extension of C  Early 1980s: Bjarne Stroustrup (Bell Laboratories)  Provides capabilities for object-oriented programming Objects: reusable software components Model items in real world Object-oriented programs Easy to understand, correct and modify  Hybrid language C-like style Object-oriented style Both 2
  • 3. Basics of a Typical C++ Environment  C++ systems  Program-development environment  Language  C++ Standard Library 3
  • 4. Basics of a Typical C++ Environment4 Phases of C++ Programs: 1. Edit 2. Preprocess 3. Compile 4. Link 5. Load 6. Execute Loader Primary Memory Program is created in the editor and stored on disk. Preprocessor program processes the code. Loader puts program in memory. CPU takes each instruction and executes it, possibly storing new data values as the program executes. Compiler Compiler creates object code and stores it on disk. Linker links the object code with the libraries, creates a.out and stores it on disk Editor Preprocessor Linker CPU Primary Memory . . . . . . . . . . . . Disk Disk Disk Disk Disk
  • 5. Hello World This is a comment line - the line is a brief description of program does. directives for the preprocessor. They are not executable code lines but indications for the compiler. <iostream.h> tells the compiler's preprocessor to include the iostream standard header file. cout is the standard output stream The return instruction causes the main() function finish and return the code terminating the program without any errors during its execution This line corresponds to the beginning of the main function declaration. The main function is the point C++ programs begin their execution. It is first to be executed when a program starts.
  • 6. Basics of a Typical C++ Environment  Input/output  cin Standard input stream Normally keyboard  cout Standard output stream Normally computer screen  cerr Standard error stream Display error messages 6
  • 7. Comments  It is of 2 types:-  Single line comments //  example of single line comment //this is the very simple example of single line comments  Multi line comments /* This is the example of multi line comment */
  • 8. A Simple Program: Printing a Line of Text 8 Escape Sequence Description n Newline. Position the screen cursor to the beginning of the next line. t Horizontal tab. Move the screen cursor to the next tab stop. r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. a Alert. Sound the system bell. Backslash. Used to print a backslash character. " Double quote. Used to print a double quote character.
  • 9. Variables  Variable names Valid identifier Series of characters (letters, digits, underscores) Cannot begin with digit Case sensitive length of an identifier is not limited, (but some compilers only the 32 first characters rest ignore) Neither spaces nor marked letters can be part of an identifier can also begin with an underline character ( _ ) your own identifiers cannot match any key word of the C++ language 9
  • 10. Variables  Location in memory where value can be stored  Common data types int - integer numbers char - characters double - double precision floating point numbers Float - floating point numbers Bool - Boolean value (true or false) 10
  • 11. Signed and Unsigned Integer Types  For integer data types, there are three sizes:  Int  long - larger size of integer,  short, which are declared as long int and short int.  The keywords long and short are called sub-type qualifiers.  The requirement is that short and int must be at least 16 bits, long must be at least 32 bits,  short is no longer than int, which is no longer than long.  Typically, short is 16 bits, long is 32 bits, and int is either 16 or 32 bits.  all integer data types are signed data types,  i.e. they have values which can be positive or negative
  • 12. Declaration of variables  Declare variables with name and data type before use int a; float mynumber; int MyAccountBalance; int integer1; int integer2; int sum; int integer1, integer2, sum;
  • 13. Example  A program to add two numbers; #include<iostream> using namespace std; int main() { int a, b; // variable declaration a = 10; b= 20; int addition = a + b; cout << “answer is:” << addition << endl; return 0; }
  • 14. Scope of variables Global variables can be referred to anywhere in the code, within any function, whenever it is after its declaration. The scope of the local variables is limited to the code level in which they are declared.
  • 15. Constants: Literals  A constant is any expression that has a fixed value.  It can be Integer Numbers, Floating-Point Numbers, Characters and Strings  Examples  75 // decimal  0113 // octal  0x4b // hexadecimal  6.02e23 // 6.02 x 10 23  1.6e-19 // 1.6 x 10 -19  3.0 // 3.0  #define PI 3.14159265  #define NEWLINE 'n' circle = 2 * PI * r; cout << NEWLINE;
  • 16. Defined constants (#define)  You can define your own names for constants simply by using the #define preprocessor directive.  #define identifier value  Example  #define PI 3.14159265  #define NEWLINE 'n'  #define WIDTH 100
  • 17. Operators  Input stream object  >> (stream extraction operator) Used with std::cin Waits for user to input value, then press Enter (Return) key Stores value in variable to right of operator Converts value to variable data type  = (assignment operator)  Assigns value to variable  Binary operator (two operands)  Example: sum = variable1 + variable2; 17
  • 18. Arithmetic operators  The five arithmetical operations + addition - subtraction * multiplication / division % module
  • 19. Example int a, b; a = 10; b = 4; a = b; b = 7; a = 2 + (b = 5); a = b = c = 5; a -= 5; a /= b; a++; a+=1; a=a+1; B=3; A=++B; A=B++;
  • 20. Relational operators  == Equal  != Different  > Greater than  < Less than  >= Greater or equal than  <= Less or equal than  (7 == 5)  (5 > 4)  (3 != 2)  (6 >= 6)  Suppose that a=2, b=3and c=6,  (a == 5)  (a*b >= c)  (b+4 > a*c)  ((b=2) == a)
  • 21. Logic operators ( !, &&, || )  For example:  ( (5 == 5) && (3 > 6) )  ( (5 == 5) || (3 > 6))  Conditional operator ( ? )  condition ? result1 : result2  if condition is true the expression will return result1, if not it will return result2.  7==5 ? 4 : 3  7==5+2 ? 4 : 3  5>3 ? a : b
  • 22. Precedence of Operators  Rules of operator precedence  Operators in parentheses evaluated first  Nested/embedded parentheses  Operators in innermost pair first  Multiplication, division, modulus applied next  Operators applied from left to right  Addition, subtraction applied last  Operators applied from left to right 22 Operator(s) Operation(s) Order of evaluation (precedence) () Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level” (i.e., not nested), they are evaluated left to right. *, /, or % Multiplication Division Modulus Evaluated second. If there are several, they re evaluated left to right. + or - Addition Subtraction Evaluated last. If there are several, they are evaluated left to right.
  • 23. Types of Errors  Syntax errors A syntax error occurs when the programmer fails to obey one of the grammar rules of the language.  Runtime errors A runtime error occurs whenever the program instructs the computer to do something that it is either incapable or unwilling to do.  Logic errors Logic errors are usually the most difficult kind of errors to find and fix, because there frequently is no obvious indication of the error. Usually the program runs successfully. It simply doesn't behave as it should. it doesn't produce the correct answers.
  • 24. Syntax errors Syntax Error: undeclared identifer “cout”
  • 25. Syntax errors  result = (firstVal - secondVal / factor; Syntax Error: ’)’ expected  cout << “Execution Terminated << endl; Syntax Error: illegal string constant  double x = 2.0, y = 3.1415, product; x * y = product; Syntax Error: not an l-value
  • 26. Logical errors  int a, b; int sum = a + b; cout << "Enter two numbers to add: "; cin >> a; cin >> b; cout << "The sum is: " << sum;  char done = 'Y'; while (done = 'Y') { //... cout << "Continue? (Y/N)"; cin >> done; }