SlideShare a Scribd company logo
1 of 23
Download to read offline
Programming Fundamentals
Lecture 2
Objectives
• To review the basic syntax of a C++ program
• To know about data hierarchy
• To differentiate between an editor and
integrated development environment (IDE)
• To know about the rules of variable naming
• To know about Declaration, Initialization and
Definition of a Variable
• To learn the correct use of operators
CS214 - PF 2
Course Instructor : Muhammad Haris
Mohsin
Data Hierarchy
• As viewed from Computer to User
• Bits Characters Fields Records Files
CS214 - PF 3
Course Instructor : Muhammad Haris
Mohsin
Data Hierarchy
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
4
Data Hierarchy
• Bit
– The smallest data item in a computer
– Either 0 or 1
• Character
– Digits, letters and special symbols are known as characters
– The computer’s character set is the set of all the characters
used to write programs and represent data items
– C++ uses the ASCII character set
• Field
– A field is a group of characters or bytes that conveys
meaning
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
5
Data Hierarchy
• Record
– Several related fields can be used to compose a record
(implemented as a class in most of the languages)
– For example a payroll system may have following
fields
• Employee identification number (a whole number)
• Name (a combination of characters)
• Address (a combination of characters)
• Hourly pay rate (a number with a decimal point)
• Year-to-date earnings (a number with a decimal point)
• Amount of taxes withheld (a number with a decimal point)
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
6
Data Hierarchy
• File
– A file is a group of related records
– More generally, a file contains arbitrary data in
arbitrary formats
– A file is viewed simply as a sequence of bytes
– Any organization of the bytes in a file, such as
organizing the data into records, is a view created
by the application programmer
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
7
A Simple Program in C++
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
8
Editor and IDE
• Editor
– Editor is only capable of editing the text you write
– For compiling, debugging or execution you need extra
utilities
– Notpad, wordpad
• Integrated Development Environment (IDE)
– It is not just a tool where you write the code ,but you
can also compile, debug and execute the code
– Microsoft visual studio
– Dev C++
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
9
Header File
• Header files contain definitions of functions
and variables which is imported or used into
any C++ program by using the pre-processor
#include statement
• Header file have an extension ".h" which
contains C++ function declaration
• Each header file contains information (or
declarations) for a particular group of
functions
CS214 - PF 10
Course Instructor : Muhammad Haris
Mohsin
Header File
• One example of a header file is “iostream”
• The header file named “iostream” contains
the definitions of basic input and output
functions like cin and cout
• Similarly the header file named “math”
contains the definitions of functions related to
mathematics
CS214 - PF 11
Course Instructor : Muhammad Haris
Mohsin
Types of Header Files
• System Header Files
– These header files comes with compiler and
contains the definitions of functions previously
defined
• User Header Files
– These are defined by users, and contains the
definitions of functions defined by user
CS214 - PF 12
Course Instructor : Muhammad Haris
Mohsin
Why we Need Header Files
• When we want to use any function in our C++
program then first we need to import their
definition from C++ library
• For importing their declaration and definition
we need to include header file in program
• Header file are included at the top of any C++
program
CS214 - PF 13
Course Instructor : Muhammad Haris
Mohsin
How to Use a Header File
• The header files can be included by using one of the
following syntax
• The use of angle brackets <> informs the compiler to
search the compiler’s included directory for the
specified file
• The use of the double quotes "" around the filename
inform the compiler to search in the current directory
for the specified file
CS214 - PF 14
Course Instructor : Muhammad Haris
Mohsin
Commonly used C++ Commands
main( )
{
--------
}
• main() function is the entry point of any C++
program
• It is the point at which execution of program is
started
• When a C++ program is executed, the execution
control goes directly to the main() function
• You can use main as void main() or int main()
CS214 - PF 15
Course Instructor : Muhammad Haris
Mohsin
Commonly used C++ Commands
• #include
– Tells the compiler that I want to use specific header
file
• cin
– cin is used for get or read value form keyboard
• cout
– cout is used for print message on screen
• cout<<endl; and cout<<‘n’;
– Terminates the line, and start the message from new
line
CS214 - PF 16
Course Instructor : Muhammad Haris
Mohsin
Commonly used C++ Commands
• cout<<‘t’;
– Equivalent to a horizontal tab break
• system(“pause”);
– It runs the pause command (as though from a
command line) which waits until the user presses any
key
• return 0;
– A standard way to state that the main has successfully
executed if it returns zero
– Only used when we use “int main”
– No need to use return 0 with “void main”
CS214 - PF 17
Course Instructor : Muhammad Haris
Mohsin
Commonly used C++ Commands
• using namespace std;
– namespace std helps to use cout command more
easily
– Without namespace std
– With namespace std
CS214 - PF 18
Course Instructor : Muhammad Haris
Mohsin
Commonly used C++ Commands
• // and /* */
– // is used to add comments in front of a statement
– If you want to a section of code as a comment
then enclose the code between /* */
• Stream Insertion Operator “<<“ and “>>”
– Are used to input or display streams/messages
• << is used to display message
• >> is used to take input from keyboard
CS214 - PF 19
Course Instructor : Muhammad Haris
Mohsin
Variable Naming
• A variable name is called an “identifier” and is
only valid if
– It is not a Keyword of C++
– Is a series of letters, numbers and Underscores (_)
– Does not start with a Number
• An identifier is case sensitive which means
that A1 and a1 are two different identifiers
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
20
Some Logical Questions
1) Why we prefer ASCII codes for C++ instead of Unicode?
2) What is the advantage of adding a header file by using
double quotes? And when we prefer this method?
3) Is it possible that you used system(“pause”)
command in your code, but it is still not pausing? If yes,
then what is the reason?
4) Write at least one alternative command for
system(“pause”)
5) Where we can declare a variable in a C++ program? Is
there any restriction about the position of declaration?
6) According to variable naming rules, the variable name can
start with an underscore (_). But it is not recommended,
why?
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
21
Declaration, Initialization and
Definition of a Variable
int a;
int b = 10;
int c;
c = 8;
• When we declare a variable only a block of
memory is decided for that variable
• When we define or initialize a variable, then
memory is allocated
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
22
The Use of Operators
• While using the operators we write the
variable towards the left side of operator and
its definition goes towards right side of
operator
int a = 10, b = 20;
a = b;
• The above statement means that value of b is
assigned to a
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
23

