SlideShare a Scribd company logo
Programming fundamentals
week 2 : Getting Started with C++
Objective
In this chapter, you will:
• Become familiar with the basic components
and syntax of a C++ program. Explore
simple data types
• Variables declaration
• Memory used by data types
• Comments
• Escape sequence
Variables and Identifiers
• Variables have names – we call these names
identifiers.
• An identifier must begin with a letter or an
underscore _
• C++ is case sensitive upper case (capital) or
lower-case letters are considered different
characters. Average, average and AVERAGE are
three different identifiers.
• Identifiers cannot be reserved words (special
words like int, main, etc.)
• Two predefined identifiers are cout and cin
4
Variables and Identifiers
(continued)
• The following are legal identifiers in C++:
– first
– conversion
– payrate
Example of illegal identifier
Illegal identifier Description
Employee salary There can be no space between employee and salary
Hello! The exclamation mark cannot be used in an identifier
One+two The symbol + cannot be used in an idenrifie.
2nd An identifier cannot begin with a digit
5
Reserved Words (Keywords)
• Reserved words (also called keywords) are defined with
predefined meaning and syntax in the language
• Include:
• int
• float
• double
• char
• const
• void
• return
6
C++ Fundamental Data Types
• Data type: set of values together with a set of
operations
• C++ data types fall into three categories:
• Primitive data types include integer, float,
character, Boolean.
• Abstract data type include class, structure.
• Derived data types include array, function,
pointer, and reference.
7
C++ Fundamental Data Types
In C++, data types are declarations for variables.
This determines the type and size of data associated
with variables. For example,
int age = 13;
Here, age is a variable of type int. meaning, the
variable can only store integers of either 2 or 4 bytes.
8
C++ Fundamental Data Types
The table below shows the fundamental data types,
their meaning, and their sizes (in bytes):
Data Type Meaning Size (in bytes)
int Integer 2 or 4
float Floating-point 4
double Double floating-point 8
char Character 1
bool Boolean 1
void Empty 0
9
int Data Type
• The int keyword is used to indicate integers.
• Its size is usually 4 bytes. Meaning, it can store
values from -2147483648 to 2147483647
• Examples:
– Int salary = 85000;
• Positive integers do not need a + sign
10
• float and double are used to store floating-point
numbers (decimals and exponentials).
• The size of float is 4 bytes and the size of double
is 8 bytes. Hence, double has two times the
precision of float. To learn more, visit C++ float
and double.
• For example,
– float area = 64.74;
– double volume – 134.64534;
Floating-Point Data Types
11
char Data Type
• Keyword char is used for characters.
• Its size is 1 byte.
• Characters in C++ are enclosed inside single quotes ' '
– 'A', 'a', '0', '*', '+', '$', ‘&’
For example,
char test = ‘h’;
12
bool Data Type
• The bool data type has one of two possible values:
true or false.
• Booleans are used in conditional statements and
loops (which we will learn in later chapters).
• For example,
– bool con = false;
13
void Data Type
• The void keyword indicates an absence of data. It
means "nothing" or "no value".
• We will use void when we learn about functions
and pointers.
– Note: We cannot declare variables of the void type.
14
string Type
• Programmer-defined type supplied in Standard C++ library
• Sequence of zero or more characters
• Enclosed in double quotation marks
• Null: a string with no characters
• Each character has relative position in string
– Position of first character is 0
• Length of a string is number of characters in it
– Example: length of "William Jacob" is 13
15
Form and Style
• Consider two ways of declaring variables:
– Method 1
int feet, inch;
double x, y;
– Method 2
int a,b;double x,y;
• Both are correct; however, the second is hard to
read
Modifiers
• We can further modify some of the fundamentals data
types by using type modifiers. There are 4 type modifiers
in C++. They are:
– signed
– unsigned
– short
– long
Modified Data types List
Data Type Size (in
Bytes)
Meaning
signed int 4 Used for integers (equivalent to int0
unsigned int 4 Can only store positive integers
short 2 Used for small integers(range -32768 to 32767)
unsigned short 2 Used for small positive integers(range 0 to
65.535)
long 4 Used for large integers (equivalent to long int)
long long 8 Used for very large integers
unsigned long
long
8 Used for very large positive integers
uong double 12 Used for large floating-point numbers
signed char 1 Used for characters (range -127 to 127)
18
Use of Blanks
• In C++, you use one or more blanks to
separate numbers when data is input
• Used to separate reserved words and
identifiers from each other and from other
symbols
• Must never appear within a reserved word or
identifier
19
Constants and Variables
• Named constant: memory location whose
content can’t change during execution
• The syntax to declare a named constant is:
• In C++, const is a reserved word.
• Variable: memory location whose content
may change during execution
20
Programming Example:
Variables and Constants
• Variables
int feet; //variable to hold given feet
int inches; //variable to hold given inches
double centimeters; //variable to hold length in
//centimeters
• Named Constant
const double CENTIMETERS_PER_INCH = 2.54;
const int INCHES_PER_FOOT = 12;
21
Whitespaces
• Every C++ program contains whitespaces
– Include blanks, tabs, and newline characters
• Used to separate special symbols, reserved
words, and identifiers
• Proper utilization of whitespaces is
important
– Can be used to make the program readable
22
Declaring & Initializing
Variables
• Ways to place data into a variable:
– Use C++’s assignment statement
•feet = 35;
– Use input (read) statements
•cin >> feet;
23
Declaring & Initializing
Variables
– Use C++’s assignment statement example
int first=13, second=10;
char ch=' ';
double x=12.6;
• All variables must be initialized before they
are used
– But not necessarily during declaration
24
Input (Read) Statement
• cin is used with >> to gather input
• The stream extraction operator is >>
• For example, if miles is a double variable
cin >> miles;
– Causes computer to get a value of type
double
– Places it in the variable miles
25
Input (Read) Statement
(continued)
• Using more than one variable in cin allows
more than one value to be read at a time
• For example, if feet and inches are
variables of type int, a statement such as:
cin >> feet >> inches;
– Inputs two integers from the keyboard
– Places them in variables feet and inches
respectively
26
Output
• The syntax of cout and << is:
– Called an output statement
• The stream insertion operator is <<
• Expression evaluated and its value is
printed at the current cursor position on the
screen
27
Output (continued)
• A manipulator is used to format the output
– endl causes insertion point to move to
beginning of next line
– Example:
28
Output (continued)
• The new line character is 'n'
– May appear anywhere in the string
cout << "Hello there.";
cout << "My name is James.";
• Output:
Hello there.My name is James.
cout << "Hello there.n";
cout << "My name is James.";
• Output :
Hello there.
My name is James.
29
Output (continued)
30
Comments
• Comments are for the reader, not the compiler
• Two types:
– Single line
// This is a C++ program. It prints the sentence:
// Welcome to C++ Programming.
– Multiple line
/*
You can include comments that can
occupy several lines.
*/
31
Documentation
• A well-documented program is easier to
understand and modify
• You use comments to document programs
• Comments should appear in a program to:
– Explain the purpose of the program
– Identify who wrote it
– Explain the purpose of particular statements
32
Preprocessor Directives
• C++ has a small number of operations
• Many functions and symbols needed to run a C++
program are provided as collection of libraries
• Every library has a name and is referred to by a
header file
• Preprocessor directives are commands supplied to
the preprocessor
• All preprocessor commands begin with #
• No semicolon at the end of these commands
33
Preprocessor Directives
(continued)
• Syntax to include a header file:
• For example:
#include <iostream>
The #include is a preprocessor directive used to include files
in our program. This allows us to use cout in our program
to print output on the screen and cin to take input from
user.
34
namespace and Using cin and
cout in a Program
• cin and cout are declared in the header file
iostream, but within std namespace
• To use cin and cout in a program, use the
following two statements:
#include <iostream>
using namespace std;
• using namespace std means that we can use
names for objects and variables from the standard
library.
35
Creating a C++ Program
• C++ program has two parts:
– Preprocessor directives
– The program
• Preprocessor directives and program statements
constitute C++ source code (.cpp)
• Executable code is produced and saved in a file
with the file extension .exe
36
Creating a C++ Program
(continued)
• A C++ program is a collection of functions, one of which
is the function main
• The first line of the function main is called the heading of
the function:
int main()
• The statements enclosed between the curly braces ({ and
}).
• A valid C++ program must have the main() function. The
curly braces indicate the start and the end of the function.
• The execution of code beings from this function.
37
Creating a C++ Program
Example: Body of the Function
Program:
#include <iostream>
using namespace std;
int main(){
cout<<“Hello World”;
}
Output:
Hello World
38
Creating a C++ Program
(continued)
39
Creating a C++ Program
(continued)
Sample Run:
Line 9: firstNum = 18
Line 10: Enter an integer: 15
Line 13: secondNum = 15
Line 15: The new value of firstNum = 60
40
Program Style and Form
• Every C++ program has a function main
• It must also follow the syntax rules
• Other rules serve the purpose of giving
precise meaning to the language
41
Use of Semicolons, Brackets, and
Commas
• All C++ statements end with a semicolon
– Also called a statement terminator
• { and } are not C++ statements
• Commas separate items in a list

More Related Content

PPT
Basics of c++ Programming Language
Ahmad Idrees
 
PPTX
COMPUTER PROGRAMMING LANGUAGE C++ 1.pptx
atokoosetaiwoboluwat
 
PPTX
Chap_________________1_Introduction.pptx
Ronaldo Aditya
 
PPTX
Data Type in C Programming
Qazi Shahzad Ali
 
PPT
Elementary_Of_C++_Programming_Language.ppt
GordanaJovanoska1
 
PPTX
POLITEKNIK MALAYSIA
Aiman Hud
 
PDF
INTRODUCTION TO C PROGRAMMING in basic c language
GOKULKANNANMMECLECTC
 
PPTX
C programming language
Abin Rimal
 
Basics of c++ Programming Language
Ahmad Idrees
 
COMPUTER PROGRAMMING LANGUAGE C++ 1.pptx
atokoosetaiwoboluwat
 
Chap_________________1_Introduction.pptx
Ronaldo Aditya
 
Data Type in C Programming
Qazi Shahzad Ali
 
Elementary_Of_C++_Programming_Language.ppt
GordanaJovanoska1
 
POLITEKNIK MALAYSIA
Aiman Hud
 
INTRODUCTION TO C PROGRAMMING in basic c language
GOKULKANNANMMECLECTC
 
C programming language
Abin Rimal
 

Similar to programming week 2.ppt (20)

PPTX
basics of c programming for naiver.pptx
ssuser96ba7e1
 
PPTX
#Code2 create c++ for beginners
GDGKuwaitGoogleDevel
 
PPTX
INTRODUCTION TO C++.pptx
MamataAnilgod
 
PDF
Module_1_Introduction-to-Problem-Solving.pdf
MaheshKini3
 
PPTX
C programming tutorial for Beginner
sophoeutsen2
 
PPTX
Unit No 2.pptx Basic s of C Programming
Vaibhav Parjane
 
PPTX
c-introduction.pptx
Mangala R
 
PPTX
2. Introduction to 'C' Language (1).pptx
AkshatMuke1
 
PDF
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
sanatahiratoz0to9
 
PPT
C material
tarique472
 
PPTX
C language
Rupanshi rawat
 
PPTX
Data types slides by Faixan
ٖFaiXy :)
 
