STD 8 Notes C++ 2015-16
STD 8 Notes C++ 2015-16
ICT
INTRODUCTION TO C++
So far, you have learnt a package like MS – Office containing Word, Excel,
PowerPoint, etc. You have also learnt an Operating system i.e. Windows.
What is a program?
List of commands given to the computer to perform a specific task is known as a
program.
Loading C++
You can start C++ by clicking on the short cut icon “Shortcut to Turbo C++” .
C++ automatically opens one program file. It is named as noname00.cpp - cpp
stands for C plus plus.
Your C++ screen indicates the cursor position by displaying the row number and
column number at the left-hand bottom corner of the screen. It also contains the
horizontal and vertical scroll bars and the function key bar.
The above steps can be avoided and the output can be directly seen by
pressing ctrl+F9 keys together also. Even if you choose to use the shortcut
method for execution of your program but the process of Compilation and
Linking takes place in the background. Remember without these processes
the program will not execute.
To begin your C++ program, you require two header files. They are:
iostream.h - input output stream.header file
conio.h - console input output.header file
When you want to write a C++ program, you should begin it with the
following commands:
#include<iostream.h>
#include<conio.h>
These commands are a must for writing any C++ program. Make sure that you
follow the correct syntax.
Next, you need a heading for your C++ program. Every C++ program has a
common heading, which is as follows:
void main( )
C++ is a case sensitive language. Therefore all commands in C++ are written
in lower case.
The body of the C++ program i.e. the commands of a program also called as a
block, is always enclosed in { } (brace or curly brackets).
Few commands of C++
#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
cout<< “Hello World!”;
}
Observe the program carefully. The Body of the program consists of 2 lines. Every
line in the body ends with the semi-colon. As you end every sentence in English
with a full-stop, you must end every command in C++ with a semi-colon.
Whenever, a message has to be displayed on the screen, it should always be
written within quotation marks, i.e. “ ”.
After you complete the program, you need to execute or run the program.
You can do so by pressing Ctrl + F9 keys on the keyboard.
This program will clear your screen and display:
Hello World!
Few shortcut keys of C++ (useful when you type C++ programs)
End key - to reach the cursor at the end of the line
Home key - to reach the cursor at the beginning of the line
Pgup - to move the cursor several lines up
Pgdn - to move the cursor several line down
F5 - to maximize the window
[↑ ↨] - to maximize or restore the window
[ ▌] - to close the file
Escape Sequences for display of output
You can use some options while printing the text on the screen. The options are as
follows:
\n or endl - prints the text on a new line
\t - prints the text with a space or gap of 5 columns
\a - prints the text with a beep sound
For eg:
cout<< “fun”;
cout<< “time”;
will give the output as:
funtime
cout<< “fun\t”;
cout<< “time”;
will give the output as:
fun time
Whenever you use a value in a program, you need to assign it to a variable. Your
command will be as follows:
int a=5;
float x = 12.5;
char ch= ‘a’; note: characters are to be enclosed in single quotation marks.
Sample program for assigning two values and print them on two different
lines:
OR
#include<iostream.h> #include<iostream.h>
#include<conio.h> #include<conio.h>
void main( ) void main( )
{ {
clrscr( ); clrscr( );
int a=5; int a,b;
int b=10; a=5;
cout<< “first number is=”<<a<<endl; b=10;
cout<< “second number =”<<b; cout<< “first number is=”<<a<<endl;
getch( ); cout<< “second number =”<<b;
} getch( );
}
cin command
So far you have been assigning values to the variables within the C++ program.
You will now learn to give values to the variables during program execution.
Here, the user is allowed to give the values to the variables used in the program
through the keyboard.
In case of more than one variable, the cin command will be written as:
OR
cin>>variable 1;
cin>>variable 2;
cin>>variable 3;
Example: cin>>a;
cin>>b;
cin>>c;
A sample program with explanation using cin command:
W.A.P to accept two float values from the user and find the sum and the average.
#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
float a,b,s,avg;
cout<< “Enter values for a and b”<<endl; (message prompt to enter values)
cin>>a>>b; (values entered from keyboard will be accepted by the variables)
s=a+b;
avg=s/2;
cout<< “sum=”<<s<<endl;
cout<< “average=”<<avg;
getch( );
}
Program output:
Program
#include <iostream.h>
#include <conio.h>
void main( )
{
clrscr( );
char ch;
cout<< “Enter any character”<<endl;
cin>>ch;
cout<<ch<<endl;
cout<<ch<<endl;
cout<<ch<<endl;
cout<<ch<<endl;
getch( );
}
Output of the program
*
*
*
*
A group of characters is called as a string. If you want to store a string, i.e. a word
in a variable, you need to declare the approximate size of that string. Such a string
whose length or size is declared or defined is called as character array.
Program
#include <iostream.h>
#include <conio.h>
void main( )
{
clrscr( );
char nm[15];
cout<< “Enter your name”<<endl;
cin>>nm;
cout<< nm<<endl;
cout<< “Welcome to the world of computers”;
getch( );
}
Manju
Welcome to the world of computers
Program
#include <iostream.h>
#include <conio.h>
#include<stdio.h>
void main( )
{
clrscr( );
char nm[25];
int ag;
cout<< “Enter your full name”<<endl;
gets(nm);
cout<< “Enter your age”<<endl;
cin>>ag;
puts(nm);
cout<< “You are”<<ag<< “years old”;
getch( );
}
2. Relational Operators
These operators establish a relation between two values and decide whether it is
true or false.
Operator What it signifies Example
== Equal to a= =b
!= Not equal to a!=b
> Greater than a>b
< Less than a<b
>= Greater than equal to a>=b
<= Less than equal to a <=b
Syntax: if(condition)
{
action to be taken if the condition is true;
}
else
{
action to be taken if the condition is false;
}
Example: Write a program to input your name and age and check eligibility for
voting.
#include <iostream.h>
#include <conio.h>
#include<stdio.h>
void main( )
{
clrscr( );
char nm[25];
int ag;
cout<< “Enter your full name”<<endl;
gets(nm);
cout<< “Enter your age”<<endl;
cin>>ag;
if(ag>=18)
{
cout<<nm<< “You are eligible to vote”;
}
else
{
cout<<nm<< “Sorry you cannot vote”;
}
3. Logical operators
Example 1
“If you have money and if your mother gives you permission then you can go and
have an ice cream”.
In the above statement, we understand that unless both the conditions are satisfied,
you cannot eat ice cream.
Example 2
If x is greater than y and x is greater than z, then x is the largest number amongst
the three numbers x, y and z.
In the above statement, we compare x with the other two numbers y and z and then
arrive at a conclusion that x is the largest.
In order to solve the above program, we require Logical operators, which help us
to combine two or more conditions together.
Logical operator or ( | | )
This operator is used to compare and check any one of the given conditions is true.
If yes, then action to be taken is performed.
Input your age and the amount of money you have. If you are above 15 and you
have Rs.300 or more with you, you can eat out.
Program
#include <iostream.h>
#include <conio.h>
void main( )
{
clrscr( );
int ag,rs;
cout<< “Enter your age”<<endl;
cin>>ag;
cout<< “Enter the amount of money”<<endl;
cin>>rs;
if (ag>15 && rs>=300)
{
cout<< “You can eat out”;
}
else
{
cout<< “sorry, you have to eat at home”;
}
getch( );
}
Output of the program
Enter your age
13
Enter the amount of money
350
In the above program, only one condition is satisfied or true. Hence, the above
output.
IF ELSE-IF STATEMENT
When you have to check for multiple conditions, then we use the if else-if
statement in C++.
Syntax
if (condition 1)
{
action to be taken if condition 1 is true;
}
else if (condition 2)
{
action to be taken if condition 2 is true;
}
else
{
action to be taken if both conditions 1 and 2 are false;
}
Sample program
A program to input 3 numbers and find out the largest among them.
#include <iostream.h>
#include <conio.h>
void main( )
{
clrscr( );
int a,b,c;
cout<< “Enter the 3 numbers”<<endl;
cin>>a>>b>>c;
if (a>b && a>c)
{
cout<<a<< “is the largest”;
}
else if (b>a && b>c)
{
cout<<b<< “is the largest”;
}
else
{
cout<< c<< “is the largest”;
}
getch( );
}
25 is the largest
Loops are used in a program to repeat a statement for a given number of times. For
eg: loops can be used for displaying a string many times, for counting numbers,
calculating the sum of the numbers, etc.
Example: To print numbers from 1 to 5 we will write the ‘for’ loop as follows:
for(i=0;i<=5;i++) OR for(i=0;i<=5;i=i+1)
2. Body of the loop: A set of statements which are executed within the loop.
3. Test condition: Each loop has a test condition. Depending upon the test
condition, the loop will be repeated or terminated. If the test condition is true,
the control of the program enters the body of the loop for executing the
statements within the loop. If the condition is false, the loop terminates. In the
above example, i<=5, is the test condition.
4. Step value (increment or decrement): The step value in a loop determines the
increment (increase) or decrement (decrease) of the control variable. In the above
example, i++ or i=i+1, is the step value or increment.
Now, we will see the working of the for loop with an example:
#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int i;
for(i=1;i<=5;i++)
{
cout<< “Hello”<<endl; (Body of the loop)
}
getch( );
}
1. The variable i is initialized to 1.
2. Control checks for the condition, i<=5. If it is true, then it enters the body of
the loop and executes the statement.
3. After executing the statement, it returns back to the for statement and
increases the value of i by 1. The value of i, now becomes 2. The condition,
i<=5 is checked again. If it is true, then it enters the body of the loop and
executes the statement again.
4. Thus, this process is repeated, till the test condition becomes false or value
of i exceeds 5.
**************************