SlideShare a Scribd company logo
1 of 17
Download to read offline
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

More Related Content

Similar to 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
 

Similar to 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
 

Recently uploaded

内布拉斯加大学林肯分校毕业证录取书( 退学 )学位证书硕士
内布拉斯加大学林肯分校毕业证录取书( 退学 )学位证书硕士内布拉斯加大学林肯分校毕业证录取书( 退学 )学位证书硕士
内布拉斯加大学林肯分校毕业证录取书( 退学 )学位证书硕士obuhobo
 
VIP Call Girls Service Film Nagar Hyderabad Call +91-8250192130
VIP Call Girls Service Film Nagar Hyderabad Call +91-8250192130VIP Call Girls Service Film Nagar Hyderabad Call +91-8250192130
VIP Call Girls Service Film Nagar Hyderabad Call +91-8250192130Suhani Kapoor
 
Booking open Available Pune Call Girls Ambegaon Khurd 6297143586 Call Hot In...
Booking open Available Pune Call Girls Ambegaon Khurd  6297143586 Call Hot In...Booking open Available Pune Call Girls Ambegaon Khurd  6297143586 Call Hot In...
Booking open Available Pune Call Girls Ambegaon Khurd 6297143586 Call Hot In...Call Girls in Nagpur High Profile
 
Top Rated Pune Call Girls Deccan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Deccan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Deccan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Deccan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Call Girls in Nagpur High Profile
 
Vip Modals Call Girls (Delhi) Rohini 9711199171✔️ Full night Service for one...
Vip  Modals Call Girls (Delhi) Rohini 9711199171✔️ Full night Service for one...Vip  Modals Call Girls (Delhi) Rohini 9711199171✔️ Full night Service for one...
Vip Modals Call Girls (Delhi) Rohini 9711199171✔️ Full night Service for one...shivangimorya083
 
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdfreStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdfKen Fuller
 
CALL ON ➥8923113531 🔝Call Girls Husainganj Lucknow best Female service 🧳
CALL ON ➥8923113531 🔝Call Girls Husainganj Lucknow best Female service  🧳CALL ON ➥8923113531 🔝Call Girls Husainganj Lucknow best Female service  🧳
CALL ON ➥8923113531 🔝Call Girls Husainganj Lucknow best Female service 🧳anilsa9823
 
Delhi Call Girls Munirka 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Munirka 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Munirka 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Munirka 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
CFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector ExperienceCFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector ExperienceSanjay Bokadia
 
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
OSU毕业证留学文凭,制做办理
OSU毕业证留学文凭,制做办理OSU毕业证留学文凭,制做办理
OSU毕业证留学文凭,制做办理cowagem
 
Top Rated Pune Call Girls Warje ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated  Pune Call Girls Warje ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Top Rated  Pune Call Girls Warje ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated Pune Call Girls Warje ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Call Girls in Nagpur High Profile
 
Resumes, Cover Letters, and Applying Online
Resumes, Cover Letters, and Applying OnlineResumes, Cover Letters, and Applying Online
Resumes, Cover Letters, and Applying OnlineBruce Bennett
 
Experience Certificate - Marketing Analyst-Soham Mondal.pdf
Experience Certificate - Marketing Analyst-Soham Mondal.pdfExperience Certificate - Marketing Analyst-Soham Mondal.pdf
Experience Certificate - Marketing Analyst-Soham Mondal.pdfSoham Mondal
 
Personal Brand Exploration - Fernando Negron
Personal Brand Exploration - Fernando NegronPersonal Brand Exploration - Fernando Negron
Personal Brand Exploration - Fernando Negronnegronf24
 
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...robinsonayot
 
Delhi Call Girls Greater Noida 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Greater Noida 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Greater Noida 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Greater Noida 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...poojakaurpk09
 
CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual serviceanilsa9823
 

Recently uploaded (20)

内布拉斯加大学林肯分校毕业证录取书( 退学 )学位证书硕士
内布拉斯加大学林肯分校毕业证录取书( 退学 )学位证书硕士内布拉斯加大学林肯分校毕业证录取书( 退学 )学位证书硕士
内布拉斯加大学林肯分校毕业证录取书( 退学 )学位证书硕士
 
VIP Call Girls Service Film Nagar Hyderabad Call +91-8250192130
VIP Call Girls Service Film Nagar Hyderabad Call +91-8250192130VIP Call Girls Service Film Nagar Hyderabad Call +91-8250192130
VIP Call Girls Service Film Nagar Hyderabad Call +91-8250192130
 
Booking open Available Pune Call Girls Ambegaon Khurd 6297143586 Call Hot In...
Booking open Available Pune Call Girls Ambegaon Khurd  6297143586 Call Hot In...Booking open Available Pune Call Girls Ambegaon Khurd  6297143586 Call Hot In...
Booking open Available Pune Call Girls Ambegaon Khurd 6297143586 Call Hot In...
 
Top Rated Pune Call Girls Deccan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Deccan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Deccan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Deccan ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
 
Vip Modals Call Girls (Delhi) Rohini 9711199171✔️ Full night Service for one...
Vip  Modals Call Girls (Delhi) Rohini 9711199171✔️ Full night Service for one...Vip  Modals Call Girls (Delhi) Rohini 9711199171✔️ Full night Service for one...
Vip Modals Call Girls (Delhi) Rohini 9711199171✔️ Full night Service for one...
 
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdfreStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
 
CALL ON ➥8923113531 🔝Call Girls Husainganj Lucknow best Female service 🧳
CALL ON ➥8923113531 🔝Call Girls Husainganj Lucknow best Female service  🧳CALL ON ➥8923113531 🔝Call Girls Husainganj Lucknow best Female service  🧳
CALL ON ➥8923113531 🔝Call Girls Husainganj Lucknow best Female service 🧳
 
Delhi Call Girls Munirka 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Munirka 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Munirka 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Munirka 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
CFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector ExperienceCFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector Experience
 
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Delhi 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
OSU毕业证留学文凭,制做办理
OSU毕业证留学文凭,制做办理OSU毕业证留学文凭,制做办理
OSU毕业证留学文凭,制做办理
 
Top Rated Pune Call Girls Warje ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated  Pune Call Girls Warje ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Top Rated  Pune Call Girls Warje ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated Pune Call Girls Warje ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
 
Resumes, Cover Letters, and Applying Online
Resumes, Cover Letters, and Applying OnlineResumes, Cover Letters, and Applying Online
Resumes, Cover Letters, and Applying Online
 
Experience Certificate - Marketing Analyst-Soham Mondal.pdf
Experience Certificate - Marketing Analyst-Soham Mondal.pdfExperience Certificate - Marketing Analyst-Soham Mondal.pdf
Experience Certificate - Marketing Analyst-Soham Mondal.pdf
 
VVVIP Call Girls In East Of Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In East Of Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...VVVIP Call Girls In East Of Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In East Of Kailash ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
 
Personal Brand Exploration - Fernando Negron
Personal Brand Exploration - Fernando NegronPersonal Brand Exploration - Fernando Negron
Personal Brand Exploration - Fernando Negron
 
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
 
Delhi Call Girls Greater Noida 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Greater Noida 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Greater Noida 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Greater Noida 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
Virgin Call Girls Delhi Service-oriented sexy call girls ☞ 9899900591 ☜ Rita ...
 
CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
 

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