SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
Programming Fundamentals
Lecture 4
Objectives
• To know about manipulators
• setw() and setfill() manipulator
• To know about Data type conversions
• To know about casts
• Cascading of << and >> operators
CS214 - PF 2
Course Instructor : Muhammad Haris
Mohsin
Review Problem 1
• What will be the output of following piece of code?
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
3
Done in Class, Have a
look at your lecture notes
Review Problem 2
• Write a code in C++ which will ask the user to enter
his/her age in days and then show the age in Years,
Months and Days.
• For simplicity you may suppose that there are 30 days
in each month
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
4
Manipulators
• Manipulators are instructions to the output
stream that modify the output in various ways
• The commonly used manipulators are endl,
setw() and setfill()
• endl
– endl manipulator terminates the line
• setw()
– setw() manipulator changes the field width of output
• setfill()
– setfill() manipulator fills the empty spaces of output
with a specific character
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
5
setw() Manipulator
• setw() manipulator accept an integer argument
and display the text in that specified length
• We can change the alignment of the text to left or
right
• The default alignment is right
• We need to add the header file named iomanip
to use setw() manipulator
• Have a look at the example for more details of
setw() manipulator
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
6
setfill() Manipulator
• setfill() manipulator accept a character as an
argument and fills the blank spaces with that
particular character
• For example the following line
cout<<setfill(‘.’)<<left<<setw(10)<<“Name”<<endl;
• Will print the following
Name……
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
7
Practice Problem
• Print the following message on screen
Name Age Savings
Ali 18 500
Usman 15 500
Sharjeel 22 1500
• Modify your code to fill the empty spaces with
dashes (-)
• Solution is on next slide
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
8
Practice Problem (Solution)
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
9
Type Conversions
• Unlike many other programming languages C++
allows the expressions containing mixed data
types
• When we add several data types in a single
expression, the C++ compiler converts the data
types to a common one, and this process is called
type conversion
• The type conversions may happen in two
different ways
– Automatic conversions
– Conversions through casts
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
10
Automatic Data Type Conversions
• When we mix two or more different data types in an
expression the data type with the lowest order is
converted into the data type of highest order (this is
called implicit conversion)
• Automatic conversions only work for built-in data types
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
11
Casts in C++
• Implicit conversions are automatically performed
when a value is copied to a compatible type
• If we want to copy the value to a non-compatible
type then we convert its type by using casts
• There are several casts in C++ like
– Static cast
– Dynamic cast
– Reinterpret cast
– Constant cast
• For the time being we will focus only on static
cast
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
12
Static Cast
• In C++ type conversion, the static_cast
operator performs an explicit type conversion
• All types of conversions that are well-defined and
allowed by the compiler are performed using
static_cast
• The static_cast operator can be used for
operations such as:
– Converting a pointer of a base class to a pointer of a
nonvirtual derived class
– Converting numeric data types such as int to float or
float to double
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
13
Syntax of Static_Cast
• The syntax of using static_cast is
static_cast<new_type>(expression)
• For example the following expression will convert the
int x to a character, and assign the converted value to a
character
int x = 65; char a;
a = static_cast<char>(x);
• The traditional type-casting equivalents to above
expressions would be:
(new_type) expression; // a = (char)x;
new_type(expression); // a = char(x);
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
14
Benefits of Static_Cast
• There are several advantages of using
static_cast and its syntax
– We can convert between non-built-in data types
– The implicit conversions are hard to see and hard
to search, so it is too difficult to find the possible
errors due to type conversions
– By using new syntax we make sure that we are
deliberately changing the data type and it is not a
logical error
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
15
Practice Problem
• Write a code which should ask the user to
enter a character and display its equivalent
ASCII code
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
16
Cascading in cout and cin
• We have already seen that we can cascade the stream
operator << in any C++ program, like
int a = 10, b = 20, c = 30;
cout<<a<<‘t’<<b<<‘t’<<c<<endl;
• Similarly we can cascade the >> operator as well
• The cascaded >> operator works for mixed data types,
or we introduce dummy characters to such mixing
• The benefit of cascading >> operator is, there will be
no need to press the Enter key after typing every input
• Have a look at the example for more clear
understanding
CS214 - PF
Course Instructor : Muhammad Haris
Mohsin
17

Contenu connexe

Similaire à Lecture 4 spring2018

AVR_Course_Day3 c programming
AVR_Course_Day3 c programmingAVR_Course_Day3 c programming
AVR_Course_Day3 c programmingMohamed Ali
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxRonaldo Aditya
 