More Related Content

Similar to Lecture 2 spring2018

Chapter 1: Introduction to Command Line
Chapter 1: Introduction  to Command LineChapter 1: Introduction  to Command Line
Chapter 1: Introduction to Command Lineazzamhadeel89
 
Object oriented programming 11 preprocessor directives and program structure
Object oriented programming 11 preprocessor directives and program structureObject oriented programming 11 preprocessor directives and program structure
Object oriented programming 11 preprocessor directives and program structureVaibhav Khanna
 
Basic C Structure and related terms with example
Basic C Structure and related terms with exampleBasic C Structure and related terms with example
Basic C Structure and related terms with examplesanjana mun
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.pptInfotech27
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.pptTeacherOnat
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.pptJayarAlejo
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.pptJayarAlejo
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.pptEPORI
 
C++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.pptC++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.pptyp02
 
C Programming Lecture 2 - Structure of a C Program.pptx
C Programming Lecture 2 - Structure of a C Program.pptxC Programming Lecture 2 - Structure of a C Program.pptx
C Programming Lecture 2 - Structure of a C Program.pptxMurali M
 
Basics of C Prog Lang.pdf
Basics of C Prog Lang.pdfBasics of C Prog Lang.pdf
Basics of C Prog Lang.pdfKalighatOkira
 

Similar to Lecture 2 spring2018 (20)

Chapter 1: Introduction to Command Line
Chapter 1: Introduction  to Command LineChapter 1: Introduction  to Command Line
Chapter 1: Introduction to Command Line
 
C_Programming_Notes_ICE
C_Programming_Notes_ICEC_Programming_Notes_ICE
C_Programming_Notes_ICE
 
