User Guides C++ Learning Guide

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
[BREAK=New Page]

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​

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.
[BREAK=New Page]

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:-

Types Range Bytes
Char[Character] -128 – 128 1
Int[Integer] -32768 – 32767 2
Float[Decimal] 3.4 *10 raised to -38 - 38 4
Double[Integer] 1.7 *10 raised to -308 - 308 8
Long Double [Integer] 3.4 *10 raised to -4932 - 4932 10
Long[Integer] N/Known 12

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.
[BREAK=New Page]
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 Tech Talkz
Then cout<<name; will give the output Tech
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.

[BREAK=New Page]

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 with its .exe file to clear our confusion.
I am starting with simple addition program:-
#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.

#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()

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:-

#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.
#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.

#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.
#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 .

#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
Contd.. till if else & looping.

Regards hope u all like it.


:clap:
 
Sashwat,

Sorry to interfere but I am interested to learn C can you direct me the right path, I don't know anything about programming language.

Thanks
 
Stick said:
Sashwat,

Sorry to interfere but I am interested to learn C can you direct me the right path, I don't know anything about programming language.

Thanks

GO FOR C++ IT IS THE INCREMENTED VER. SO WILL LEARN MORE FROM THAT. aND SO LESS REPLY:S
 
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.

My tutor said this '/' as FORWARD SLASH, what a mess yaar? Shashwat m i right?
 
Back
Top