Modal Driven Architecture In Software Engineering By NADEEM AHMED
Modal Driven Architecture In Software Engineering By NADEEM AHMED Modal Driven Architecture In Software Engineering By NADEEM AHMED
Modal Driven Architecture In Software Engineering By NADEEM AHMED NA000000
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Mohamed El Desouki
 
Testing Model Transformations
Testing Model TransformationsTesting Model Transformations
Testing Model Transformationsmiso_uam
 
0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdfssusere19c741
 
Schema Evolution Patterns - Velocity SJ 2019
Schema Evolution Patterns - Velocity SJ 2019Schema Evolution Patterns - Velocity SJ 2019
Schema Evolution Patterns - Velocity SJ 2019Alex Rasmussen
 
chapter11 - Programming.pdf
chapter11 - Programming.pdfchapter11 - Programming.pdf
chapter11 - Programming.pdfsatonaka3
 
Being Smart about C/C++ Development on AIX and Linux
Being Smart about C/C++ Development on AIX and Linux Being Smart about C/C++ Development on AIX and Linux
Being Smart about C/C++ Development on AIX and Linux Strongback Consulting
 
Object oriented programming 9 expressions and type casting in c++
Object oriented programming 9 expressions and type casting in c++Object oriented programming 9 expressions and type casting in c++
Object oriented programming 9 expressions and type casting in c++Vaibhav Khanna
 
04 Functional Programming in Python
04 Functional Programming in Python04 Functional Programming in Python
04 Functional Programming in PythonEbad ullah Qureshi
 
Csc153 chapter 02
Csc153 chapter 02Csc153 chapter 02
Csc153 chapter 02PCC
 
Data Migration Done Right for Microsoft Dynamics 365/CRM
Data Migration Done Right for Microsoft Dynamics 365/CRMData Migration Done Right for Microsoft Dynamics 365/CRM
Data Migration Done Right for Microsoft Dynamics 365/CRMDaniel Cai
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorialSURBHI SAROHA
 
Monomi: Practical Analytical Query Processing over Encrypted Data
Monomi: Practical Analytical Query Processing over Encrypted DataMonomi: Practical Analytical Query Processing over Encrypted Data
Monomi: Practical Analytical Query Processing over Encrypted DataMostafa Arjmand
 

Similaire à Lecture 4 spring2018 (20)

AVR_Course_Day3 c programming
AVR_Course_Day3 c programmingAVR_Course_Day3 c programming
AVR_Course_Day3 c programming
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptx
 
Modal Driven Architecture In Software Engineering By NADEEM AHMED
Modal Driven Architecture In Software Engineering By NADEEM AHMED Modal Driven Architecture In Software Engineering By NADEEM AHMED
Modal Driven Architecture In Software Engineering By NADEEM AHMED
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++
 
Testing Model Transformations
Testing Model TransformationsTesting Model Transformations
Testing Model Transformations
 
0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf
 
Schema Evolution Patterns - Velocity SJ 2019
Schema Evolution Patterns - Velocity SJ 2019Schema Evolution Patterns - Velocity SJ 2019
Schema Evolution Patterns - Velocity SJ 2019
 
Basic Elements of C++
Basic Elements of C++Basic Elements of C++
Basic Elements of C++
 
chapter11 - Programming.pdf
chapter11 - Programming.pdfchapter11 - Programming.pdf
chapter11 - Programming.pdf
 
Being Smart about C/C++ Development on AIX and Linux
Being Smart about C/C++ Development on AIX and Linux Being Smart about C/C++ Development on AIX and Linux
Being Smart about C/C++ Development on AIX and Linux
 
Object oriented programming 9 expressions and type casting in c++
Object oriented programming 9 expressions and type casting in c++Object oriented programming 9 expressions and type casting in c++
Object oriented programming 9 expressions and type casting in c++
 
Mis4200notes8 2
Mis4200notes8 2Mis4200notes8 2
Mis4200notes8 2
 
04 Functional Programming in Python
04 Functional Programming in Python04 Functional Programming in Python
04 Functional Programming in Python
 
C_Programming_Notes_ICE
C_Programming_Notes_ICEC_Programming_Notes_ICE
C_Programming_Notes_ICE
 
Csc153 chapter 02
Csc153 chapter 02Csc153 chapter 02
Csc153 chapter 02
 
C++.ppt
C++.pptC++.ppt
C++.ppt
 