Structure
StructureStructure
Structure
 
Object oriented programming 11 preprocessor directives and program structure
Object oriented programming 11 preprocessor directives and program structureObject oriented programming 11 preprocessor directives and program structure
Object oriented programming 11 preprocessor directives and program structure
 
Student record
Student recordStudent record
Student record
 
Basic C Structure and related terms with example
Basic C Structure and related terms with exampleBasic C Structure and related terms with example
Basic C Structure and related terms with example
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.pptC++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.ppt
 
Ums in c
Ums in cUms in c
Ums in c
 
Coding
CodingCoding
Coding
 
C Programming Lecture 2 - Structure of a C Program.pptx
C Programming Lecture 2 - Structure of a C Program.pptxC Programming Lecture 2 - Structure of a C Program.pptx
C Programming Lecture 2 - Structure of a C Program.pptx
 
Basics of C Prog Lang.pdf
Basics of C Prog Lang.pdfBasics of C Prog Lang.pdf
Basics of C Prog Lang.pdf
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
C language unit-1
C language unit-1C language unit-1
C language unit-1
 
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDYC LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
 

Recently uploaded

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Recently uploaded (20)

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 

Lecture 2 spring2018

  • 2. Objectives • To review the basic syntax of a C++ program • To know about data hierarchy • To differentiate between an editor and integrated development environment (IDE) • To know about the rules of variable naming • To know about Declaration, Initialization and Definition of a Variable • To learn the correct use of operators CS214 - PF 2 Course Instructor : Muhammad Haris Mohsin
  • 3. Data Hierarchy • As viewed from Computer to User • Bits Characters Fields Records Files CS214 - PF 3 Course Instructor : Muhammad Haris Mohsin
  • 4. Data Hierarchy CS214 - PF Course Instructor : Muhammad Haris Mohsin 4
  • 5. Data Hierarchy • Bit – The smallest data item in a computer – Either 0 or 1 • Character – Digits, letters and special symbols are known as characters – The computer’s character set is the set of all the characters used to write programs and represent data items – C++ uses the ASCII character set • Field – A field is a group of characters or bytes that conveys meaning CS214 - PF Course Instructor : Muhammad Haris Mohsin 5
  • 6. Data Hierarchy • Record – Several related fields can be used to compose a record (implemented as a class in most of the languages) – For example a payroll system may have following fields • Employee identification number (a whole number) • Name (a combination of characters) • Address (a combination of characters) • Hourly pay rate (a number with a decimal point) • Year-to-date earnings (a number with a decimal point) • Amount of taxes withheld (a number with a decimal point) CS214 - PF Course Instructor : Muhammad Haris Mohsin 6
  • 7. Data Hierarchy • File – A file is a group of related records – More generally, a file contains arbitrary data in arbitrary formats – A file is viewed simply as a sequence of bytes – Any organization of the bytes in a file, such as organizing the data into records, is a view created by the application programmer CS214 - PF Course Instructor : Muhammad Haris Mohsin 7
  • 8. A Simple Program in C++ CS214 - PF Course Instructor : Muhammad Haris Mohsin 8
  • 9. Editor and IDE • Editor – Editor is only capable of editing the text you write – For compiling, debugging or execution you need extra utilities – Notpad, wordpad • Integrated Development Environment (IDE) – It is not just a tool where you write the code ,but you can also compile, debug and execute the code – Microsoft visual studio – Dev C++ CS214 - PF Course Instructor : Muhammad Haris Mohsin 9
  • 10. Header File • Header files contain definitions of functions and variables which is imported or used into any C++ program by using the pre-processor #include statement • Header file have an extension ".h" which contains C++ function declaration • Each header file contains information (or declarations) for a particular group of functions CS214 - PF 10 Course Instructor : Muhammad Haris Mohsin
  • 11. Header File • One example of a header file is “iostream” • The header file named “iostream” contains the definitions of basic input and output functions like cin and cout • Similarly the header file named “math” contains the definitions of functions related to mathematics CS214 - PF 11 Course Instructor : Muhammad Haris Mohsin
  • 12. Types of Header Files • System Header Files – These header files comes with compiler and contains the definitions of functions previously defined • User Header Files – These are defined by users, and contains the definitions of functions defined by user CS214 - PF 12 Course Instructor : Muhammad Haris Mohsin
  • 13. Why we Need Header Files • When we want to use any function in our C++ program then first we need to import their definition from C++ library • For importing their declaration and definition we need to include header file in program • Header file are included at the top of any C++ program CS214 - PF 13 Course Instructor : Muhammad Haris Mohsin
  • 14. How to Use a Header File • The header files can be included by using one of the following syntax • The use of angle brackets <> informs the compiler to search the compiler’s included directory for the specified file • The use of the double quotes "" around the filename inform the compiler to search in the current directory for the specified file CS214 - PF 14 Course Instructor : Muhammad Haris Mohsin
  • 15. Commonly used C++ Commands main( ) { -------- } • main() function is the entry point of any C++ program • It is the point at which execution of program is started • When a C++ program is executed, the execution control goes directly to the main() function • You can use main as void main() or int main() CS214 - PF 15 Course Instructor : Muhammad Haris Mohsin
  • 16. Commonly used C++ Commands • #include – Tells the compiler that I want to use specific header file • cin – cin is used for get or read value form keyboard • cout – cout is used for print message on screen • cout<<endl; and cout<<‘n’; – Terminates the line, and start the message from new line CS214 - PF 16 Course Instructor : Muhammad Haris Mohsin
  • 17. Commonly used C++ Commands • cout<<‘t’; – Equivalent to a horizontal tab break • system(“pause”); – It runs the pause command (as though from a command line) which waits until the user presses any key • return 0; – A standard way to state that the main has successfully executed if it returns zero – Only used when we use “int main” – No need to use return 0 with “void main” CS214 - PF 17 Course Instructor : Muhammad Haris Mohsin
  • 18. Commonly used C++ Commands • using namespace std; – namespace std helps to use cout command more easily – Without namespace std – With namespace std CS214 - PF 18 Course Instructor : Muhammad Haris Mohsin
  • 19. Commonly used C++ Commands • // and /* */ – // is used to add comments in front of a statement – If you want to a section of code as a comment then enclose the code between /* */ • Stream Insertion Operator “<<“ and “>>” – Are used to input or display streams/messages • << is used to display message • >> is used to take input from keyboard CS214 - PF 19 Course Instructor : Muhammad Haris Mohsin
  • 20. Variable Naming • A variable name is called an “identifier” and is only valid if – It is not a Keyword of C++ – Is a series of letters, numbers and Underscores (_) – Does not start with a Number • An identifier is case sensitive which means that A1 and a1 are two different identifiers CS214 - PF Course Instructor : Muhammad Haris Mohsin 20
  • 21. Some Logical Questions 1) Why we prefer ASCII codes for C++ instead of Unicode? 2) What is the advantage of adding a header file by using double quotes? And when we prefer this method? 3) Is it possible that you used system(“pause”) command in your code, but it is still not pausing? If yes, then what is the reason? 4) Write at least one alternative command for system(“pause”) 5) Where we can declare a variable in a C++ program? Is there any restriction about the position of declaration? 6) According to variable naming rules, the variable name can start with an underscore (_). But it is not recommended, why? CS214 - PF Course Instructor : Muhammad Haris Mohsin 21
  • 22. Declaration, Initialization and Definition of a Variable int a; int b = 10; int c; c = 8; • When we declare a variable only a block of memory is decided for that variable • When we define or initialize a variable, then memory is allocated CS214 - PF Course Instructor : Muhammad Haris Mohsin 22
  • 23. The Use of Operators • While using the operators we write the variable towards the left side of operator and its definition goes towards right side of operator int a = 10, b = 20; a = b; • The above statement means that value of b is assigned to a CS214 - PF Course Instructor : Muhammad Haris Mohsin 23