Tutorial: Learn All The Basics Of C++ Language

Status
Not open for further replies.

TechnoBOY

Padawan

C++ Coding??? Dread !!!!
Forget it now here is the
Complete Antidote!!!


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.

C++ Charactor Set - Constants

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.



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-1281
Int[integer]
Float[decimal]
-32768-3267
3.4*10raised to -38-38
2
4
Double [integer]
long double[integer]
1.7*10 raised to -308-308
3.4*10 raised to -4932-4932
10
long[integer]----------------------------------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.



C++ 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 Enclave
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:-

Quote:
Code:
#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.

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



C++ - Simple Programming examples


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




SIMPLE IF STATEMENT



The general form of simple if else statement as already written above is:-

Quote:
Code:
if(Test Condition)
{
<Program Statement>
}
statement;

For e.g.:-

Code:
if
Code:
(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:-

Quote:
Code:
[SIZE=2][FONT=arial]if(Test Condition)
{
<Program Statement>
}
else
{
<Program Statement>
}[/FONT][/SIZE]
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:
Code:
[CODE]If (code<0)
            Cout<<”Code is negative number”;

else

Cout<<”Code is positive number”;[/CODE]

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

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

Quote:
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:
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();
    }

ADDITION AND SUBSTRACTION IN IF ELSE


As the topic state it is very clear for one to understand that how to add and sub. Using if else. Now most of u will be thinking that this can be done buy simple programming then why to use if else. Ya your thinking is absolutely correct we don’t have to use if else while adding but imagine u are subtracting using simple programming and a user entered values as follows:-

You’re coding for Subtraction using simple programming:-

C= (a-B);

Suppose user entered a=10 and b= 8 then answer will come 2. But if user entered a=8 b=10 then answer will come -2. So to come out from this -2 mistake[Actually it is not a mistake but we have to get positive value] we use if else so the coding goes as follows:-

Code:
Code:
#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a,b,c;

cout<<”Enter the value of a and b\n”;
cin>>a>>b;

if(a>b)
c=(a-b);
else
c=(b-a);

cout<<”The Subtract of A and B is\t”<<c;


getch();
}
So that’s the end of if else hope u understand it plz guys if u have any confusion just ask, I am here to solve your problem .
Remind u again download the attached Program files.


POST & PRE OPERATOR


An operator that require only one operand or data item are called unary operators. C++ supports unary minus(-), ++ (Increment), -- (Decrement) on airthmatic operands.
In C++ the other unary operator are ++ (Increment), -- (Decrement). These operator can be used before or after the variable, to give condition type increment and decerement.


INCREMENT OPERATOR

++ Increases the value by one. If it is used before or after the variable.....

Code:
i++ means i=i+1;
++i means i+1=i;


Both the formula are very diff and can the whole output, i=i+1 means that incrementation is done after the value is called. But i+1=i; means incrementation is done before the value is called.
For e.g. :-

Code:
Code:
int i;
i=10;
i=i++;
cout<<i;
Here first the value is incremented then the value is printed and the output will come 11. But if the same program is written in a way like this:-

Code:
Code:
int i;
i=10;
cout<<i++;


Then the output will be 10 no incrementation will be done beacuse as I already told i++ means i=i+1
so the compiler will first print the value of i then it will increament.

Also the same program with diff result:--
Code:
Code:
int i;
i=10;
cout<<++i;

Here the output will be 11 incrementation will be done beacuse as I already told ++i means i+1=i
so the compiler will first increament the value then it will print the value of i.
Note:- The following topic is important if U have any problem then please ask...

For e.g: -
Code:
Code:
int i,j;
i=10;
j=i++;
cout<<i<<" "<<j;
Here the output will be 10 11 . Here the value of i is assigned to j then in the value of i there is post incrementation of 1 so the final value of i remains same and j is 11.

MULTIPlE INCREMENTS


SO after simple increment and decrement move into the second chamber of complicated increments this type of increment are rarely used in programs but they can be asked in the outputs so as a guider I have to tell everything .
These type of increments and decrements are used in same line and with same integar or diff integar..

For e.g. :-----
Code:
Code:
void main()
{
clrscr();
int x;
x=10;
cout<<x++<<" "<<++x;
getch();
}
In the above program there are 2 output in the same line and the integer is same so the output will go from left to right but the assigning of numbers will be from right to left..

