kvsangathan.nic.in Class-XII Computer Science Study Material : Kendriya Vidyalaya Sangathan
Organisation : Kendriya Vidyalaya Sangathan
Announcement : Class-XII Computer Science Study Material
Download Study Material : http://www.syllabus.gen.in/uploads/54-study_material_XII_comp(1).pdf
Home Page : http://kvsangathan.nic.in/
STUDY MATERIAL (Computer Science) CLASS-XII
Unit-I
Objective Oriented Programming in C++
Review of C++:-
C++ Character Sets:
Letters A-Z , a-z Digits 0-9
Special Symbols Space + – * / ^ \ ( ) [ ] { } = != <> . „ “ $ , ; : % ! & ? _ # <= >= @
White Spaces Blank spaces, horizontal tab, carriage return
Other Characters Any of the 256 ASCII character
Token:-The smallest individual unit in a program is known as token. Tokens used in C++ are:
KEYWORDS :
Keywords are the certain reserved words that convey a special meaning to the compiler. These are reserve for special purpose and must not be used as identifier name.eg for , if, else , this , do, etc.
IDENTIFIERS:
Identifiers are programmer defined names given to the various program elements such as variables, functions, arrays, objects, classes, etc.. It may contain digits, letters and underscore, and must begin with a letter or underscore. C++ is case sensitive as it treats upper and lower case letters differently. The following are some valid identifiers: Pen time580 s2e2r3 _dos _HJI3_JK
LITERALS:
The data items which never change their value throughout the program run. There are several
kinds of literals:
· Integer literals
· Character literals
· Floating literals
· String literals
Integer literals :
Integer literals are whole numbers without any fractional part. An integer literal must have at least one digit and must not contain any decimal point. It may contain either + or – sign. A number with no sign is assumed as positive. C++ allows three types of integer literals:
(i) Decimal Integer Literals:- An integer literal without leading 0 (zero) is called decimal integer literals e.g., 123, 786 , +97 , etc.
(ii) Octal Integer Literals:- A sequence of octal digit starting with 0 (zero) is taken to be an octal integer literal ( zero followed by octal digits). e.g., 0345, 0123 , etc.
(iii) Hexadecimal Integer Literals :- Hexadecimal Integer Literals starts with 0x or 0X followed by any hexa digits. e.g., 0x9A45, 0X1234, etc.
Character literals:
Any single character enclosed within single quotes is a character literal. e.g ‘ A’ , ‘3’
Floating literals:
Numbers which are having the fractional part are referred as floating literals or real literals. It may be a positive or negative number. A number with no sign is assumed to be a positive number. e.g 2.0, 17.5, -0.00256
String Literals:
It is a sequence of character surrounded by double quotes. e.g., “abc” , “23”.
PUNCTUATORS:
The following characters are used as punctuators which are also known as separators in C++ [ ] { } ( ) , ; : * ……….. = #
Punctuator Name Function
[ ] Brackets These indicates single and multidimensional array subscripts
() Parenthesis These indicate function calls and function parameters.
{ } Braces Indicate the start and end of compound statements.
; Semicolon This is a statement terminator.
, Comma It is used as a separator.
: Colon It indicates a labeled statement
* Asterisk It is used as a pointer declaration
… Ellipsis These are used in the formal argument lists of function prototype to indicate a variable number of arguments.
= Equal to It is used as an assigning operator.
# Pound sign This is used as preprocessor directives.
OPERATORS:
An operator is a symbol or character or word which trigger some operation (computation) on its operands.
(i) Unary operators: Those which require only one operand to operate upon. e.g. unary – , unary + , ++ , – – ! .
(ii) Binary operators: Binary operators require two operands to operate upon. e.g. +, *, /, -, etc.
(iii) Ternary Operator :Ternary operator require three operands to operate upon.
Conditional operator (? 🙂 is a ternary operator in C++.
COMMENTS IN A C++ PROGRAM.:
Comments are the pieces of code that compiler ignores to compile. There are two types of comments in C++.
1. Single line comment: The comments that begin with // are single line comments. The Compiler simply ignores everything following // in the same line.
2. Multiline Comment : The multiline comment begin with /* and end with */ . This means everything that falls between /* and */ is consider a comment even though it is spread across many lines.
Input Output (I/O) operations In C++: Input & Output operations are supported by the istream (input stream) and ostream (output stream) classes. The predefined stream objects for input, output are :
(i) The cout Object:
The identifier cout is a predefined object of ostream class that represents the standered output stream in C++ and tied to slandered output. cout stands for console output . cout sends all output to the standard output device i.e. monitor.
The syntax of cout is as follows:
cout<< data;
Where data may be a variable or constant or string etc. e.g.
cout<< a ; ( here a can be any variable)
Output Operator (<<): The output operator (<<) is also known as ‘stream insertion’ or ‘put to’ operator. It directs the contents of the variable (or value) on its right to the object on its left (i.e., cout).
(ii) The cin Object :
The cin object is an istream class object tied to slandered input. cin stands for console input. cin object used to get input from the keyboard. When a program reaches the line with cin, the user at the keyboard can enter values directly into variables.
The syntax of cin is as follows:
cin>> variablename;
e.g
cin>> ch; ( here ch can be any variable)
10
input Operator (>>): The input operator (>>) is also known as extraction or ‘get from’ operator .
It extracts (or takes) the value from the keyboard and assign it to the variable on its right.
CASCADING OF OPERATOR:
When input or output ( >>or <<) are used more than one time in a single statement then it is called as cascading of operators.
e.g cout<< roll<< age<< endl;
DATA TYPES IN C++:
Data types are means to identify the types of data and associated operations of handling it. Data types in C++ are of two types:
1. Fundamental or Built-in data types .
2. Derived data types.
1. Fundamental or Built-in data types: These data types are already known to compiler. These are the data types those are not composed of other data types. There are following fundamental data types in C++:
(i) int data type (for integer) :- int data type is used for integer value. An identifiers declare as int cannot have fractional part.
(iii) char data type (for characters):- An identifiers declare as char can store a character.
(iv) float data type (for floating point numbers):- An identifier declare as float can hold a floating point number.
(v) double data type (for double precision floating point numbers):- The double data type is also used for handling floating point numbers but it occupies twice as much memory as float and store numbers with much larger range and precision. Data Type Modifiers:-There are following four data type modifiers in C++ , which may be used to modify the fundamental datatypes to fit various situations more precisely:
(i) signed
(ii) unsigned
(iii) long
(iv) short
signed, unsigned, long, short data type modifiers may be apply to char & int data types. However you may also apply long to double
Data Type Size (in Bytes) Range
char 1 -128 to 127
unsigned char 1 0 to 255
Signed char 1 Same as char
Int 2 -32768 to 32767
unsigned int 2 0 to 65535
signed int 2 Same as int
short (or short int) 2 -32768 to 32767
long (or long int) 4 -2147483648 to 2147483647
float 4 3.4 x 10-38 to 3.4 x 1038 – 1 (upto 7 digits of precision)
double 8 1.7 x 10-308 to 1.7 x 10308 – 1 (upto 15 digits of precision)
long double 10 3.4 x 10-4932 to 1.1 x 104932 – 1 (upto 19 digits of precision)
2. Derived Data Types:- These are the data types that are composed of fundamental data types. e.g., array, class, structure, etc.
Variables:-A named memory location, whose contains can be changed with in program execution is known as variable. OR
A variable is an identifier that denotes a storage location, which contains can be varied during program execution.
Declaration of Variables:- All variables must be declared before they are used in executable statements. Variable declaration reserves memory required for data storage and associates it with a name. Syntax for variable declaration is:
datatypes variable_name1, variable_name2, variable_name3,……………. ;
e.g.,
int num;
int num, sum, avg;
We can also initialize a variable at the time of declaration by using following syntax:
datatypes variable_name = value;
e.g.,
int num = 0;
Constant:- A named memory location, whose contains cannot be changed with in program execution is known as constant. OR
A constant is an identifier that denotes a storage location, which contains cannot be varied during program execution.
Syntax for constant declaration is:
const datatypes constant_name = value ;
e.g.,
const float pi = 3,14f ;
Formatted Output (Manipulators) :
Manipulators are the operators used with the insertion operator << to format the data display. The most commonly used manipulators are endl and setw.
1. The endl manipulator :The endl manipulator, when used in a output statement , causes a line feed to be inserted. It has same effect as using new line character “\n”. e.g.,
cout<< “ Kendriya Vidyalaya Sangathan”<<endl;
cout<< “ Human Resource and Development”;
The output of the above code will be Kendriya Vidyalaya Sangathan Human Resource and development
2. The setw( ) Manipulator : The setw( ) manipulator sets the width of the field assign for the output. It takes the size of the field (in number of character) as a parameter. The output will be right justified e.g., the code :
cout<<setw(6)<<”R” ;
Generates the following output on the screen (each underscore represent a blank space)
_ _ _ _ _ R
In order to use these manipulator , it is must to include header file iomanip.h