LET’S LEARNINTRODUCTION
STANDARD OUTPUT STREAM: cout
STANDARD INPUT STREAM: cin
C++ MANIPULATORS
1. endl
2. setbase()
3. setw()
4. setfill()
5. setprecision()
CHARACTER ENCODING: ASCII AND UNICODE
ASCII
UNICODE
BACKSLASH CHARACTERS
CHAPTER EVALUATION EXERCISE
STANDARD OUTPUT STREAM: cout
STANDARD INPUT STREAM: cin
C++ MANIPULATORS
1. endl
2. setbase()
3. setw()
4. setfill()
5. setprecision()
CHARACTER ENCODING: ASCII AND UNICODE
ASCII
UNICODE
BACKSLASH CHARACTERS
CHAPTER EVALUATION EXERCISE
INTRODUCTION
STANDARD
OUTPUT STREAM: "cout"
cout << “Good Morning”; // prints Good Morning on screen
cout <<
120; // prints number 120 on screen
cout << x; // prints value of x on screen
cout<<
“x”; // prints character x on screen
cout <<
“Hello, I am “ << age << “ years old and my pincode is “ << pincode;
In the last statement, if age=24 and pincode=691301 then output is:
Hello, I am 24 years old and my pincode is 691301 |
First Line.
Second Line.
Third Line.
|
STANDARD
INPUT STREAM: "cin"
#include<iostream>
#define Pi 3.14 //symbolic constant
int main()
{
float
area, R;
std::cout<<"Enter
radius of circle : ";
std::cin>>R;
area=Pi*R*R;
std::cout<<"Area
of circle = "<<area<<" sq.units";
} |
Output
Enter radius of circle
: 5
Area of circle =
78.5 sq.units
|
C++ MANIPULATORS
#include<iostream>
#include<iomanip>
using
namespace std;
main()
{
int a;
// using setbase()
cout<< "Enter a Number:
"; cin>>a;
cout<< "OCTAL value of
"<<a<< " is
"<<setbase(8)<<a<<endl;
cout<< "HEXADECIMAL value of
"<<setbase(10)<<a<<" is
"<<setbase(16)<<a;
// using oct, hex, dec manipulator
functions
cout<< "\n\nEnter Another
Number: "; cin>>a;
cout<< "OCTAL value of
"<<dec<<a<< " is
"<<oct<<a<<endl; //why "dec" ?
cout<< "HEXADECIMAL value of
"<<dec<<a<<" is "<<hex<<a;
}
|
4. setfill() is an output manipulator that is used to specify a character which will fill the unused columns within a setw() specified width.
#include<iostream>
#include<iomanip>
using namespace std;
main()
{
int a = 28;
// set * as fill value for
width=5,10
cout
<<setfill('*')<<setw(5)<<a<<endl;
cout
<<setw(10)<<a<<endl;
// assign 6
digit float
float b = 9.59281;
// set 2 positions for
output
cout <<endl<<b<<"
with Precision 2 = ";
cout
<<setprecision(2)<<b<<endl;
// now, b =
9.6 (rounded value)
// bring back b to
original value, setprecision = 6 digits
// set FIXED precision to
b and print
cout <<setprecision(6)<<b<<"
with FIXED Precision 2 = ";
cout
<<fixed<<setprecision(2)<<b<<endl;
// now you
have set precision value for 2 fractional part
// so now, b = 9.59 (exact
value as declared)
}
|
#include <bits/stdc++.h> This is a
special header file that includes all header
files. You don’t need to remember and declare each header file for each
purpose. However, namespace if any has
to be declared. For example, from the above program, #include<iostream> #include<iomanip> using namespace std;
can all be replaced with, #include <bits/stdc++.h> using
namespace std; |
A character (char) occupies 1 Byte of memory space in ASCII format. The char can occupy more than 1 Byte of memory space in Unicode format. Unicode efficiently handles both the 'char' and 'string' data types.
ASCII character codes from 128 to 255 (total 128) are extended ASCII values for complex characters. E.g ‘€’ has ASCII code 128, ‘TM’ has ASCII code 153 etc.
Unicode has currently defined 143,924 character codes (143,696 graphic characters including emoji, 163 format characters and 65 control characters).
It is a superset of ASCII and so, the first 256 character codes of Unicode are same as that of ASCII.
Unicode contains character codes for most written languages including left-to-right scripts like English, right-to-left scripts like Arabic, Chinese, Japanese etc.
Escape
Sequence
|
\' |
\" |
\\ |
\a |
\b |
\t |
\v |
Description
|
single quote
|
double quote
|
backslash
|
audible
bell
|
backspace
|
horizontal
tab
|
vertical
tab
|
#include<iostream>
using namespace std;
main ()
{
char ch;
cout << "Enter a valid
keyboard character: ";
cin >> ch;
cout <<
"\"ASCII\\UNICODE\" value of " << ch <<"
is: " << (int)ch;
// Here, \" is escape sequence to
print double quote…see output
// Also, \\ is escape sequence to print
backslash…see output
}
|
Enter a
valid keyboard character: A
"ASCII\UNICODE"
value of A is: 65
CHAPTER EVALUATION EXERCISE
A) Choose the most appropriate answer (1 mark)
No comments:
Post a Comment