As the initial value of x is 10 and for assigning heading from right the first thing is ++x means x+1=x so the value become 11. Then there is x++ means x=x+1; so the comp will 1'st print the value then it will increment it so the value remains same as 11.The final output is 11 11.

I am not going into deep of this because this is not too much important as far as I know. SO lets start the loop.

LOOPS /REPETITIONS/ITERATIONS


A loop can be defined as the repition of the statements until the condition got fulfilled.Looping is consisted of statements that are executed until some condition for termination of a loop.

A program loop normally cosists of 2 segment:-

Control Statement
Body of the loop

The control statement test the condition and then direct the repeted execution of the statement contained in the body of the loop.
The body of the loop consists of the program statement that are given by the programmer....

The looping process in general performs the following 4 steps:---

Setting and Initialization of the counter.
Execution of the statement in the loop.
Checking of the condition to run the loop for specific period of time.
Incrementing the counter.


The C++ provides three loop constructs for performing loop operations. They are:

The while statement.
The do while statement.
The for statement.


THE WHILE STATEMENT


The simplest of all the loop :D. This looping structure is known as while loop, the basic format of while loop in C++ language....
Code:
Code:
while(test condition)
{
//Body of the loop
program statement
}

The following points should be remembered while using while loop :---

(1)The loop will not get executed if the given condition becomes false.
(1)The loop will be executed till the condition is false the result will be presented as soon as the condition becomes false.
There must be some looping termination inside the loop to avoid abnormal output..

Here are some e.g. To clear ur doubts........
Note:-- Plz download the attached documents to know better programming through while loop and all :D

Program to show no. From 1-100 using while loop..

Code:
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
i=1;
while(1<=100)
{
cout<<i<<",";
i++;
}
getch();
}


Here the condition is that the loop should run till the value of i becomes 100 as soon as it becomes 100 the loop ends and the output will be shown.
Firstly the value is 1 then the condition is fulfiled that 1 is less than 100 prog. Will go inside the loop the 1'st statement is cout<<i the initial value of i is 1 so 1 will be the 1'st output then acc/que the second value should be 2 then 3 and so on upto 100 so we have to write i++ so there will be an increment of 1 in the value of i and the loop goes upto 100......:)

Program to add value upto 100..


I am directly starting from void main() so dont get confused ....:D

Code:
Code:
void main()
{
clrscr();
int i,s;
i=1;
s=0;
while(i<=100)
{
s=s+i;
i++;
}
cout<<"Sum =" <<" "<<s;
getch();
}

Here I had taken 2 variables i for taking the values from 1-100 and sum for adding those values... I had asssigned the value of s=0 because 0 is an additive properties of add.. If u will add some no.. to 0 the no. Remains same ....Similarly 1 is the multiplicative prop. Of Multi..:D:D . Here first the value of i is 1 then we add the value into s. The initila value of s is 0 so after add.. the value become 1 then i got incremented again loop will run and again there will be addition. And the vaue of s was 1 after adding 2 it becomes 3 ad the loop goes on till the value of i becomes 100...

To find out the factorial of any no..

Code:
Code:
void main()
{
int n,f;
f=1;
cout<<"Enter the value of i to find the factorial\n";
cin>>n;
while(n!=0) or (n>0)or (n>=1)
{
f=f*i;
i--;
}
cout<<"Factorial ="<<" " <<f;
getch();
}


Here the user will in enter the no.. and we have to find the factorial. For ur info.. telling as most of u had forgotten what is factorial, the number resulting from multiplying a whole number by every whole number between itself and 1 inclusive.

For e.g. :- Fact. Of 5 :--- 5*4*3*2*1= 120. :D
So to find the factotorial the prdecessor no. Includin the no should be mulitiplied... To find this i had taken a variable in which the entered value will be stored. And variable f to hold fac. Value.. First I had given the value of f 1 as i had told 1 is the multiplicative prop. Of Multi..:D:D.. SO multiplying an no with 1 will give same no.. Suppose user enters th value 5 first the condition will be checked. I had given 3 cond. All 3 are correct but u had to use any one of em.. So prog. Will enter the loop then the no will be multi . With f and the value of n will get decremented again it will get multi. Till the condition becomes false . And finally the output wil be given



