SlideShare a Scribd company logo
1 of 24
CHAPTER 1.2
INTRODUCTION TO C++ PROGRAMMING
Dr. Shady Yehia Elmashad
Outline
1. Introduction to C++ Programming
2. Comment
3. Variables and Constants
4. Basic C++ Data Types
5. Simple Program: Printing a Line of Text
6. Simple Program: Adding Two Integers
7. a Simple Program: Calculating the area of a Circle
-+
1. Introduction to C++ Programming
• C++ language
- Facilitates a structured and disciplined approach to
computer program design
• Following are several examples
- The examples illustrate many important features of C++
- Each example is analyzed one statement at a time.
 2000 Prentice Hall, Inc. All rights
Outline
4
1. Comments
2. Load <iostream>
3. main
3.1 Print "Welcome
to C++n"
3.2 exit (return 0)
Program Output
1 // Fig. 1.2: fig01_02.cpp
2 // A first program in C++
3 #include <iostream>
4
5 int main()
6 {
7 cout << "Welcome to C++!n";
8
9 return 0; // indicate that program
ended successfully
10}
Welcome to C++!
preprocessor directive
Message to the C++ preprocessor.
Lines beginning with # are preprocessor directives.
#include <iostream> tells the preprocessor to
include the contents of the file <iostream>, which
includes input/output operations (such as printing to
the screen).
Comments
Written between /* and */ or following a //.
Improve program readability and do not cause the
computer to perform any action.
C++ programs contain one or more functions, one of
which must be main
Parenthesis are used to indicate a function
int means that main "returns" an integer value.
A left brace { begins the body of every function
and a right brace } ends it.
Prints the string of characters contained between the
quotation marks.
The entire line, including std::cout, the <<
operator, the string "Welcome to C++!n" and
the semicolon (;), is called a statement.
All statements must end with a semicolon.
return is a way to exit a function
from a function.
return 0, in this case, means that
the program terminated normally.
2. Comment
• Message to everyone who reads source program and
is used to document source code.
• Makes the program more readable and eye catching.
• Non executable statement in the C++.
• Always neglected by compiler.
• Can be written anywhere and any number of times.
• Use as many comments as possible in C++ program.
2. Comment
1. Single Line Comment
•starts with “//” symbol.
•Remaining line after “//” symbol is ignored by browser.
•End of Line is considered as End of the comment.
2. Multiple Line Comment (Block Comment)
•starts with “/*” symbol.
•ends with “*/” symbol.
Types of comment
2. Comment
Example
/* this program calculate the sum of
two numbers */
#include<iostream> // header file
using namespace std;
int main( ) // ‫الرئيسية‬ ‫الدالة‬
{
int x, y , sum ; // declaration part
/* read the two numbers */
cin >> x >> y ;
// calculate the sum
sum = x + y ;
// print the result
cout << sum ;
return 0;
}
3. Variables and Constants
Variables
• Variables are memory location in computer's memory to
store data.
• Each variable should be given a unique name called
identifier, to indicate the memory location in addition to a
data type.
• Variable names are just the symbolic representation of a
memory location.
• Variable value can be changed during program execution
3. Variables and Constants
Variables Declaration
variable_type variable_name;
Example: int a; - Declares a variable named a of type int
int a, b, c; - Declares three variables, each of type int
int a; float b;
3. Variables and Constants
Constants
• Constant is the term that has a unique value and can't be
changed during the program execution.
• Declaration:
1. #define constant_name constant_value
Example: #define PI 3.14
2. const constant_type constant_name = constant_value ;
Example: const float PI = 3.14;
3. Variables and Constants
Variables and Constants Names
• Can be composed of letters (both uppercase and
lowercase letters), digits and underscore '_' only.
• Must begin with a letter or underscore ‘_’.
• Don’t contain space or special character:
(#, *, ?, -, @, !, $, %,&, space,……)
• Can’t be one of the reserved words (they are used by
the compiler so they are not available for re-definition
or overloading.)
3. Variables and Constants
Reserved Words Examples
int float double char
string short long signed
for while if switch
break default do else
case return sizeof static
continue goto true false
const void private struct
class cin cout new
3. Variables and Constants
Reserved Words Examples
• Which of the following variable names are valid/not valid
and why if not?
Valid or not
Name
Valid or not
Name
10rate
area
Shoubra faculty
shoubra_faculty
W#d
w234
1233
Ahmed
Cin
A3
Shoubra-faculty
A_3
int
temp
 2000 Prentice Hall, Inc. All rights
Outline
14
1. Initialize const
variable
2. Attempt to modify
variable
Program Output
1 // Fig. 4.7: fig04_07.cpp
2 // A const object must be initialized
3
4 int main()
5 {
6 const int x; // Error: x must be
initialized
7
8 x = 7; // Error: cannot modify a
const variable
9
10 return 0;
11}
Fig04_07.cpp:
Error E2304 Fig04_07.cpp 6: Constant variable
'x' must be
initialized in function main()
Error E2024 Fig04_07.cpp 8: Cannot modify a
const object in
function main()
*** 2 errors in Compile ***
Notice that const variables must be
initialized because they cannot be modified
later.
4. Basic C++ Data Types
Keyword
Type
short - int - long
Integer
float - double - long double
Real
char
Character
string
String
bool
Boolean
4. Basic C++ Data Types
• Real: hold numbers that have fractional part with different
levels of precision, depending on which of the three
floating-point types is used.
Example: float PI = 3.14;
• Character: hold a single character such as ‘a’, ‘A’ and ‘$’.
Example: char ch = ‘a’;
• String: store sequences of characters, such as words or
sentences.
Example: string mystring = "This is a string";
• Boolean: hold a Boolean value. It may be assigned an
integer value 1 (true) or a value 0 (false).
Example: bool status;
4. Basic C++ Data Types
typedef Declarations
• You can rename an existing type using typedef.
typedef type freshname;
• For example, this tells the compiler that number is
another name for int:
typedef int number;
• Therefore, the following declaration is perfectly legal
and creates an integer variable called distance:
number distance;
5. a Simple Program:
Printing a Line of Text
• std::cout
 Standard output stream object
 “Connected” to the screen
 std:: specifies the "namespace" which cout belongs to
- std:: can be removed through the use of using statements
• <<
 Stream insertion operator
 Value to the right of the operator (right operand) inserted into
output stream (which is connected to the screen)
 std::cout << “Welcome to C++!n”;
• 
 Escape character
 Indicates that a “special” character is to be output
5. a Simple Program:
Printing a Line of Text
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.
 2000 Prentice Hall, Inc. All rights
Outline
20
1. Load <iostream>
2. main
2.1 Print "Welcome"
2.2 Print "to C++!"
2.3 newline
2.4 exit (return 0)
Program Output
Welcome to C++!
1 // Fig. 1.4: fig01_04.cpp
2 // Printing a line with multiple statements
3 #include <iostream>
4
5 int main()
6 {
7 cout << "Welcome ";
8 cout << "to C++!n";
9
10 return 0; // indicate that program ended
successfully
11}
Unless new line 'n' is specified, the text continues
on the same line.
 2000 Prentice Hall, Inc. All rights
Outline
21
1. Load <iostream>
2. main
2.1 Print "Welcome"
2.2 newline
2.3 Print "to"
2.4 newline
2.5 newline
2.6 Print "C++!"
2.7 newline
2.8 exit (return 0)
Program Output
1 // Fig. 1.5: fig01_05.cpp
2 // Printing multiple lines with a single
statement
3 #include <iostream>
4
5 int main()
6 {
7 cout << "WelcomentonnC++!n";
8
9 return 0; // indicate that program ended
successfully
10}
Welcome
to
C++!
Multiple lines can be printed with one
statement.
6. a Simple Program:
Adding Two Integers
• (stream extraction operator)
 When used with std::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;
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;
 2000 Prentice Hall, Inc. All rights
Outline
23
.1
Load <iostream>
2. main
2.1 Initialize variables
integer1,
integer2, and sum
2.2 Print "Enter
first integer"
2.2.1 Get input
2.3 Print "Enter
second integer"
2.3.1 Get input
2.4 Add variables and
put result into sum
2.5 Print "Sum is"
2.5.1 Output sum
2.6 exit (return 0)
Program Output
1 // Fig. 1.6: fig01_06.cpp
2 // Addition program
3 #include <iostream>
4
5 int 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 << std::endl; //
print sum
15
16 return 0; // indicate that program ended
successfully
17}
Enter first integer
45
Enter second integer
72
Sum is 117
Notice how std::cin is used to get user
input.
Variables can be output using std::cout << variableName.
std::endl flushes the buffer and
prints a newline.
7. a Simple Program:
Calculating the area of a Circle
# include <iostream>
# define PI 3.14
using namespace std;
int main ( )
{ /* This program asks the user to enter a radius then calculate the
area */
float radius, Area;
cout<< “ Please enter a radius: “ ;
cin>> radius;
Area = PI * radius * radius ;
cout<< “ The area of the circle is ” << Area ;
return 0;
}
 Write a program to calculate the volume of a sphere.

