7/29/2019 CLASS XI CPP NOTES
1/27
C++ Notes
What is C++?
C++ is an object-oriented programming language. It was developed by Bjarne Stroustrup at
AT&T Bell Laboratories in Murray Hill, New Jersey, USA, in the early 1980s.
Conversion of source program to executable fileThe conversion of a source file into an executable file is a two step process:
1. First you compile your source file into an object file.2. Link it with necessary library routines.
Source Code/File: The program written in a programming language like C++ is called asource file, or source code. It is not an executable file. In C++ it has .CPP extension.
Object Code/File: Object code/file is the representation of codes that compiler orassembler generates by processing a source code/file. Object files contain compact
code, often called binaries. Its extension in DOS is .OBJ.
Executable Code/File: Executable code/file, in computer science, is as file whosecontents are meant to be interpreted by operating system. Its extension in DOS is .EXE.
Compiler: It is a program which converts the program written in a programminglanguage to a program in machine language. Every programming language has its own
compiler.
Linker: It is a program which takes a compiled program to the necessary library routines,to make it an executable program.
Compiling: The process of converting the source program into an object program iscalled compiling. In this, program written in any programming language is converted into
the machine language with the help of compiler.
Linking: The process of converting the object program into an executable program iscalled linking. In this the program in machine language instruction is linked with
necessary library routines, with the help of linker.
Prog1.CPP
Prog1.OBJ
Prog1.EXE
COMPILER
SOURCE FILE
LINKER
OBJECT FILE
EXECUTABLE FILE
7/29/2019 CLASS XI CPP NOTES
2/27
C++ Notes
C++ Character set: It is a set of characters that can be used in C++ program. It consists ofalphabets (A..Z, a..z), digits (0 ...9), and special characters (&, :, ;, *, space etc).
C++ Tokens: A Token is a group of characters that logically belong together. It is thesmallest individual unit in a program. C++ supports the following types of tokens:
a) Identifier: It is a symbolic name given by a programmer for any data item orfunction. The identifier is a sequence of characters takes from C++ character set. The
rules for formation of identifier are:
Can consist of alphabets, digits and special character underscore ( _ ). Can start with an alphabet or underscore, but not with digit. C++ is case sensitive. Blank space not allowed
b) Variable: It is a location in the computer memory which can store data and is given asymbolic name for easy reference. Its value can change during program execution.The syntax for declaring a variable:
[ = ],. ; Example :
char grade;
int a, b=10, c;
Data type has to be any one of the data types present in C++. The basic data types
available in C++ are:
Data
Types Range
Size
(Bytes) Usages
char 0 to 255 1 For storing Characters
int -32768 to 32767 2 For storing numbers without decimal
long -2,147,483,648 to 2,147,483,647 4 For storing numbers without decimal
float - 3.4 x 1038
to 3.4 x 1038
- 1 4For storing floating point numbers. It
has seven digits of precision.
double - 1.7 x 10308
to 1.7 x 10308
- 1 8For storing floating point numbers. It
has fifteen digits of precision.
long
double- 3.4 x 10
4932to 3.4 x 10
4932- 1 10
For storing floating point numbers. It
has nineteen digits of precision.
c) Keywords: A keyword is a reserved word that has a predefined meaning and purposein the language. It cannot be used as an identifier by the user in this program. e.g.
float
d) Constant: It is an identifier whose value does not change during program execution.The syntax for declaring a constant is
const = ; For e.g. const float pi = 3.14;
7/29/2019 CLASS XI CPP NOTES
3/27
C++ Notes
e) Operators: An operator is a symbol or letter used to indicate a specific operation onvariable or value in a program. There are three types of operators in C++:
Unary operators: Operators which operate on one operand. For e.g. Theincrement (++), the decrement (--), and logical NOT (!) are the unary operator.
Binary operators: Operators which operate on two operands are called binaryoperators. For example, all arithmetic operators, all relational operators and
logical AND (&&), and OR (||) are the binary operators.
Ternary Operators: Operators which operate on three operands are calledternary operators. For example, the conditional (? : ) operator is a ternary
operator.
f) Punctuators/Separators: The following characters are used as punctuators/separators inC++: ( ) { } , ; : * # [ ]
# : It is called the preprocess directive in C++. A preprocessor directive is an instructionto the compiler itself. A part of the compiler called preprocessor, deals with thesedirectives, before real compilation process.
The #include directive: The preprocessor directive #include tells the compiler to insertanother file into your source file. In effect, the #include directive is replace by the
contents of the file indicated.
Output using cout: cout statement is used to display a value on screen. The cout isalways used with the insertion operator (
7/29/2019 CLASS XI CPP NOTES
4/27
C++ Notes
For example: coutb>>c;
Assignment Operator ( = ): The equal sign, causes the value on right to be assigned to be
assigned to variable on the left. The statements which assign values to variables are called
assignment statements.Syntax:
Variable = value/variable/expression
Example: a = 10;
Arithmetic Operator: The arithmetic operators available in C++ are:
Operator Usage
+ Used for Addition
- Used for Subtraction
* Used for multiplication
/ Used for division
%
This operator is called the remainder or the modulus operator. It is used to find
the remainder when one integer value/variable is divided by another
value/variable. This operator cannot be used with floating type variables/values.
For e.g.
int a=10, b=3;
cout
7/29/2019 CLASS XI CPP NOTES
5/27
C++ Notes
Logical Operators: Two conditions can be logically combined with the help of logical
operators. The logical operators present in C++ are:
Operator Usage Example
&& AND
The compound condition evaluates to true,
if both the conditions in the compound
condition evaluate to true.
((a>b) && (a>c))
|| OR
The compound condition evaluates to true,
if any or both the conditions in the
compound condition evaluate to true.
((a>b) || (a>c))
! NOT
It negates the condition. That is:
If the condition evaluates to true, itmakes it false.
If the condition evaluates to false, itmakes it true.
!(a>b)
Arithmetic Assignment Operators (Shorthand Operators): Arithmetic assignment operator,
is one,, which combines arithmetic operator and an assignment operator. The arithmetic
assignment operators present in C++ are:
Operator Example Meaning
+= A += B; A = A + B;
-= A -= B; A = A - B;
*= A *= B; A = A * B;
/= A /= B; A = A / B;
%= A %= B; A = A % B;
Increment/Decrement Operators (Unary Operator):
Increment ( ++ ) Operator: It is used to increment the value of variable by 1. It can be used
in the following two ways:
Pre Increment Post Increment
In this, the operator precedes the variable.
Example: ++a
In this, then operator follows the variable.
Example: a++
b = ++a;
This statement is equivalent to the following
two statements:
a = a + 1;
b = a;
So, in pre increment the value of the variable
is incremented, before it is used in any other
operation (Assignment in this case)
b = a++;
This statement is equivalent to the following
two statements:
b = a;
a = a + 1;
So, in post increment the value of the
variable is incremented, after it is used in
any other operation (Assignment in this
case)
7/29/2019 CLASS XI CPP NOTES
6/27
C++ Notes
Decrement (--) Operator: It is used to decrement the values of variable by 1. It can be used
in the following two ways:
Pre Decrement Post Decrement
In this, the operator precedes the variable.
Example: --a
In this, then operator follows the variable.
Example: a--
b = --a;
This statement is equivalent to the following
two statements:
a = a - 1;
b = a;
So, in pre decrement the value of the
variable is decremented, before it is used in
any other operation (Assignment in thiscase)
b = a--;
This statement is equivalent to the following
two statements:
b = a;
a = a - 1;
So, in post decrement the value of the
variable is decremented, after it is used in
any other operation (Assignment in thiscase)
Conditional Operator (? : ): The question marks (?) and the colon (: ) make up the
conditional operator. The operator operates on three operands that are why it is called the
ternary operator. The whole expression is called the conditional expression. The expression
before the question mark is called the test expression. If the test expression is true, then the
entire conditional takes on the value of the operands after the question mark. If the test
expression evaluates to false, then the conditional expression takes on the value of the
operand following the colon.
Syntax:
? :
Example:
res = ( x < y ) ? x : y;
In the above example, if the test expression evaluate to true the variable res will be assigned
the value of the variable after the question mark, i.e. x else it will be assigned the value of
the variable after the colon i.e. y.
Example:
cout
7/29/2019 CLASS XI CPP NOTES
7/27
C++ Notes
Conversion: Automatic Type conversion: When two operands of different data types are
encountered in the same expression, the variable of lower data type is automatically
converted to the data type of variable with higher data type, and then the
expression is calculated.
For Example:
int a=98;
float b=5;
cout
7/29/2019 CLASS XI CPP NOTES
8/27
C++ Notes
else
{
Statement 1;
:
Statement N;
}
{
cout
7/29/2019 CLASS XI CPP NOTES
9/27
C++ Notes
Syntax Example
switch ( )
{
case : Statement 1;
:
Statement n;
case : Statement 1;
:
Statement n;
case : Statement 1;
:
Statement n;
case : Statement 1;
:
Statement n;:
:
default : Statement 1;
:
Statement n;
}
Note: default case is optional.
switch(color)
{
case 3: cout
7/29/2019 CLASS XI CPP NOTES
10/27
C++ Notes
Manipulators: Manipulators are operators used with the insertion operator (
7/29/2019 CLASS XI CPP NOTES
11/27
C++ Notes
example 1(a) the loop continues if the value of a
7/29/2019 CLASS XI CPP NOTES
12/27
C++ Notes
While Loop:
The while loop executes a section of the program till a specified condition is true. While loop
is used when the number of times the loop is to be executed is not known. The condition is
always checked first before the loop is executed.
Syntax:
while()
{
statement 1;
statement 2; Loop Body
:
statement n;
}
Examples:
1. while-loop with a single statement 2. The execution of the following loop
depends on the value of Ans, therefore the
number of times the loop is executed is notknown.
int a=1;
while (a
7/29/2019 CLASS XI CPP NOTES
13/27
C++ Notes
1. 2.
int a=1;
do
{
cout
7/29/2019 CLASS XI CPP NOTES
14/27
C++ Notes
Nested Loops:
When a for, while or do..while loop is used inside another looping construct, the concept is
called a nested loop. This concept is used whenever for each repetition of a process many
repetitions of another process are required.
Some sample skeletal structures ofNested Loopsfor( .. )
{
for( .. )
}
while( .. )
{
while( .. )
}
Do
{
while( .. )
}
while( .. );
do
{
for( .. )
}
while( .. );
Example:
for( int p = 1; p
7/29/2019 CLASS XI CPP NOTES
15/27
C++ Notes [Function]
Function: A function is a named unit of a group of statements that can be invoked from
other parts of the program. The advantages of using functions are:
Functions enable us to break a program down into a number of smaller and smaller andsimpler units. This makes the program more organized and structured and easier to
debug.
It helps in reusability of code and thus reduces program size. If similar code is requiredat several places in the program, the use of a function allows this code to be written just
once, and to be called wherever it is required.
It helps to execute the same set of statements with different set of values.Function Declaration/Prototype: Just as any variable is declare before it is used in a
program, it is necessary to declare a function to inform the compiler that the function would
be referenced at a later point in the program. Function declaration is also called function
prototype.
Syntax for function declaration:
( [], . );
Example: int sum ( int, int);
A declaration (prototype) tells the compiler that later on in our program we are going to
write a particular function. It also informs the compiler about:
1. the type of result to be returned;
2. the identifier of the function;
3. the number and types of parameters;
It is terminated by a semicolon.
: It is the data type of the value that is returned to the calling function after
the function is executed. It can be any of the data types, available in C++.
: It is the name of the function, which is given with accordance to the
naming conventions used for naming an identifier. A function is called with the help of its
name.
: These are the data types of the parameters/inputs that the
function receives when it is called. In function declaration it is optional to give the name of
the parameters. The different data types specify to the compiler, the total number of
parameters, and the data type of these parameters that the function will receive, when it is
called (invoked).
Function definition: The function definition contains the function header and the function
body i.e. the set of statements which are executed when the function is called.
Syntax for function definition:
(
7/29/2019 CLASS XI CPP NOTES
16/27
C++ Notes [Function]
return ( value / variable / expression );
}
Example:
int sum ( int a, int b)
{
int s = a + b;
return s;
}
Note: The return statement is used to return a value to the calling function. It is always the
last statement in the function definition (if the function has return type). The data type of
the value returned should match with the return type of the function.
// Example 1
// Program to find the factorial of a number using a function Fact ()
#include
#include
long Fact( long); // Function Prototype
void main()
{
long num;
clrscr();
coutnum;
cout
7/29/2019 CLASS XI CPP NOTES
17/27
C++ Notes [Function]
Function Header: It is the first line of the function definition, which specifies the following:
Return type of the function Name of the function Name of the parameters, along with their data types
Note: It is not terminated with semicolon.
Function Body: It is the set of statements, which are executed when the function is called.
Function Call: It is the statement in the calling function, through which the control is passed
to the function definition of another function. This will execute the statements is the called
function and then the control is returned back to the statement in the calling function,
which follows the function call statement. The function call consists of the function name,
followed by actual parameters, if any, enclosed in parentheses.
Calling Function: The function, which calls a function is called the calling function. In the
above example main() is the calling function.
Called Function: The function which is called by anther function is known as the called
function.
Formal Parameter: The parameters mentioned in the function header are called formal
parameter.
Actual Parameter: Values/Variables which are used while making a call to the function are
called actual parameter.
Difference between call by value and call by reference
Call by Reference Call by Value
1) The changes made to the formal
parameters are reflected back to the actual
parameters.
1) The changes made to the formal
parameters are not reflected back to the
actual parameters.
2) It provides a mechanism of returningmore than one value.
2) Not Available
3) The reference parameters require an
ampersand (&) sign before their name.
3) Not Available
4) The Formal and Actual parameters share
the same memory location.
4) The formal and actual parameters have
different memory location.
5) void Swap(int & a, int & b){
int temp = a;
a = b;
b = temp;}
5) void Swap(int a, int b)
{
int temp = a;
a = b;
b = temp;}
// Program to illustrate the invoking of a function using call by value
// and call by reference methods
#include
#include
// Function to swap the values of two variables,
// where both the parameters are value parameters
void swapv ( int a, int b)
{
int temp = a;
a = b;
7/29/2019 CLASS XI CPP NOTES
18/27
C++ Notes [Function]
b = temp;
cout
7/29/2019 CLASS XI CPP NOTES
19/27
C++ Notes [Function]
void main()
{
clrscr();
int a,b,c;
couta>>b;
c = Sum(a,b); // Function call statement, the returned value is stored in the variable c
cout
7/29/2019 CLASS XI CPP NOTES
20/27
C++ Notes [Function]
In-Built functions in Header File
Function Usage Example Result
1. fabs(x) Calculates the absolute value
of the floating point number x.
cout
7/29/2019 CLASS XI CPP NOTES
21/27
C++ Notes [Function]
cout
7/29/2019 CLASS XI CPP NOTES
22/27
C++ Notes [Function]
}
}
return lcm;
}
int GCD(int a, int b)
{
int i, gcd;
for(i=1; i>b;
l = LCM(a,b);
cout
7/29/2019 CLASS XI CPP NOTES
23/27
What is difference between Actual Parameters and Formal Parameters? Give an
example in C++to illustrate both types ofparameters.
ActualParameter Formal ParameterA parameterused in a function call isknown as actual parameter. It is used tosend data to function.
A parameterused in a functiondefinition is known as formal parameter.It is used to accept data from actualparameters.
Void abc(int a) // a is formal parameter{Int x=9;x=x+a;}Void main(){Int p=6;abc(p); } // p is actual parameter
What do you mean by function prototyping? Write down the advantages of
function prototypes in C++?
Ans. Function prototyping means writing the function prototypes before their
function call occurs. A function prototype is a declaration of the function tells the
program about the type of the value re tu rned by the function and the numberand
type of arguments. The advantage of function prototyping is that it enables a
compiler to compare each use of function with the prototype to determine whether
the function is invoked properly or not. The numberand types of arguments can be
easily compared and any wrong number of types of the argument is reported.
Therefore, function prototyping makes C++straightaway point to the error.
How is global prototype different from a local prototype?
Ans. Ifa function prototype is placed in the body ofanother function, it is local
prototype and the function is locally available to the function that declares it.
Ifa function prototype is placed outside all the functions, it is global prototype and
the function is globally available to all the functions.
7/29/2019 CLASS XI CPP NOTES
24/27
-------------------------------------------------------------------------------------------------------
Sample Paper Computer science
Class: XI------------------------------------------------------------------------------------------------------------Time Duration: 3 Hours Maximum Marks: 70
------------------------------------------------------------------------------------------------------------
Instructions:
All Questions are Compulsory.
I. 1. Name different types of digital computers based on their size and performance. [1]2. What is system software? [1]
3. Explain first come first served (FCFS) scheduling. [2]
4. Explain multiprocessing operating system. [2]
(Total: 6M)II. 1. Differentiate between syntax error and semantics error. Give example for each. [2]
2. What do you mean by robustness of a program? What is guard code? [2]3. What is a base class? What is a derived class? How are these two interrelated? [3]
4. Explain:
a.) Encapsulationb.) Transitive nature of inheritance
c.) portability [3]
(Total: 10M)
III. 1. Convert the following binary number to decimal:a) 101.1001 b) 11101.111
2. Convert the following octal values to decimala) 1204.3 b) 743
3. Convert the following decimal values to hexadecimal:
a) 314.21 b) 2048
4. Convert the following decimal values to binary:
a) 57.31 b) 29.25. Convert the following binary number to hexadecimal:
a) 1010110110111 b) 10110111011011 (1M X 5 =5M)
6. Differentiate between:
a) RAM and ROM b) impact printer and non impact printer. [4]7. What is a port? [1]
(Total: 10M)
IV. 1. Determine then output if the input is:
( a ) 2000 ( b ) 1900 ( c ) 1971
void main( ){
int year;
cin>>year;
if ( year%100 = =0)
7/29/2019 CLASS XI CPP NOTES
25/27
-------------------------------------------------------------------------------------------------------
{
if ( year % 400 = =0)
cout
7/29/2019 CLASS XI CPP NOTES
26/27
-------------------------------------------------------------------------------------------------------
4. What is the effect of absence of break in a switch statement? Can the case labels
in a switch have identical values?
5. Rewrite the following code fragment using switch:
void main ( ){
if (ch = = E)
cout
7/29/2019 CLASS XI CPP NOTES
27/27
f * = i;
cout