DO WHILE STATEMENT


The do while loop is the 2'nd type of loop, the main positive aspect of thios loop that whether the condition is true or false the loop will run one time, if the condition will be true then the loop will be continued otherwise it will stop..

The syntax of the do while statement is as follows:-

Code:
Code:
do
{
<program statement>
}
while(<condition>);
Here the condition is at the last so the loop will run 1'st time till reaching the condition....

The followind point should be remembered while using do-while loop:---

(1)It is executed at least once..
(1)It is executed till the condition remains trueand the control comes out as soon as the loop ends.
(1)There must be some looping terminating condition otherwise the loop willnot stop...

Here are some e.g. To clear ur doubts........

To check whether the number is Armstrong or not....

Note:- An armstrong no. Is a no. Whose digit cube sum is equal to the same no..

Code:
Code:
void main()
{
clrscr();
int n,k,r.s;
cout<<"Enter the number\n";
cin>>n;
k=n;
s=0;
do
{
r=n%10;
s=s+r*r*r;
n=n/10;
}while(n!=0);
if(k==s)
cout<<"It is Armstrong no.";
else
cout<<"It is not Armstrong No..";
getch();
}


In the above program I had taken 4 variables n for storing the number, k for storing the value of n so that we can check the condition,s for adding,r for taking remainder or storing every digit of the entered number...
So suppose u had entered the no 153 the no. Gets stored in k so n and k both will hold 153. Now the loop will begins first acc/cond given no will get divided be 10 and the remainder will get stored in r. First when u will divide 153 by 10 the remainder will come 3 the in second line cube of 3 i.e 27 will be added to s i.e 0 so s is now 27 then the remaining part will again got divided and the cube of 5 i.e 125 then again the last no cube i.e 1 will be added so the sum of 27+125+1=153 so the no. Is armstrong no..

To find factorial of any number.

Code:
Code:
void main()
{
clrscr();
int n,f;
cout<<"Enter the number\n";
cin>>n;
f=1;
do
{
f=f*n;
n=n-1;
}while(n!=0);
getch();
}
I have already explained it so its should be clear to all...:D




FOR STATEMENT

I dont know why but this is the only loop which i finds the best. This loop is an entry controlled loop means condition should be fulfiled to enter it.The syntax of this loop is as follows:--

Code:
Code:
for(initialisation;condition;increment/decrement)
{
<program statement>;
}
Initialisation Expression:-

It is executed only when the loop 1'st start. It provides loop variable an initial value.

Test Expression:-

It involves relation operator. It is executed every time through the loop before the body of the loop is executed . If the test expression is true the loop wil go on otherwise the loop wil end.

Increment/Decrement (re-initialising) expression:-

It os always executed at the end of the loop after the body of the loop.
Here are some e.g. To help u:D

For the following output:

*
* *
* * *
* * * *
* * * * *

Code:
Code:
void main()
{
clrscr();
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<"*"<<" ";
cout<<"\n";
}
}
getch();
}
For finding out the factorial of any number.

Code:
Code:
void main()
{
clrscr();
int i,f,n;
cout<<"Enter the number\n";,
cin>>n;
f=1;
for(i=n;i>=1;i++)
{
f=f*i;
}
cout<<"The factorial is =\t"<<f;
getch();
}
For displaying the table of any number..