More Related Content

Similar to Introduction to C++ lecture ************

Similar to Introduction to C++ lecture ************ (20)

Lecture # 1 - Introduction Revision - 1 OOPS.pptx
Lecture # 1 - Introduction  Revision - 1 OOPS.pptxLecture # 1 - Introduction  Revision - 1 OOPS.pptx
Lecture # 1 - Introduction Revision - 1 OOPS.pptx
 
Chapter02-S11.ppt
Chapter02-S11.pptChapter02-S11.ppt
Chapter02-S11.ppt
 
Chap 2 c++
Chap 2 c++Chap 2 c++
Chap 2 c++
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
Cinfo
CinfoCinfo
Cinfo
 
CHAPTER-2.ppt
CHAPTER-2.pptCHAPTER-2.ppt
CHAPTER-2.ppt
 
Basics Of C++.pptx
Basics Of C++.pptxBasics Of C++.pptx
Basics Of C++.pptx
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++
 
C++ Overview
C++ OverviewC++ Overview
C++ Overview
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
 
C programming
C programmingC programming
C programming
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
C++ Constructs.pptx
C++ Constructs.pptxC++ Constructs.pptx
C++ Constructs.pptx
 
C chap02
C chap02C chap02
C chap02
 
C chap02
C chap02C chap02
C chap02
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteaching
 
