Data Loading...
COMPUTER CLASS 11 COMPUTER SCIENCE VOLUME 2 EM FROM 2019 Flipbook PDF
CLASS 11 COMPUTER VOLUME 2 FROM 2019 DOWNLOADED FROM KALVISOLAI
www.kalvisolai.com
180 Views
80 Downloads
FLIP PDF 10.35MB
www.kalvisolai.com
www.books.kalvisolai.com
GOVERNMENT OF TAMILNADU
HIGHER SECONDARY FIRST YEAR
COMPUTER SCIENCE VOLUME-II
A publication under Free Textbook Programme of Government of Tamil Nadu
Department of School Education Untouchability is Inhuman and a Crime www.tamilgk.kalvisolai.com Index.indd 1
www.studymaterial.kalvisolai.com 14-08-2018 17:10:10
www.kalvisolai.com
www.books.kalvisolai.com
Government of Tamil Nadu First Edition
-
2018
NOT FOR SALE
Content Creation
The wise possess all
State Council of Educational Research and Training © SCERT 2018
Printing & Publishing
Tamil NaduTextbook and Educational Services Corporation www.textbooksonline.tn.nic.in
II www.tamilgk.kalvisolai.com Index.indd 2
www.studymaterial.kalvisolai.com 14-08-2018 17:10:10
www.kalvisolai.com
PREFACE
www.books.kalvisolai.com
Human civilization achieved the highest peak with the development of computer known as “Computer era”. Literate are those who have the knowledge in using the computer whereas others are considered illiterate inspite of the other degrees obtained.
T h e growth of the nation at present lies in the hands of the youth, hence the content of this book is prepared in such a way so as to attain utmost knowledge considering the future needs of the youth.
This book does not require prior knowledge in computer Technology Each unit comprises of simple activities and demonstrations which can be done by the teacher and also students.
HOW TO USE THE BOOK
Technical terminologies are listed in glossary for easy understanding The “ Do you know?” boxes enrich the knowledge of reader with additional information Workshops are introduced to solve the exercises using software applications QR codes are used to link supporting additional materials in digital form
How to get connected to QR Code? o
Download the QR code scanner from the google play store/ apple app store into your smartphone
o
Open the QR code scanner application
o
Once the scanner button in the application is clicked, camera opens and then bring it closer to the QR code in the textbook.
o
Once the camera detects the QR code, a URL appears in the screen. Click the URL and go to the content page.
III www.tamilgk.kalvisolai.com Index.indd 3
www.studymaterial.kalvisolai.com 14-08-2018 17:10:10
www.kalvisolai.com
www.books.kalvisolai.com
CAREER GUIDANCE AFTER 12TH COURSES B.E / B.Tech
COLLEGES/ UNIVERSITIES
PROFESSION
Software Engineer, Hardware All University and their affiliated Engineer, Software Colleges and Self financing Colleges in Development, Healthcare India and Abroad. Section, IT & ITEs
Science and Humanities B.Sc (Computer Science) BCA B.Sc ( Maths, Physics, Chemistry, Bio-Chemistry, Geography, journalism, Library Sciences, Political Science, Travel and Tourism)
All University and their affiliated Government Job and Private Colleges and Self financing Colleges in Company BPO, Geologist, India and Abroad. Journalist
LAW LLB B.A+LLB B.Com BBM+LLB BBA+LLB CA Diploma
All University and their affiliated Lawyer, Legal Officer, Govt Colleges and Self financing Colleges in Job India and Abroad. The Institute of Chartered Accountant of India (ICAI) Government Polytechnic and Selffinancing colleges
CA Private and Govt. Junior Engineer (Government and Private)
Commerce Courses B.com-Regular, B.com-Taxation & Tax Procedure, B.com-Travel &Tourism, B.com-Bank Management, B.com-Professional, BBA/BBM-Regular, BFM- Bachelors in Financial All University and their affiliated Markets, BMS-Bachelors in Management Colleges and Self financing Colleges in India and Abroad. Studies, BAF- Bachelors in Accounting & Finance, Certified Stock Broker & Investment Analysis, Certified Financial Analyst, Certified Financial Planner, Certified Investment Banker
Private Organization , Government ,Banking sectors and prospects for self – employment.
IV www.tamilgk.kalvisolai.com Index.indd 4
www.studymaterial.kalvisolai.com 14-08-2018 17:10:10
www.kalvisolai.com
COURSES
www.books.kalvisolai.com
COLLEGES/ UNIVERSITIES
PROFESSION
Management Courses Business Management Bank Management Event Management Hospital Management Human Resource Management Logistics Management
Private Organization , All University and their affiliated Government ,Banking sectors Colleges and Self financing Colleges in and prospects for self – India and Abroad. employment.
Science and Humanities B.Sc.Botany B.Sc.Zoology B.Sc.Dietician & Nutritionist B.Sc.Home Science B.Sc.Food Technology B.Sc.Dairy Technology B.Sc. Hotel Management B.Sc. Fashion Design B.Sc. Mass Communication B.Sc. Multimedia B.Sc. -3D Animation
All University and their affiliated Government Job and Private Colleges and Self financing Colleges in Company BPO, Geologist, India and Abroad Journalist
V www.tamilgk.kalvisolai.com Index.indd 5
www.studymaterial.kalvisolai.com 14-08-2018 17:10:10
www.kalvisolai.com
www.books.kalvisolai.com
Table of Contents Chapter No.
Title
Page
UNIT III – INTRODUCTION TO C++
9
Introduction to C++
10
Flow of Control
11
Functions
101
12
Arrays and Structures
137
1 60
UNIT IV - OBJECT ORIENTED PROGRAMMING WITH C++
13
Introducton to Object Oriented Programming Techniques
194
14
Classes and objects
200
15
Polymorphism
256
16
Inheritance
280
UNIT V - COMPUTER ETHICS AND CYBER SECURITY
17
Computer Ethics And Cyber Security
327
18
Tamil Computing
342
E - book
Assessment
DIGI links
VI www.tamilgk.kalvisolai.com Index.indd 6
www.studymaterial.kalvisolai.com 14-08-2018 17:10:10
www.kalvisolai.com
www.books.kalvisolai.com
Unit III Introduction to C++
CHAPTER
9
Introduction to C++ Learning Objectives After the completion of this chapter, the student will be able to • Understand the basic building blocks of C++ programming language • Able to construct simple C++ programs • Execute and debug C++programs
9.1 Introduction C++ is one of the most popular programming language developed by Bjarne Stroustrup at AT & T Bell Lab during 1979. C++ supports both procedural and Object Oriented Programming paradigms. Thus, C++ is called as a hybrid language. C++ is a superset (extension) of its predecessor C language. Bjarne Stroustrup named his new language as “C with Classes”. The name C++ was coined by Rick Mascitti where ++ is the C language increment operator.
Bjarne Stroustrup
Inventor of C++ Programming Language Bjarne is a Danish Computer Scientist was born on 30th December 1950. He has a Master degree in Mathematics and Computer Science in 1975 from Aarhus University, Denmark and Ph.D in Computer Science in 1979 from the University of Cambridge, England.
History of C++ C++ was developed by Bjarne Stroustrup at AT & T Bell Laboratory during 1979. C++ is originally derived from C language and influenced by many languages like Simula, BCPL, Ada, ML, CLU and ALGOL 68. Till 1983, it was referred “New C” and “C with Classes”. In 1983, the name was changed as C++ by Rick Mascitti. 1 www.tamilgk.kalvisolai.com Chapter 9.indd 1
www.studymaterial.kalvisolai.com 14-08-2018 17:00:19
www.kalvisolai.com
www.books.kalvisolai.com
9.2 Character set
C++ is standardized by the International Organization for Standardization (ISO). The latest standard version published in December 2017 as ISO/IEC 14882:2017 which is informally known as C++17. The first standardized version was published in 1998 as ISO/IEC 14882:1998 which was then enhanced by the C++03 (ISO/IEC 14882:2003), C++11 (ISO/IEC 14882:2011) and C++14 (ISO/ IEC 14882:2014). The Next standard version will be C++20 in 2020.
Character set is a set of characters which are allowed to write a C++ program. A character represents any alphabet, number or any other symbol (special characters) mostly available in the keyboard. C++ accepts the following characters. Most of the Character set, Tokens and expressions are very common to C based programming languages like C, C++, Java, PHP etc.,
C# (C-Sharp), D, Java and newer versions of C languages has been influenced by C++.
Alphabets Numeric Special Characters
Benefits of learning C++ • C++ is a highly portable language and is often the language of choice for multidevice, multi-platform app development.
White space
• C++ is an object-oriented programming language and includes classes, inheritance, polymorphism, data abstraction and encapsulation.
Other characters
A …. Z, a ….. z 0 …. 9 + - * / ~ ! @ # $ % ^& [ ] ( ) { } = >< _ \ | ? . , : ‘ “ ; Blank space, Horizontal tab (→), Carriage return ( ), Newline, Form feed C++ can process any of the 256 ASCII characters as data.
9.3 Lexical Units (Tokens):
• C++ has a rich function library.
C++ program statements are constructed by many different small elements such as commands, variables, constants and many more symbols called as operators and punctuators. These individual elements are collectively called as Lexical units or Lexical elements or Tokens. C++ has the following tokens:
• C++ allows exception handling, inheritance and function overloading which are not possible in C. • C++ is a powerful, efficient and fast language. It finds a wide range of applications – from GUI applications to 3D graphics for games to real-time mathematical simulations.
• Keywords • Literals • Punctuators
• Identifiers • Operators
2 www.tamilgk.kalvisolai.com Chapter 9.indd 2
www.studymaterial.kalvisolai.com 14-08-2018 17:00:19
www.kalvisolai.com
www.books.kalvisolai.com
TOKEN: The smallest individual unit in a program is known as a Token or a Lexical unit
9.3.1 Keywords
Keywords are the reserved words which convey specific meaning to the C++ compiler. They are the essential elements to construct C++ programs. Most of the keywords are common to C, C++ and Java. C++ is a case sensitive programming language so, all the keywords must be in lowercase. Table 9.1 C++ Keywords asm char delete extern if operator return struct try void
auto class do float inline private short switch typedef volatile
break const double for int protected signed template union while
case continue else friend long public sizeof this unsigned
catch default enum goto new register static throw virtual
• With revisions and additions, the recent list of keywords also includes:
using, namespace, bal, static_cast, const_cast, dynamic_cast, true, false
• Identifiers containing a double underscore are reserved for use by C++ implementations and standard libraries and should be avoided by users.
9.3.2 Identifiers Identifiers are the user-defined names given to different parts of the C++ program viz. variables, functions, arrays, classes etc., These are the fundamental building blocks of a program. Every language has specific rules for naming the identifiers.
Rules for naming an identifier: • The first character of an identifier must be an alphabet or an underscore (_). 3 www.tamilgk.kalvisolai.com Chapter 9.indd 3
www.studymaterial.kalvisolai.com 14-08-2018 17:00:19
www.kalvisolai.com
www.books.kalvisolai.com
• Only alphabets, digits and underscore are permitted. Other special characters are not allowed as part of an identifier.
• You may use capital style notation, such as tamilMark ie. capitalizing the first letter of the second word.
• C++ is case sensitive as it treats upper and lower-case characters differently.
9.3.3 Literals (Constants) Literals are data items whose values do not change during the execution of a program. Therefore Literals are called as Constants. C++ has several kinds of literals:
• Reserved words or keywords cannot be used as an identifier name. As per ANSI standards, C++ places no limit on its length and therefore all the characters are significant. Identifiers Num NUM _add total_sales tamilMark num-add
this
2myfile
Valid / Invalid Valid Valid Valid Valid Valid Invalid
Invalid
Invalid
Literals (Constants) Numeric Constants
Reason for invalid
Integer constants
Real Constants Boolean Constants Contains special character (-) This is one of the keyword. Keyword cannot be used as identifier names. Name must start begins with an alphabet or an underscore
Character Constants String Literals
Figure 9.1 Types of Constants
Numeric Constants: As the name indicates, the numeric constants are numeric values, which are used as constants. Numeric constants are further classified as: 1. Integer Constants (or) Fixed point constants.
• You may use an underscore in variable names to separate different parts of the name (eg: total_sales is a valid identifier where as the variable called total sales is an invalid identifier).
2. Real constants (or) Floating point constants.
4 www.tamilgk.kalvisolai.com Chapter 9.indd 4
www.studymaterial.kalvisolai.com 14-08-2018 17:00:19
www.kalvisolai.com
www.books.kalvisolai.com
(1) Integer Constants (or) Fixed point constants
Valid Invalid 012 05,600(Commas is not allowed) 04.56 (Decimal point is not -027 allowed)** 0158 (8 is not a permissible digit +0231 in octal system)
Integers are whole numbers without any fractions. An integer constant must have at least one digit without a decimal point. It may be signed or unsigned. Signed integers are considered as negative, commas and blank spaces are not allowed as part of it. In C++, there are three types of integer constants: (i) Decimal (ii) Octal (iii) Hexadecimal
Notes ** When you use a fractional number that begins with 0, C++ has consider the number as an integer not an Octal.
(i) Decimal
(iii) Hexadecimal
Any sequence of one or more digits (0 …. 9)
Any sequence of one or more Hexadecimal values (0 …. 9, A …. F) that starts with 0x or 0Xis considered as an Hexadecimal constant.
Valid Invalid 725 7,500 (Comma is not allowed) -27 66 5(Blank space is not allowed) 4.56 9$ (Special Character not allowed)
Valid Invalid 0x123 0x1,A5 (Commas is not allowed) 0x.14E (Decimal point is not 0X568 allowed like this)
If you assign 4.56 as an integer decimal constant, the compiler will accept only the integer portion of 4.56 ie. 4. It will simply ignore .56.
The suffix L or l and U or u added with any constant forces that to be represented as a long or unsigned constant respectively.
Notes
(2) Real Constants (or) Floating point constants
If a Decimal constant declared with fractions, then the compiler will take only the integer part of the value and it will ignore its fractional part. This is called as “Implicit Conversion”. It will be discussed later.
A real or floating point constant is a numeric constant having a fractional component. These constants may be written in fractional form or in exponent form. Fractional form of a real constant is a signed or unsigned sequence of digits including a decimal point between the digits. It must have at least one digit before and after a decimal point. It may have prefix
(ii) Octal Any sequence of one or more octal values (0 …. 7) that begins with 0 is considered as an Octal constant. 5 www.tamilgk.kalvisolai.com Chapter 9.indd 5
www.studymaterial.kalvisolai.com 14-08-2018 17:00:19
www.kalvisolai.com
www.books.kalvisolai.com
with + or - sign. A real constant without any sign will be considered as positive.
Escape sequences characters
Exponent form of real constants consists of two parts: (1) Mantissa and (2) Exponent. The mantissa must be either an integer or a real constant. The mantissa followed by a letter E or e and the exponent. The exponent should also be an integer.
C++ allows certain non-printable characters represented as character constants. Non-printable characters are also called as non-graphical characters. Nonprintable characters are those characters that cannot be typed directly from a keyboard during the execution of a program in C++, for example: backspace, tabs etc. These nonprintable characters can be represented by using escape sequences. An escape sequence is represented by a backslash followed by one or two characters.
For example, 58000000.00 may be written as 0.58 × 108 or 0.58E8. Mantissa (Before E) 0.58
Exponent (After E) 8
5.864 E-1
5.864 × 10
5864 E-2
5864 × 10
0.5864 E-2
0.5864 × 102 58.64
-2
Non-graphic
Table 9.2 Escape Sequences
Example: 1
(or)
Escape sequence \a \b \f \n \r \t \v \\ \’ \” \? \On \xHn \0
58.64 58.64
Boolean Literals Boolean literals are used to represent one of the Boolean values(True or false). Internally true has value 1 and false has value 0. Character constant A character constant is any valid single character enclosed within single quotes. A character constant in C++ must contain one character and must be enclosed within a single quote.
Non-graphical character Audible or alert bell Backspace Form feed Newline or linefeed Carriage return Horizontal tab Vertical tab Backslash Single quote Double quote Question Mark Octal number Hexadecimal number Null
Even though an escape sequence contains two characters, they should be enclosed within single quotes because, C++ consider escape sequences as character constants and allocates one byte in ASCII representation.
Valid character constants : ‘A’, ‘2’, ‘$’ Invalid character constants : “A” The value of a single character constant has an equivalent ASCII value. For example, the value of ‘A’ is 65. 6 www.tamilgk.kalvisolai.com Chapter 9.indd 6
www.studymaterial.kalvisolai.com 14-08-2018 17:00:19
www.kalvisolai.com
www.books.kalvisolai.com
9.3.4 Operators
ASCII (American Standard Code for Information Interchange) was first developed and published in 1963 by the X3 committee, a part of the American Standards Association (ASA).
The symbols which are used to do some mathematical or logical operations are called as “Operators”. The data items or values that the operators act upon are called as “Operands”.
String Literals
Operator
Sequence of characters enclosed within double quotes are called as String literals. By default, string literals are automatically added with a special character ‘\0’ (Null) at the end. Therefore, the string “welcome” will actually be represented as “welcome\0” in memory and the size of this string is not 7 but 8 characters i.e., inclusive of the last character \0. Valid string Literals : “A”, “Welcome” “1234”
+
6
a
-
b
Operands
Invalid String Literals : ‘Welcome’, ‘1234’
Evaluate Yourself
5
In C++, The operators are classified on the basis of the number of operands.
?
1. What is meant by literals? How many types of integer literals available in C++?
(i) Unary Operators - Require only one operand (ii) Binary Operators - Require two operands
2. What kind of constants are following? i) 26 ii) 015 iii) 0xF iv) 014.9
(iii) Ternary Operators - Require three operands
3. What is character constant in C++?
C++ Operators are classified as:
4. How are non graphic characters represented in C++?
(1) Arithmetic Operators
5. Write the following real constants into exponent form: i) 32.179 ii) 8.124 iii) 0.00007
(2) Relational Operators (3) Logical Operators
6. Write the following real constants into fractional form: i) 0.23E4 ii) 0.517E-3 iii) 0.5E-5
(4) Bitwise Operators
7. What is the significance of null (\0) character in a string?
(6) Conditional Operator
(5) Assignment Operators
(7) Other Operators 7
www.tamilgk.kalvisolai.com Chapter 9.indd 7
www.studymaterial.kalvisolai.com 14-08-2018 17:00:19
www.kalvisolai.com
www.books.kalvisolai.com
(1) Arithmetic Operators
The ++ or -- operators can be placed either as prefix (before) or as postfix (after)
Arithmetic operators perform simple arithmetic operations like addition, subtraction, multiplication, division etc.,
to a variable. With the prefix version, C++ performs the increment / decrement before using the operand.
Operator Operation Example + Addition 10 + 5 = 15 Subtraction 10 – 5 = 5 * Multiplication 10 * 5 = 50 10 / 5 = 2 / Division (Quotient of the division) Modulus 10 % 3 = (To find the 1(Remainder % reminder of a of the division) division) • The
above
mentioned
For example: N1=10, N2=20; S = ++N1 + ++N2;
working process of the above statement. N1 = 10, N2 = 20; S = ++N1 + ++N2 N1 = 10 + 1
arithmetic
requires minimum of two operands.
Figure 9.2 Prefix Increment Working process
++ (Plus, Plus) Increment operator
In the above example, the value of num is first incremented by 1, then the incremented value is assigned to the respective operand.
-- (Minus, Minus) Decrement operator An increment or decrement operator
acts upon a single operand and returns a new value. Thus, these operators are unary
With the postfix version, C++ uses the value of the operand in evaluating the expression before incrementing / decrementing its present value.
operators. The increment operator adds 1 to its operand and the decrement operator subtracts 1 from its operand. For example,
For example: N1=10, N2=20;
• x++ is the same as x = x+1;
S = N1++ + ++N2;
It adds 1 to the present value of x
The following Figure explains the working process of the above statement.
• x-- is the same as to x = x–1;
N2 = 21
S= 32
Increment and Decrement Operators
N2 = 20 + 1
N1 = 11
operators are binary operators which
The following Figure explains the
It subtracts 1 from the present value of x 8 www.tamilgk.kalvisolai.com
Chapter 9.indd 8
www.studymaterial.kalvisolai.com 14-08-2018 17:00:20
www.kalvisolai.com
www.books.kalvisolai.com
N1 = 10, N2 = 20; S = N1++ + ++N2
N2 = 20 + 1
N1 = 10
N2 = 21
S= 31
Figure 9.3 Postfix Increment Working process In the above example, the value assigned to operand N1 is taken into consideration, first and then the value will be incremented by 1. Operator
Operation
++
Increment
--
Decrement
Example (Assume n=2; what will be value of a) Prefix Postfix a=++n; a=n++; Value of a = 3 Value of a = 2 a=--n; a=n--; Value of a=1 Value of a=2
(2) Relational Operators Relational operators are used to determine the relationship between its operands. When the relational operators are applied on two operands, the result will be a Boolean value i.e 1 or 0 to represents True or False respectively. C++ provides six relational operators. They are, Operator > < >= b a= b a > 2
0
0
0
0
1
1
0
0
(a>> 2) = 0000 00112= 310 (iii) The Bitwise one’s compliment operator: The bitwise One’s compliment operator ~(Tilde),inverts all the bits in a binary pattern, that is, all 1’s become 0 and all 0’s become 1. This is an unary operator. Example: If a =15; Equivalent binary values of a is 0000 1111 Operator
~
Operation
(~a)
Result a
0
0
0
0
1
1
1
1
(~a)
1
1
1
1
0
0
0
0
(~a) = 1111 00002= -1610 12 www.tamilgk.kalvisolai.com Chapter 9.indd 12
www.studymaterial.kalvisolai.com 14-08-2018 17:00:20
www.kalvisolai.com
www.books.kalvisolai.com
(5)Assignment Operator: Assignment operator is used to assign a value to a variable which is on the left hand side of an assignment statement. = (equal to) is commonly used as the assignment operator in all computer programming languages. This operator copies the value at the right side of the operator to the left side variable. It is also a binary operator.
A
=
32
C++ uses different types of assignment operators. They are called as Shorthand assignment operators. Operator +=
-=
*=
/=
%=
Name of Operator
Example
Addition Assignment
a = 10; c = a += 5; (ie, a = a + 5) c = 15
Subtraction Assignment
a = 10; c = a -= 5; (ie. a = a – 5) c=5
Multiplication Assignment
a = 10; c = a *= 5; (ie. a = a * 5) c = 50
Division Assignment
a = 10; c = a /= 5; (ie. a = a / 5) c=2
Modulus Assignment
a = 10; c = a %= 5; (ie. a = a % 5) c=0
Discuss the differences between = and == operators
(6) Conditional Operator: In C++, there is only one conditional operator is used. ?: is a conditional Operator. This is a Ternary Operator. This operator is used as analternate to if … else control statement. We will learn more about this operator in later chapters along with if …. else structure. 13 www.tamilgk.kalvisolai.com Chapter 9.indd 13
www.studymaterial.kalvisolai.com 14-08-2018 17:00:20
www.kalvisolai.com
www.books.kalvisolai.com
(7)Other Operators: The Comma operator
Comma ( , ) is an operator in C++ used to string together several expressions. The group of expression separated by comma is evaluated from left to right.
Sizeof
This is called as compile time operator. It returns the size of a variable in bytes.
Pointer
* &
Pointer to a variable Address of
Component selection
. ->
Direct component selector Indirect component selector
Class member operators
:: Scope access / resolution .* Dereference ->* Dereference pointer to class member
Precedence of Operators: Operators are executed in the order of precedence. The operands and the operators are grouped in a specific logical way for evaluation. This logical grouping is called as an Association. The order of precedence: ()[]
Operators within parenthesis are performed first
++, --
Postfix increment / decrement
++, --
Prefix increment / decrement
*, /, %
Multiplication, Division, Modulus
+, = ==, != &&
Addition, Subtraction Less than, Less than or equal to, Greater than, Greater than or equal to Equal to, Not equal to Logical AND
||
Logical OR
?:
Conditional Operator
=
Simple Assignment
+=, -=, *=, /=
Shorthand operators
,
Higher
Comma operator
Lower 14
www.tamilgk.kalvisolai.com Chapter 9.indd 14
www.studymaterial.kalvisolai.com 14-08-2018 17:00:20
www.kalvisolai.com
www.books.kalvisolai.com
In C++, one or two operators may be used in different places with different meaning. For example: Asterisk ( * ) is used for multiplication as well as for pointer to a variable.
9.3.5 Punctuators Punctuators are symbols, which are used as delimiters, while constructing a C++ program. They are also called as “Separators”. The following punctuators are used in C++; most of these symbols are very similar to C and Java. Separator
Description
Example
int main ( ) Opening and closing curly braces indicate the { start and the end of a block of code. A block Curly braces int x=10, y=20, sum; of code containing more than one executable { } sum = x + y; statement. These statements together are called as cout > to get input. It extracts the value through the keyboard and assigns it to the variable on its right; hence, it is called as “Stream extraction” or “get from” operator. It is a binary operator i.e., it requires two operands. The first operand is the pre-defined identifier cin (pronounced as C-In) that identifies keyboard as the input device. The second operand must be a variable.
cin
>>
Variable
Figure 9.4 Working process of cin 16 www.tamilgk.kalvisolai.com Chapter 9.indd 16
www.studymaterial.kalvisolai.com 14-08-2018 17:00:20
www.kalvisolai.com
www.books.kalvisolai.com
To receive or extract more than one value at a time, >> operator should be used for each variable. This is called cascading of operator. Example:
cin >> num;
Pre-defined object cin extracts a value typed on keyboard and stores it in variable num.
cin >>x >> y;
This is used to extract two values. cin reads thefirst value and immediately assigns that to variable x; next, it reads the second value which is typed after a space and assigns that to y. Space is used as a separator for each input.
9.4.2 Output Operator: C++ provides num2; sum = num1 + num2; cout