Data Migration Done Right for Microsoft Dynamics 365/CRM
Data Migration Done Right for Microsoft Dynamics 365/CRMData Migration Done Right for Microsoft Dynamics 365/CRM
Data Migration Done Right for Microsoft Dynamics 365/CRM
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
 
Monomi: Practical Analytical Query Processing over Encrypted Data
Monomi: Practical Analytical Query Processing over Encrypted DataMonomi: Practical Analytical Query Processing over Encrypted Data
Monomi: Practical Analytical Query Processing over Encrypted Data
 

Dernier

定制(SCU毕业证书)南十字星大学毕业证成绩单原版一比一
定制(SCU毕业证书)南十字星大学毕业证成绩单原版一比一定制(SCU毕业证书)南十字星大学毕业证成绩单原版一比一
定制(SCU毕业证书)南十字星大学毕业证成绩单原版一比一z xss
 
定制英国克兰菲尔德大学毕业证成绩单原版一比一
定制英国克兰菲尔德大学毕业证成绩单原版一比一定制英国克兰菲尔德大学毕业证成绩单原版一比一
定制英国克兰菲尔德大学毕业证成绩单原版一比一z zzz
 
The Next Things To Immediately Do About Mating Press
The Next Things To Immediately Do About Mating PressThe Next Things To Immediately Do About Mating Press
The Next Things To Immediately Do About Mating Pressmatingpress170
 
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一A SSS
 
Back on Track: Navigating the Return to Work after Parental Leave
Back on Track: Navigating the Return to Work after Parental LeaveBack on Track: Navigating the Return to Work after Parental Leave
Back on Track: Navigating the Return to Work after Parental LeaveMarharyta Nedzelska
 
Human Rights are notes and helping material
Human Rights are notes and helping materialHuman Rights are notes and helping material
Human Rights are notes and helping materialnadeemcollege26
 
LinkedIn Strategic Guidelines April 2024
LinkedIn Strategic Guidelines April 2024LinkedIn Strategic Guidelines April 2024
LinkedIn Strategic Guidelines April 2024Bruce Bennett
 
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量sehgh15heh
 
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCRdollysharma2066
 
Digital Marketing Training Institute in Mohali, India
Digital Marketing Training Institute in Mohali, IndiaDigital Marketing Training Institute in Mohali, India
Digital Marketing Training Institute in Mohali, IndiaDigital Discovery Institute
 
Crack JAG. Guidance program for entry to JAG Dept. & SSB interview
Crack JAG. Guidance program for entry to JAG Dept. & SSB interviewCrack JAG. Guidance program for entry to JAG Dept. & SSB interview
Crack JAG. Guidance program for entry to JAG Dept. & SSB interviewNilendra Kumar
 
美国SU学位证,雪城大学毕业证书1:1制作
美国SU学位证,雪城大学毕业证书1:1制作美国SU学位证,雪城大学毕业证书1:1制作
美国SU学位证,雪城大学毕业证书1:1制作ss846v0c
 
办理老道明大学毕业证成绩单|购买美国ODU文凭证书
办理老道明大学毕业证成绩单|购买美国ODU文凭证书办理老道明大学毕业证成绩单|购买美国ODU文凭证书
办理老道明大学毕业证成绩单|购买美国ODU文凭证书saphesg8
 
Introduction to phyton , important topic
Introduction to phyton , important topicIntroduction to phyton , important topic
Introduction to phyton , important topicakpgenious67
 
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证nhjeo1gg
 
AICTE PPT slide of Engineering college kr pete
AICTE PPT slide of Engineering college kr peteAICTE PPT slide of Engineering college kr pete
AICTE PPT slide of Engineering college kr peteshivubhavv
 
Storytelling, Ethics and Workflow in Documentary Photography
Storytelling, Ethics and Workflow in Documentary PhotographyStorytelling, Ethics and Workflow in Documentary Photography
Storytelling, Ethics and Workflow in Documentary PhotographyOrtega Alikwe
 
Escorts Service Near Surya International Hotel, New Delhi |9873777170| Find H...
Escorts Service Near Surya International Hotel, New Delhi |9873777170| Find H...Escorts Service Near Surya International Hotel, New Delhi |9873777170| Find H...
Escorts Service Near Surya International Hotel, New Delhi |9873777170| Find H...nitagrag2
 
原版定制卡尔加里大学毕业证(UC毕业证)留信学历认证
原版定制卡尔加里大学毕业证(UC毕业证)留信学历认证原版定制卡尔加里大学毕业证(UC毕业证)留信学历认证
原版定制卡尔加里大学毕业证(UC毕业证)留信学历认证diploma001
 