Lecture # 1 introduction revision - 1
Lecture # 1   introduction  revision - 1Lecture # 1   introduction  revision - 1
Lecture # 1 introduction revision - 1
 

Recently uploaded

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 

Recently uploaded (20)

OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 

Introduction to C++ lecture ************

  • 1. CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING Dr. Shady Yehia Elmashad
  • 2. Outline 1. Introduction to C++ Programming 2. Comment 3. Variables and Constants 4. Basic C++ Data Types 5. Simple Program: Printing a Line of Text 6. Simple Program: Adding Two Integers 7. a Simple Program: Calculating the area of a Circle
  • 3. -+ 1. Introduction to C++ Programming • C++ language - Facilitates a structured and disciplined approach to computer program design • Following are several examples - The examples illustrate many important features of C++ - Each example is analyzed one statement at a time.
  • 4.  2000 Prentice Hall, Inc. All rights Outline 4 1. Comments 2. Load <iostream> 3. main 3.1 Print "Welcome to C++n" 3.2 exit (return 0) Program Output 1 // Fig. 1.2: fig01_02.cpp 2 // A first program in C++ 3 #include <iostream> 4 5 int main() 6 { 7 cout << "Welcome to C++!n"; 8 9 return 0; // indicate that program ended successfully 10} Welcome to C++! preprocessor directive Message to the C++ preprocessor. Lines beginning with # are preprocessor directives. #include <iostream> tells the preprocessor to include the contents of the file <iostream>, which includes input/output operations (such as printing to the screen). Comments Written between /* and */ or following a //. Improve program readability and do not cause the computer to perform any action. C++ programs contain one or more functions, one of which must be main Parenthesis are used to indicate a function int means that main "returns" an integer value. A left brace { begins the body of every function and a right brace } ends it. Prints the string of characters contained between the quotation marks. The entire line, including std::cout, the << operator, the string "Welcome to C++!n" and the semicolon (;), is called a statement. All statements must end with a semicolon. return is a way to exit a function from a function. return 0, in this case, means that the program terminated normally.
  • 5. 2. Comment • Message to everyone who reads source program and is used to document source code. • Makes the program more readable and eye catching. • Non executable statement in the C++. • Always neglected by compiler. • Can be written anywhere and any number of times. • Use as many comments as possible in C++ program.
  • 6. 2. Comment 1. Single Line Comment •starts with “//” symbol. •Remaining line after “//” symbol is ignored by browser. •End of Line is considered as End of the comment. 2. Multiple Line Comment (Block Comment) •starts with “/*” symbol. •ends with “*/” symbol. Types of comment
  • 7. 2. Comment Example /* this program calculate the sum of two numbers */ #include<iostream> // header file using namespace std; int main( ) // ‫الرئيسية‬ ‫الدالة‬ { int x, y , sum ; // declaration part /* read the two numbers */ cin >> x >> y ; // calculate the sum sum = x + y ; // print the result cout << sum ; return 0; }
  • 8. 3. Variables and Constants Variables • Variables are memory location in computer's memory to store data. • Each variable should be given a unique name called identifier, to indicate the memory location in addition to a data type. • Variable names are just the symbolic representation of a memory location. • Variable value can be changed during program execution
  • 9. 3. Variables and Constants Variables Declaration variable_type variable_name; Example: int a; - Declares a variable named a of type int int a, b, c; - Declares three variables, each of type int int a; float b;
  • 10. 3. Variables and Constants Constants • Constant is the term that has a unique value and can't be changed during the program execution. • Declaration: 1. #define constant_name constant_value Example: #define PI 3.14 2. const constant_type constant_name = constant_value ; Example: const float PI = 3.14;
  • 11. 3. Variables and Constants Variables and Constants Names • Can be composed of letters (both uppercase and lowercase letters), digits and underscore '_' only. • Must begin with a letter or underscore ‘_’. • Don’t contain space or special character: (#, *, ?, -, @, !, $, %,&, space,……) • Can’t be one of the reserved words (they are used by the compiler so they are not available for re-definition or overloading.)
  • 12. 3. Variables and Constants Reserved Words Examples int float double char string short long signed for while if switch break default do else case return sizeof static continue goto true false const void private struct class cin cout new
  • 13. 3. Variables and Constants Reserved Words Examples • Which of the following variable names are valid/not valid and why if not? Valid or not Name Valid or not Name 10rate area Shoubra faculty shoubra_faculty W#d w234 1233 Ahmed Cin A3 Shoubra-faculty A_3 int temp
  • 14.  2000 Prentice Hall, Inc. All rights Outline 14 1. Initialize const variable 2. Attempt to modify variable Program Output 1 // Fig. 4.7: fig04_07.cpp 2 // A const object must be initialized 3 4 int main() 5 { 6 const int x; // Error: x must be initialized 7 8 x = 7; // Error: cannot modify a const variable 9 10 return 0; 11} Fig04_07.cpp: Error E2304 Fig04_07.cpp 6: Constant variable 'x' must be initialized in function main() Error E2024 Fig04_07.cpp 8: Cannot modify a const object in function main() *** 2 errors in Compile *** Notice that const variables must be initialized because they cannot be modified later.
  • 15. 4. Basic C++ Data Types Keyword Type short - int - long Integer float - double - long double Real char Character string String bool Boolean
  • 16. 4. Basic C++ Data Types • Real: hold numbers that have fractional part with different levels of precision, depending on which of the three floating-point types is used. Example: float PI = 3.14; • Character: hold a single character such as ‘a’, ‘A’ and ‘$’. Example: char ch = ‘a’; • String: store sequences of characters, such as words or sentences. Example: string mystring = "This is a string"; • Boolean: hold a Boolean value. It may be assigned an integer value 1 (true) or a value 0 (false). Example: bool status;
  • 17. 4. Basic C++ Data Types typedef Declarations • You can rename an existing type using typedef. typedef type freshname; • For example, this tells the compiler that number is another name for int: typedef int number; • Therefore, the following declaration is perfectly legal and creates an integer variable called distance: number distance;
  • 18. 5. a Simple Program: Printing a Line of Text • std::cout  Standard output stream object  “Connected” to the screen  std:: specifies the "namespace" which cout belongs to - std:: can be removed through the use of using statements • <<  Stream insertion operator  Value to the right of the operator (right operand) inserted into output stream (which is connected to the screen)  std::cout << “Welcome to C++!n”; •  Escape character  Indicates that a “special” character is to be output
  • 19. 5. a Simple Program: Printing a Line of Text 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.
  • 20.  2000 Prentice Hall, Inc. All rights Outline 20 1. Load <iostream> 2. main 2.1 Print "Welcome" 2.2 Print "to C++!" 2.3 newline 2.4 exit (return 0) Program Output Welcome to C++! 1 // Fig. 1.4: fig01_04.cpp 2 // Printing a line with multiple statements 3 #include <iostream> 4 5 int main() 6 { 7 cout << "Welcome "; 8 cout << "to C++!n"; 9 10 return 0; // indicate that program ended successfully 11} Unless new line 'n' is specified, the text continues on the same line.
  • 21.  2000 Prentice Hall, Inc. All rights Outline 21 1. Load <iostream> 2. main 2.1 Print "Welcome" 2.2 newline 2.3 Print "to" 2.4 newline 2.5 newline 2.6 Print "C++!" 2.7 newline 2.8 exit (return 0) Program Output 1 // Fig. 1.5: fig01_05.cpp 2 // Printing multiple lines with a single statement 3 #include <iostream> 4 5 int main() 6 { 7 cout << "WelcomentonnC++!n"; 8 9 return 0; // indicate that program ended successfully 10} Welcome to C++! Multiple lines can be printed with one statement.
  • 22. 6. a Simple Program: Adding Two Integers • (stream extraction operator)  When used with std::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; 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;
  • 23.  2000 Prentice Hall, Inc. All rights Outline 23 .1 Load <iostream> 2. main 2.1 Initialize variables integer1, integer2, and sum 2.2 Print "Enter first integer" 2.2.1 Get input 2.3 Print "Enter second integer" 2.3.1 Get input 2.4 Add variables and put result into sum 2.5 Print "Sum is" 2.5.1 Output sum 2.6 exit (return 0) Program Output 1 // Fig. 1.6: fig01_06.cpp 2 // Addition program 3 #include <iostream> 4 5 int 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 << std::endl; // print sum 15 16 return 0; // indicate that program ended successfully 17} Enter first integer 45 Enter second integer 72 Sum is 117 Notice how std::cin is used to get user input. Variables can be output using std::cout << variableName. std::endl flushes the buffer and prints a newline.
  • 24. 7. a Simple Program: Calculating the area of a Circle # include <iostream> # define PI 3.14 using namespace std; int main ( ) { /* This program asks the user to enter a radius then calculate the area */ float radius, Area; cout<< “ Please enter a radius: “ ; cin>> radius; Area = PI * radius * radius ; cout<< “ The area of the circle is ” << Area ; return 0; }  Write a program to calculate the volume of a sphere.