PPTX
Lecture 2
marvellous2
 
PPTX
C PROGRAMMING LANGUAGE.pptx
AnshSrivastava48
 
PPTX
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
RutviBaraiya
 
PPT
CHAPTER-2.ppt
Tekle12
 
PPTX
C_plus_plus
Ralph Weber
 
PPTX
C++ Introduction to basic C++ IN THIS YOU WOULD KHOW ABOUT BASIC C++
sanatahiratoz0to9
 
PPSX
Programming in C [Module One]
Abhishek Sinha
 
basics of c programming for naiver.pptx
ssuser96ba7e1
 
#Code2 create c++ for beginners
GDGKuwaitGoogleDevel
 
INTRODUCTION TO C++.pptx
MamataAnilgod
 
Module_1_Introduction-to-Problem-Solving.pdf
MaheshKini3
 
C programming tutorial for Beginner
sophoeutsen2
 
Unit No 2.pptx Basic s of C Programming
Vaibhav Parjane
 
c-introduction.pptx
Mangala R
 
2. Introduction to 'C' Language (1).pptx
AkshatMuke1
 
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
sanatahiratoz0to9
 
C material
tarique472
 
C language
Rupanshi rawat
 
Data types slides by Faixan
ٖFaiXy :)
 
Lecture 2
marvellous2
 