Dernier (20)

定制(SCU毕业证书)南十字星大学毕业证成绩单原版一比一
定制(SCU毕业证书)南十字星大学毕业证成绩单原版一比一定制(SCU毕业证书)南十字星大学毕业证成绩单原版一比一
定制(SCU毕业证书)南十字星大学毕业证成绩单原版一比一
 
定制英国克兰菲尔德大学毕业证成绩单原版一比一
定制英国克兰菲尔德大学毕业证成绩单原版一比一定制英国克兰菲尔德大学毕业证成绩单原版一比一
定制英国克兰菲尔德大学毕业证成绩单原版一比一
 
The Next Things To Immediately Do About Mating Press
The Next Things To Immediately Do About Mating PressThe Next Things To Immediately Do About Mating Press
The Next Things To Immediately Do About Mating Press
 
Students with Oppositional Defiant Disorder
Students with Oppositional Defiant DisorderStudents with Oppositional Defiant Disorder
Students with Oppositional Defiant Disorder
 
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
 
Back on Track: Navigating the Return to Work after Parental Leave
Back on Track: Navigating the Return to Work after Parental LeaveBack on Track: Navigating the Return to Work after Parental Leave
Back on Track: Navigating the Return to Work after Parental Leave
 
Human Rights are notes and helping material
Human Rights are notes and helping materialHuman Rights are notes and helping material
Human Rights are notes and helping material
 
LinkedIn Strategic Guidelines April 2024
LinkedIn Strategic Guidelines April 2024LinkedIn Strategic Guidelines April 2024
LinkedIn Strategic Guidelines April 2024
 
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
 
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
 
Digital Marketing Training Institute in Mohali, India
Digital Marketing Training Institute in Mohali, IndiaDigital Marketing Training Institute in Mohali, India
Digital Marketing Training Institute in Mohali, India
 
Crack JAG. Guidance program for entry to JAG Dept. & SSB interview
Crack JAG. Guidance program for entry to JAG Dept. & SSB interviewCrack JAG. Guidance program for entry to JAG Dept. & SSB interview
Crack JAG. Guidance program for entry to JAG Dept. & SSB interview
 
美国SU学位证,雪城大学毕业证书1:1制作
美国SU学位证,雪城大学毕业证书1:1制作美国SU学位证,雪城大学毕业证书1:1制作
美国SU学位证,雪城大学毕业证书1:1制作
 
办理老道明大学毕业证成绩单|购买美国ODU文凭证书
办理老道明大学毕业证成绩单|购买美国ODU文凭证书办理老道明大学毕业证成绩单|购买美国ODU文凭证书
办理老道明大学毕业证成绩单|购买美国ODU文凭证书
 
Introduction to phyton , important topic
Introduction to phyton , important topicIntroduction to phyton , important topic
Introduction to phyton , important topic
 
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证
原版快速办理MQU毕业证麦考瑞大学毕业证成绩单留信学历认证
 
AICTE PPT slide of Engineering college kr pete
AICTE PPT slide of Engineering college kr peteAICTE PPT slide of Engineering college kr pete
AICTE PPT slide of Engineering college kr pete
 
Storytelling, Ethics and Workflow in Documentary Photography
Storytelling, Ethics and Workflow in Documentary PhotographyStorytelling, Ethics and Workflow in Documentary Photography
Storytelling, Ethics and Workflow in Documentary Photography
 
Escorts Service Near Surya International Hotel, New Delhi |9873777170| Find H...
Escorts Service Near Surya International Hotel, New Delhi |9873777170| Find H...Escorts Service Near Surya International Hotel, New Delhi |9873777170| Find H...
Escorts Service Near Surya International Hotel, New Delhi |9873777170| Find H...
 
原版定制卡尔加里大学毕业证(UC毕业证)留信学历认证
原版定制卡尔加里大学毕业证(UC毕业证)留信学历认证原版定制卡尔加里大学毕业证(UC毕业证)留信学历认证
原版定制卡尔加里大学毕业证(UC毕业证)留信学历认证
 

