Dark Star
Cyborg Agent
C++ LEARNING GUIDE
This is a basic guide which I have come up with for the people who are looking to go for C++. I have tried to keep it simple so that everyone can understand it.
I had been working on this for last couple of weeks. Any comments/suggestions are welcome.
NOTE: This is a beginners guide so don’t expect too much, I made this guide . Keeping Mind that learner is a newbie in C++. Hey now don’t think I am an expert. Sorry for the mistakes I have tried to cover every aspect.
INTRODUCTION
C++ is an object oriented programming language. It was developed by Dr.Bjarne Stroustrup in the year 1983 at AT&T Bell laboratories, New Jersey {U.S.A}. It was originally named as C with classes. The name is derived with the increment operation in "C" which is ‘++’. So it is the incremented version of C.
An object oriented programming language means collection of objects which are self contain collection of both data structure and functions that interact with other objects. It adds classes, inheritance, functions overloading and operator overloading.
“With the help of C++ we can develop editors, compilers, communication systems, database and other real life time applications.
Note: - C++ codes are case sensitive so every command/codes written should be in Small Letters
CHARACHTER SETS
There are 2 types of character sets:-
• Source Character :- The source text is created with the help of source character such as :
Alphabet: - A-Z, a-z and _.
• Digits: - 0-9.
• Special Characters: - +, -, *, /, ^, ~, %, =, !, &, |, ( ), [ ], ?, “ “, ; , : , \, #, .
ESCAPE SEQUENCE
The characters are interpreted at run time .These escape sequence character are represented by a back slash ‘/’ followed by a character.
For e.g.:- \n, \t, \a etc.
KEY WORDS OR RESERVED WORDS
These words are reserved to do specific task and must not be used as a normal identifier name.
For e.g.:- if, for, else, do, while etc.
IDENTIFIER
Identifier are the fundamental building blocks of a program and are used to gives names to variable, function, arrays, objects, classes etc.
CONSTANT
[/COLOR]These are the items which cannot be changed during the program execution. Every constant used in a program as a type that is determined by its form and a value .
They are mainly of 3 types:-
• Numeric Constant
• String Constant /Literals
• Character Constant
• Floating Constant
INTEGER CONSTANT
One whole number without any factorial part. The integer constant may be Decimal (base 10), Octal (base 8), and Hexadecimal (base 16).
CHRACTER CONSTANT
A character constant in C++ can be any of the valid in ASCII character and is enclosed in single quotes (‘‘)
For e.g.:- char grade=’A’
General Character can be represented by above method but for special character like tab, backspace etc. C++ provides escape sequence as already told by me. It is represented by backslash (\) followed by one or more character.
Escape sequence non graphic characters:-
\a = Audible Bell
\b = Backspace
\f = Formfeeds
\n = New line (\n is same as endl \n is used inside quotes but endl doesn’t)
\r = Carriage Return
\t = Horizontal Tab
\v = Vertical Tab
\\ = Backslash
\’ = Single Quote
\”= Double Quote
\? = Question Mark
\On = Octal number
\xHN = Hexadecimal Number
NOTE: -- These Escape sequence should always be written inside ““double quotes.
As “\n”.
FLOATING CONSTANT
These are also called real constants, they contain factorial part too. A real constant in exponent form has two parts: a mantissa and an exponent.
SEPARATORS
( ) = Parentheses
These are used for function call and function parameter.
{ } = Braces
These are used for blocking codes having simple or compound statement.
[ ] = Brackets
These are used for enclosing subscripts in case of a single and multi dimensional arrays.
, = Comma
These are used for separating arguments list in a function.
; = Semicolon
These are used as a statement terminator.
: = Colon
These are used in case of a labeled statement.
AIRTHMATICAL OPERATORS
Some of the arithmetical operator are +,-, *, /, %.
+ = Add
Gives the addition of two or more numbers. (A+B)
- = Subtraction
Gives the subtract of two or more number (A-B)
* = Multiplication
Gives the multiple of two or more numbers (A*B)
/ = Divide
Gives the divide of two or more numbers (A/B)
% = Remainder=Modulus
Gives the remainder of two or more number (A%B)
Note: Where A & B are integers.
KEY WORDS
Keywords are reserved words in C++. They convey special message to language compiler. It has some of the following Keyword but we will learn some of them here.
asm, continue, float, new, signed, try, auto, default, for, operator, sizeof, typed, break,
delete, friend, private, static, union, do, goto, protected, struct, unsigned, while, do while,
DATA TYPES
They are used to indicate char, int, float, double, long double etc .The chart showing the allocation is as follows:-
*img167.imageshack.us/img167/3821/chartfw8.png
The following are the meaning of the words written in table:-
# Char(For Character)
# Int (For Integer)
# Double (For Integer)
# Long (For Integer)
# Long Double (For Integer)
# Float (Decimals)
# Void (For non returning function)
RELATIONAL OPERATOR
<= Less than
>= Greater than
= - Equal to (Assignment Operator)
== - Double Equal to (Conditional Operator)
LOGICAL OPERATORS
&& - To give and logic
! - Not equals to
|| - To give or logic
CIN COUT
The Identifier cout>> is a predefine object that corresponds to the standard output stream.
The Identifier cin>> is a predefine object that corresponds to the standard input stream.
“<<” – Output of data.
The output of data is known as insertion output to operator.
“>>” – Input of data.
Input of data is known as extraction or to get data from the operator.
Cout<< is the command used to print the value visually in the screen and cin>> for getting the info. from user.
HEADER FILES
Header files as the name suggest are the head of the program and are written outside the main body. These files contain commands to run codes like cin>>, cout<<, getch (); clrscr (); etc...
The files we are going to learn here are.
#include<iostream.h> : The main header file has the commands to run commands like clrscr(); and getch();
#include<conio.h> : This header file has codes to run cin>> & cout<<.
#include<stdio.h> : This header file has a function called gets which is used as same as cin>> but it is mainly used for entering character and it reads spaces bt. Character while cin>> doesn’t read spaces in character .
For E.g.:- cin>>name;
Suppose user had entered Joe joe
Then cout<<name; will give the output Joe
While gets(name); will give same output as the name entered.
Also clrscr(); means Clear the screen if this does not given then the output of previously opened will also come along the output of your current program output.
getch(); is the command to give a pause to the console output screen , if not entered then the output will be shown but if u will enter any value the screen will return to the coding window.
Ahhhhhhhh…. Now I have completed the theory part Hope every one understood it if not ask without any hesitation. Let’s start the main Programming part.
STARTING PROGRAMMING BASICS
First while writing the program we should keep in minds that we should do less mistakes. So without wasting time I am giving the way how to write a program.
The following steps should be taken:-
#include <Header File>
void main()
{
<Variable Declaration>
<Program Statement>
getch();
}
Note: - After void main() ';' semicolon should not be placed other wise error will occur. Instead of void main() only main() can be used but at the end the program should return a value. Otherwise use void main() if u are a beginner.
Giving getch() is not an essential part of the program. It is only given to pause(rather waits for a keystroke ... more precisely your program is waiting for an input) after the completion of the program, since most people run the program pressing Ctrl+F9 and without it, it may seem to you that there was no output given.
SIMPLE PROGRAMMING
I will start with the basic programming, so to create a simple program use the method stated above or just see the following program or download the file attached to clear our confusion.
I am starting with simple addition program:-
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
a=10;
b=20;
c=a+b;
cout<<"Total=\t"<<c;
getch();
}
In the following program I have taken 3 variables and marked them as in Integers so they could hold Integral Values. Then I have assigned the value a=10 and b =20. U will notice that I had not assigned any value for C because c will hold the additive value of A and B. Then at the last I have printed the value.In the same way u could make pro. Of sub, div, multi and of remainder.
The above program has predefined value but if u want to enter the value while running the program foll. Steps should be taken.
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<"Enter the value of a \n";
cin>>a;
cout<<”Enter the value of b\n”;
cin>>b;
c=a+b;
cout<<"Total=\t"<<c;
getch();
}
Here user is entering the values so the total will come as the value is entered.
Remember there should not be any change in the value u have taken .
For eg. U have taken the following codes;
I am starting directly from void main()
Code:
Void main()
{
clrscr();
int a;
cout<<”Enter the value of A”<<endl;
cin>>A;
In this case program has error as u have taken small a as int and entering the value of A in cin>>. So take same variable u have taken. Hope u understand me.
Program to input character:-
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char name[20];
cout<<”Enter your name or any alphabet\n”;
cin>>name;
cout<<”Your name/ Alphabet”<<name;
getch();
}
Here u will notice that after char name I have taken some value inside [ ]. I have done this because in C++ char has 1 value assigned to it but if u will enter name then C++ will print only the 1’st alphabet of the name/string. To avoid this we can define value to char variable till 128.
The main defects of the program is that it will not read spaces bt. Name, but if u want that computer should read spaces then following program should be written.
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char name[20];
cout<<”Enter your name or any alphabet\n”;
gets(name);
cout<<”Your name/ Alphabet”<<name;
getch();
}
Here I had taken one extra header file and instead of cin>> I had taken gets as I had already stated earlier that cin>> does not read spaces. So gets should be entered. Hope it is clear again I am telling u any doubt ask me I will clear it.
Now some basic programs of taking out area ,simple interest etc..
To calculate the area of square.
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int side,area;
cout<<”Enter the side of the square so as to find the area\n”;
cin>>side;
area=side*side;
cout<<”The area of the square is \t”<<area;
getch();
}
Here user is entering data and hence the area is find. Similarly I am showing u how to find the area but don’t expect much I written 10+ page and I had to complete till looping. So take a quick look.
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int radius;
cout<<”Enter the radius of the circle so as to find the area\n”;
cin>>radius;
area=(22*r*r)/7;
cout<<”The area of the Circle is \t”<<area;
getch();
}
Here user is entering the value and hence area is find. Similarly u could find area of diff. things. If u can recognize the formula.
Now I am telling u the program of swapping of 2 variable by using 3’rd variable .
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
//Swapping of 2 variable without using third variable
int a,b,c;
cout<<"Enter the value of variable A\n";
cin>>a;
cout<<"Enter the value of variable B\n";
cin>>b;
cout<<"The Entered value are"<<endl;
cout<<"A="<<a;
cout<<"\t B ="<<b;
c=a;
a=b;
b=c;
//Values after swapping
cout<<"\n The Swapped value are\n";
cout<<"A="<<a;
cout<<"\tB="<<b;
getch();
}
Here with the help of C variable I swapped the value of A and B. Always remember the transfer of value goes to right to left. As a value being transferred to c then a is empty, again b’s value is transferring to a now b is empty. Now again c value is transferred to b. So values are interchanged means swapped.
Similarly U could enter name, integer etc and can calculate diff. things but for now I am tired will post the rest soon
DECISION MAKING/CONDITION CODING
C++ has a huge list of decision making/condition coding statement some of them that we are going to learn are:-
1)if statement
2)if else statement
3)do while statement[will learn while looping]
IF STATEMENT
The if statement may be implemented in different forms depending on the complexity of conditions to be tested.
• Simple if statement
• If ………else statement
• If ………else ….. if ladder statement.
Note: - I have attached 2 MS word file i.e. meant to be downloaded. The 1’st one consist program related to simple programming and the other of if else programming.
You just need to download them and write the program in your compiler and enjoy.
SIMPLE IF STATEMENT
The general form of simple if else statement as already written above is:-
if(Test Condition)
{
<Program Statement>
}
statement;
For e.g.:-
Code:
if(income>4000)
{
tax = 0.1*income;
income = income – tax;
}
cout<<”Tax”<<tax;
cout<<”Income”<<income;
……
The above part of the program is testing whether the income is greater than 4000 or not. If ‘yes’ the tax @of 10% is deducted from the income and then the final v alue should be printed.
Note:-In the whole programming condition = = must be used because we are comparing/conditioning the values so always use = =. Also && should be use for and. AND || this should be used for or condition.
THE IF….ELSE STATEMENT
The if ….else statement is an extension of simple if statement. The general form is:-
if(Test Condition)
{
<Program Statement>
}
else
{
<Program Statement>
}
If the test condition is true then the program statement under if braces will be executed. In either case if the condition become false then else statement will proceed.
Note: - You need to put braces { } at the starting and at the end if to or more program condition is given otherwise it may cause error. For e.g.:-
For e.g.:-
Code:
If (code<0)
Cout<<”Code is negative number”;
else
Cout<<”Code is positive number”;
In above program, if code is less than 0 than the code number entered is negative if not then it is positive.
SOME PROGRAMING BASICS
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
cout<<"Enter the 2 no";
cout<<endl<<"Enter A\n";
cin>>a;
cout<<"Enter B";
cout<<"\n";cin>>b;
if(a>b)
cout<<"A is Greatest\n";
else
cout<<"B is Greatest \n";
getch();
}
This program is absolutely correct but if the same program is written with few more condition then following steps should be taken:-
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
cout<<"Enter the 2 no";
cout<<endl<<"Enter A\n";
cin>>a;
cout<<"Enter B";
cout<<"\n";cin>>b;
if(a>b)
{
cout<<”A=\t”<<a;
cout<<”B=\t”<<b;
cout<<"A is Greatest\n";
}
else if(a==b)
{
cout<<”A=\t”<<a;
cout<<”B=\t”<<b;
cout<<"Both are equal\n";
}
else
{
cout<<”A=\t”<<a;
cout<<”B=\t”<<b;
cout<<"B is Greatest \n";
cout<<”A=\t”<<a;
cout<<”B=\t”<<b;
}
getch();
}
Here = = is used because comparison is done between 2 or more variable if u want to put character then use single quotes ( ‘ ‘ ) or double quotes. But unfortunately we cannot use more than one alphabet also if u assigned the value of char to 128. The e.g. given here is directly started with if else so don’t get confused:-
For e.g:-
if(day= =’s’&& day= =’S’)
cout<<”The day is Sunday”;
But if u will give more than one alphabet then error will come. In the under written program.
if(day= =’su’ && day= =’Su’)
cout<<”The day is Sunday”;
THE IF…..ELSE….IF LADDER
This is another way of putting it together when multiple decision are involved. A multiple decision is a chain of its in which several statements are attached with each if and else conditions.
The general form Is:-
if(condition a)
{
<Program Statement>
}
else if<condition b>
{
<Program Statement>
}
else if<condition c>
{
<Program Statement>
}
………
This construct is known as if….else…..if ladder . The condition are evaluated from top to down. As soon as a true condition is found the program statement attached to it will be executed otherwise compiler will look for other condition .
This type of program can be used for making results as follows.:-
The program written below shows Simple Interest based on several condition.
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int t;
double amt,si;
cout<<"Enter the value of amount\n";
cin>>amt;
cout<<"Enter the total time period\n";
cin>>t;
if(amt>50000 && t>5)
si=(amt*t*11)/100;
else if(amt>=20000 && amt<50000 && t>8)
si=(amt*t*12/100);
else if(amt<20000 && t>=1)
si=(amt*t*6)/100;
cout<<"\nThe amount entered=\t\t"<<amt;
cout<<"\nThe Time entered=\t\t"<<t;
cout<<"\nThe Int. charged=\t\t"<<si;
cout<<"\nThe total amt. to be paid=\t"<<amt+si;
getch();
}