C PROGRAMMING LANGUAGE.pptx
AnshSrivastava48
 
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
RutviBaraiya
 
CHAPTER-2.ppt
Tekle12
 
C_plus_plus
Ralph Weber
 
C++ Introduction to basic C++ IN THIS YOU WOULD KHOW ABOUT BASIC C++
sanatahiratoz0to9
 
Programming in C [Module One]
Abhishek Sinha
 
Ad

Recently uploaded (20)

PPTX
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPTX
Presentation about variables and constant.pptx
safalsingh810
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
PDF
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PDF
Become an Agentblazer Champion Challenge
Dele Amefo
 
PDF
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
Presentation about variables and constant.pptx
safalsingh810
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
Become an Agentblazer Champion Challenge
Dele Amefo
 
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
Ad

programming week 2.ppt

  • 1. Programming fundamentals week 2 : Getting Started with C++
  • 2. Objective In this chapter, you will: • Become familiar with the basic components and syntax of a C++ program. Explore simple data types • Variables declaration • Memory used by data types • Comments • Escape sequence
  • 3. Variables and Identifiers • Variables have names – we call these names identifiers. • An identifier must begin with a letter or an underscore _ • C++ is case sensitive upper case (capital) or lower-case letters are considered different characters. Average, average and AVERAGE are three different identifiers. • Identifiers cannot be reserved words (special words like int, main, etc.) • Two predefined identifiers are cout and cin
  • 4. 4 Variables and Identifiers (continued) • The following are legal identifiers in C++: – first – conversion – payrate Example of illegal identifier Illegal identifier Description Employee salary There can be no space between employee and salary Hello! The exclamation mark cannot be used in an identifier One+two The symbol + cannot be used in an idenrifie. 2nd An identifier cannot begin with a digit
  • 5. 5 Reserved Words (Keywords) • Reserved words (also called keywords) are defined with predefined meaning and syntax in the language • Include: • int • float • double • char • const • void • return
  • 6. 6 C++ Fundamental Data Types • Data type: set of values together with a set of operations • C++ data types fall into three categories: • Primitive data types include integer, float, character, Boolean. • Abstract data type include class, structure. • Derived data types include array, function, pointer, and reference.
  • 7. 7 C++ Fundamental Data Types In C++, data types are declarations for variables. This determines the type and size of data associated with variables. For example, int age = 13; Here, age is a variable of type int. meaning, the variable can only store integers of either 2 or 4 bytes.
  • 8. 8 C++ Fundamental Data Types The table below shows the fundamental data types, their meaning, and their sizes (in bytes): Data Type Meaning Size (in bytes) int Integer 2 or 4 float Floating-point 4 double Double floating-point 8 char Character 1 bool Boolean 1 void Empty 0
  • 9. 9 int Data Type • The int keyword is used to indicate integers. • Its size is usually 4 bytes. Meaning, it can store values from -2147483648 to 2147483647 • Examples: – Int salary = 85000; • Positive integers do not need a + sign
  • 10. 10 • float and double are used to store floating-point numbers (decimals and exponentials). • The size of float is 4 bytes and the size of double is 8 bytes. Hence, double has two times the precision of float. To learn more, visit C++ float and double. • For example, – float area = 64.74; – double volume – 134.64534; Floating-Point Data Types
  • 11. 11 char Data Type • Keyword char is used for characters. • Its size is 1 byte. • Characters in C++ are enclosed inside single quotes ' ' – 'A', 'a', '0', '*', '+', '$', ‘&’ For example, char test = ‘h’;
  • 12. 12 bool Data Type • The bool data type has one of two possible values: true or false. • Booleans are used in conditional statements and loops (which we will learn in later chapters). • For example, – bool con = false;
  • 13. 13 void Data Type • The void keyword indicates an absence of data. It means "nothing" or "no value". • We will use void when we learn about functions and pointers. – Note: We cannot declare variables of the void type.
  • 14. 14 string Type • Programmer-defined type supplied in Standard C++ library • Sequence of zero or more characters • Enclosed in double quotation marks • Null: a string with no characters • Each character has relative position in string – Position of first character is 0 • Length of a string is number of characters in it – Example: length of "William Jacob" is 13
  • 15. 15 Form and Style • Consider two ways of declaring variables: – Method 1 int feet, inch; double x, y; – Method 2 int a,b;double x,y; • Both are correct; however, the second is hard to read
  • 16. Modifiers • We can further modify some of the fundamentals data types by using type modifiers. There are 4 type modifiers in C++. They are: – signed – unsigned – short – long
  • 17. Modified Data types List Data Type Size (in Bytes) Meaning signed int 4 Used for integers (equivalent to int0 unsigned int 4 Can only store positive integers short 2 Used for small integers(range -32768 to 32767) unsigned short 2 Used for small positive integers(range 0 to 65.535) long 4 Used for large integers (equivalent to long int) long long 8 Used for very large integers unsigned long long 8 Used for very large positive integers uong double 12 Used for large floating-point numbers signed char 1 Used for characters (range -127 to 127)
  • 18. 18 Use of Blanks • In C++, you use one or more blanks to separate numbers when data is input • Used to separate reserved words and identifiers from each other and from other symbols • Must never appear within a reserved word or identifier
  • 19. 19 Constants and Variables • Named constant: memory location whose content can’t change during execution • The syntax to declare a named constant is: • In C++, const is a reserved word. • Variable: memory location whose content may change during execution
  • 20. 20 Programming Example: Variables and Constants • Variables int feet; //variable to hold given feet int inches; //variable to hold given inches double centimeters; //variable to hold length in //centimeters • Named Constant const double CENTIMETERS_PER_INCH = 2.54; const int INCHES_PER_FOOT = 12;
  • 21. 21 Whitespaces • Every C++ program contains whitespaces – Include blanks, tabs, and newline characters • Used to separate special symbols, reserved words, and identifiers • Proper utilization of whitespaces is important – Can be used to make the program readable
  • 22. 22 Declaring & Initializing Variables • Ways to place data into a variable: – Use C++’s assignment statement •feet = 35; – Use input (read) statements •cin >> feet;
  • 23. 23 Declaring & Initializing Variables – Use C++’s assignment statement example int first=13, second=10; char ch=' '; double x=12.6; • All variables must be initialized before they are used – But not necessarily during declaration
  • 24. 24 Input (Read) Statement • cin is used with >> to gather input • The stream extraction operator is >> • For example, if miles is a double variable cin >> miles; – Causes computer to get a value of type double – Places it in the variable miles
  • 25. 25 Input (Read) Statement (continued) • Using more than one variable in cin allows more than one value to be read at a time • For example, if feet and inches are variables of type int, a statement such as: cin >> feet >> inches; – Inputs two integers from the keyboard – Places them in variables feet and inches respectively
  • 26. 26 Output • The syntax of cout and << is: – Called an output statement • The stream insertion operator is << • Expression evaluated and its value is printed at the current cursor position on the screen
  • 27. 27 Output (continued) • A manipulator is used to format the output – endl causes insertion point to move to beginning of next line – Example:
  • 28. 28 Output (continued) • The new line character is 'n' – May appear anywhere in the string cout << "Hello there."; cout << "My name is James."; • Output: Hello there.My name is James. cout << "Hello there.n"; cout << "My name is James."; • Output : Hello there. My name is James.
  • 30. 30 Comments • Comments are for the reader, not the compiler • Two types: – Single line // This is a C++ program. It prints the sentence: // Welcome to C++ Programming. – Multiple line /* You can include comments that can occupy several lines. */
  • 31. 31 Documentation • A well-documented program is easier to understand and modify • You use comments to document programs • Comments should appear in a program to: – Explain the purpose of the program – Identify who wrote it – Explain the purpose of particular statements
  • 32. 32 Preprocessor Directives • C++ has a small number of operations • Many functions and symbols needed to run a C++ program are provided as collection of libraries • Every library has a name and is referred to by a header file • Preprocessor directives are commands supplied to the preprocessor • All preprocessor commands begin with # • No semicolon at the end of these commands
  • 33. 33 Preprocessor Directives (continued) • Syntax to include a header file: • For example: #include <iostream> The #include is a preprocessor directive used to include files in our program. This allows us to use cout in our program to print output on the screen and cin to take input from user.
  • 34. 34 namespace and Using cin and cout in a Program • cin and cout are declared in the header file iostream, but within std namespace • To use cin and cout in a program, use the following two statements: #include <iostream> using namespace std; • using namespace std means that we can use names for objects and variables from the standard library.
  • 35. 35 Creating a C++ Program • C++ program has two parts: – Preprocessor directives – The program • Preprocessor directives and program statements constitute C++ source code (.cpp) • Executable code is produced and saved in a file with the file extension .exe
  • 36. 36 Creating a C++ Program (continued) • A C++ program is a collection of functions, one of which is the function main • The first line of the function main is called the heading of the function: int main() • The statements enclosed between the curly braces ({ and }). • A valid C++ program must have the main() function. The curly braces indicate the start and the end of the function. • The execution of code beings from this function.
  • 37. 37 Creating a C++ Program Example: Body of the Function Program: #include <iostream> using namespace std; int main(){ cout<<“Hello World”; } Output: Hello World
  • 38. 38 Creating a C++ Program (continued)
  • 39. 39 Creating a C++ Program (continued) Sample Run: Line 9: firstNum = 18 Line 10: Enter an integer: 15 Line 13: secondNum = 15 Line 15: The new value of firstNum = 60
  • 40. 40 Program Style and Form • Every C++ program has a function main • It must also follow the syntax rules • Other rules serve the purpose of giving precise meaning to the language
  • 41. 41 Use of Semicolons, Brackets, and Commas • All C++ statements end with a semicolon – Also called a statement terminator • { and } are not C++ statements • Commas separate items in a list