Code:
Code:
void main()
{
clrscr();
int i,n,t;
cout<<"Enter the number\n";
cin>>n;
for(i=1;i<=10;i++)
{
t=i*n;
cout<<t;
cout<<"\n";
}
getch();

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
source :dearest darkstar
 
Last edited:

Nerevarine

Incarnate
worthy effort, one suggestion, cut paste the code with appororpriate formatting using the
Code:
 tag like this

[CODE]Bla bla
 

krishnandu.sarkar

Simply a DIGITian
Staff member
I think, it'd be better if you format it right way. Like using the CODE blocks and bold the headlines and the questions etc.
 

Vyom

The Power of x480
Staff member
Admin
Worthy effort yea.
But I have only one qualm. The code OP is teaching is not ANSI compliant. Straight out of Indian books. It would have been a lot of good for others as well as OP (and for me apparently) if he learned and wrote about ANSI C++.

As much as I appreciate the efforts, I can't appreciate the fact that this thread may only be good for students studying in schools and just to pass in exams.
 

aaruni

The Linux Guy
Nice guide, although there are formatting problems, a 404 error for a linked image, and might have some code errors too (only glimpsed through the code, the last code doesn't close the main() properly), and the intro is repeated twice(once on top, once at the very bottom).

As already suggested, use
Code:
This sort of a thing to write your code in

- - - Updated - - -

Worthy effort yea.
But I have only one qualm. The code OP is teaching is not ANSI compliant. Straight out of Indian books. It would have been a lot of good for others as well as OP (and for me apparently) if he learned and wrote about ANSI C++.

As much as I appreciate the efforts, I can't appreciate the fact that this thread may only be good for students studying in schools and just to pass in exams.

Yeah, that too.

Check this gallery

C++ - Minus
 

Flash

Lost in speed
@OP: Is that your alias is Darkstar in this forum?

C++ LEARNING GUIDE - Guides
C++ LEARNING GUIDE - Guides
C++ LEARNING GUIDE - Guides
C++ LEARNING GUIDE - Guides
 
OP
TechnoBOY

TechnoBOY

Padawan
Nice guide, although there are formatting problems, a 404 error for a linked image, and might have some code errors too (only glimpsed through the code, the last code doesn't close the main() properly), and the intro is repeated twice(once on top, once at the very bottom).

As already suggested, use
Code:
This sort of a thing to write your code in

404 is fixe [new table ]
 

krishnandu.sarkar

Simply a DIGITian
Staff member
Nice. But ignore Yellow. It's not a good color for eyes on this theme :p

- - - Updated - - -

@OP: Is that your alias is Darkstar in this forum?

C++ LEARNING GUIDE - Guides
C++ LEARNING GUIDE - Guides
C++ LEARNING GUIDE - Guides
C++ LEARNING GUIDE - Guides

Also if you are not the same person, put those links in your post and give the proper credit to the original writer.
 

ico

Super Moderator
Staff member
[MENTION=285910]TechnoBOY[/MENTION]

I really appreciate the effort which you put in this thread. :) But I think, nobody can appreciate the non-standard code because it doesn't really teach you the true C++. Practically, it is useless.

The code in this thread will only run on Turbo C++ which is a 25 year old compiler and has no practical use. Indian education system is hence doomed. We don't have things like conio.h anymore. getch() doesn't make sense for programs which are to be run on console.

You don't touch upon namespaces which are important to understand. cin and cout are inside the std namespace. void main() isn't something which should be used.

Then you talk about multiple increment statements in a single cout statement. This behaviour is undefined. Different compilers can give different results and all are correct. g++ and Clang have different results.

Code:
[gagan@cozmo3 ~]$ [B]g++[/B] test.cpp 
[gagan@cozmo3 ~]$ ./a.out 
[B]11 12[/B]
[gagan@cozmo3 ~]$ [B]clang++[/B] test.cpp 
[gagan@cozmo3 ~]$ ./a.out 
[B]10 12[/B]

Code:
#include <iostream>

int main()
	{
	int i=10;
	std::cout<<i++<<" "<<++i;
	return 0;
	}

I think you should buy this book C++ Primer 5th Edition - Stanley B. Lippman. Study from it and update the thread. :)
 

ankush28

Bazinga
*www.techenclave.com/community/threads/tutorial-learn-all-the-basics-of-c-language.26790/

COPY PASTE?
*i.imgur.com/xqhvNBc.png
 
Last edited:

Flash

Lost in speed
*www.techenclave.com/community/threads/tutorial-learn-all-the-basics-of-c-language.26790/

COPY PASTE?
*i.imgur.com/xqhvNBc.png
I've also linked another forum, which has the same content by the member 'Darkstar', and asked here whether OP and darkstar are same guy?
But [MENTION=285910]TechnoBOY[/MENTION] didn't answer the question..
 

ico

Super Moderator
Staff member
[MENTION=285910]TechnoBOY[/MENTION]

Plagiarism will not be tolerated.

Consider this your final and last warning.

We have this thread here as well. *www.digit.in/forum/programming/52597-tutorial-learn-all-basics-c-language.html
 
Status
Not open for further replies.
Top Bottom