Lecture 4 spring2018

  • 2. Objectives • To know about manipulators • setw() and setfill() manipulator • To know about Data type conversions • To know about casts • Cascading of << and >> operators CS214 - PF 2 Course Instructor : Muhammad Haris Mohsin
  • 3. Review Problem 1 • What will be the output of following piece of code? CS214 - PF Course Instructor : Muhammad Haris Mohsin 3 Done in Class, Have a look at your lecture notes
  • 4. Review Problem 2 • Write a code in C++ which will ask the user to enter his/her age in days and then show the age in Years, Months and Days. • For simplicity you may suppose that there are 30 days in each month CS214 - PF Course Instructor : Muhammad Haris Mohsin 4
  • 5. Manipulators • Manipulators are instructions to the output stream that modify the output in various ways • The commonly used manipulators are endl, setw() and setfill() • endl – endl manipulator terminates the line • setw() – setw() manipulator changes the field width of output • setfill() – setfill() manipulator fills the empty spaces of output with a specific character CS214 - PF Course Instructor : Muhammad Haris Mohsin 5
  • 6. setw() Manipulator • setw() manipulator accept an integer argument and display the text in that specified length • We can change the alignment of the text to left or right • The default alignment is right • We need to add the header file named iomanip to use setw() manipulator • Have a look at the example for more details of setw() manipulator CS214 - PF Course Instructor : Muhammad Haris Mohsin 6
  • 7. setfill() Manipulator • setfill() manipulator accept a character as an argument and fills the blank spaces with that particular character • For example the following line cout<<setfill(‘.’)<<left<<setw(10)<<“Name”<<endl; • Will print the following Name…… CS214 - PF Course Instructor : Muhammad Haris Mohsin 7
  • 8. Practice Problem • Print the following message on screen Name Age Savings Ali 18 500 Usman 15 500 Sharjeel 22 1500 • Modify your code to fill the empty spaces with dashes (-) • Solution is on next slide CS214 - PF Course Instructor : Muhammad Haris Mohsin 8
  • 9. Practice Problem (Solution) CS214 - PF Course Instructor : Muhammad Haris Mohsin 9
  • 10. Type Conversions • Unlike many other programming languages C++ allows the expressions containing mixed data types • When we add several data types in a single expression, the C++ compiler converts the data types to a common one, and this process is called type conversion • The type conversions may happen in two different ways – Automatic conversions – Conversions through casts CS214 - PF Course Instructor : Muhammad Haris Mohsin 10
  • 11. Automatic Data Type Conversions • When we mix two or more different data types in an expression the data type with the lowest order is converted into the data type of highest order (this is called implicit conversion) • Automatic conversions only work for built-in data types CS214 - PF Course Instructor : Muhammad Haris Mohsin 11
  • 12. Casts in C++ • Implicit conversions are automatically performed when a value is copied to a compatible type • If we want to copy the value to a non-compatible type then we convert its type by using casts • There are several casts in C++ like – Static cast – Dynamic cast – Reinterpret cast – Constant cast • For the time being we will focus only on static cast CS214 - PF Course Instructor : Muhammad Haris Mohsin 12
  • 13. Static Cast • In C++ type conversion, the static_cast operator performs an explicit type conversion • All types of conversions that are well-defined and allowed by the compiler are performed using static_cast • The static_cast operator can be used for operations such as: – Converting a pointer of a base class to a pointer of a nonvirtual derived class – Converting numeric data types such as int to float or float to double CS214 - PF Course Instructor : Muhammad Haris Mohsin 13
  • 14. Syntax of Static_Cast • The syntax of using static_cast is static_cast<new_type>(expression) • For example the following expression will convert the int x to a character, and assign the converted value to a character int x = 65; char a; a = static_cast<char>(x); • The traditional type-casting equivalents to above expressions would be: (new_type) expression; // a = (char)x; new_type(expression); // a = char(x); CS214 - PF Course Instructor : Muhammad Haris Mohsin 14
  • 15. Benefits of Static_Cast • There are several advantages of using static_cast and its syntax – We can convert between non-built-in data types – The implicit conversions are hard to see and hard to search, so it is too difficult to find the possible errors due to type conversions – By using new syntax we make sure that we are deliberately changing the data type and it is not a logical error CS214 - PF Course Instructor : Muhammad Haris Mohsin 15
  • 16. Practice Problem • Write a code which should ask the user to enter a character and display its equivalent ASCII code CS214 - PF Course Instructor : Muhammad Haris Mohsin 16
  • 17. Cascading in cout and cin • We have already seen that we can cascade the stream operator << in any C++ program, like int a = 10, b = 20, c = 30; cout<<a<<‘t’<<b<<‘t’<<c<<endl; • Similarly we can cascade the >> operator as well • The cascaded >> operator works for mixed data types, or we introduce dummy characters to such mixing • The benefit of cascading >> operator is, there will be no need to press the Enter key after typing every input • Have a look at the example for more clear understanding CS214 - PF Course Instructor : Muhammad Haris Mohsin 17