Saturday, 4 June 2016

Constructors And Destructor In C++


The automatic initialization as soon as the object is created is called as constructor.
We should know the concept of access specifiers here. Whenever we create a class, we put variables under public, private, protected etc.
Now the variables which comes under the Private are known as Member Variables while the variables which comes under the Private section are known as Member Function. The Constructors are always declared in the public section of the program. The prime work of the constructor is to initialize Member Variable from the PRIVATE class.
The constructor has the same name as the class name.
Destructor is the anti of Constructor. If object is born when constructor is created, it dies when destructor is called. Destructor has also the same name as the class name except for a trend that it is preceded by ~ symbol.
Program:
#include<iostream.h>
#include<conio.h>
class ratio
{
private:
int num,den;
public: \\Access specifier
ratio() \\Constructor
{
cout<<“Object is born\n\n”;
}
~ratio() \\Destructor
{
cout<<“Object dies\n\n”;
}
};
void main()
{
clrscr();
{
ratio x; \\This is how object is created i.e Class name then Object name.
Also constructor is called here because object is created here(See the text above)
cout<<“Now x is alive\n\n”;
}
cout<<“Now between the blocks\n\n”; \\you need to be careful with blocks. Blocks are nothing but the curly brackets inside which the program is written.
{
ratio y; \\Another object created. Again the constructor will be called 
cout<<“Now y is alive\n\n”;
}
getch();
}

Functions in C++


What are Functions?

Now functions are modules or its like a part of a device which performs its work indivually. All parts of the computer like monitor or mouse or speakers can be seen as small small modules which together makes a computer. They can work indiviually as well. Speakers can work independently. This example is to show you guys that function in C++ is just a part of program which does it work when called.

Why do we use Function?

To modularise the program. Segmenting the program into small modules so that it can be easy to debug or update easily. If there’s a problem in your speaker you dont change you entire PC, you just repair or replace the speaker. Likewise, here too if there’s a problem in any segment of the program you just correct the function. The key point is functions helps in modularising the program.

How do we use it?

In C++, function is anything after which () is written. Even after void main(), we use the symbol (). () indicates that it is function. Functions can be named anything(Except keywords). However, there is a return type with every function. Return type is what does you function return to the program(Like your speaker plays songs). Data type can be int,float,double,void etc.

Programs on Functions are given on the ALL PROGRAMS option or you can search it. Detailed explanation is provided at each step. Comment below to ask any question or anything that might be useful to your knowlege.

Wednesday, 25 May 2016

THIS IS REGUARDING THE QUARIES WHICH MAY COME UP

HELLO GUYS THIS BLOG CREATED BY ME IS TO PROVIDE FREE EDUCATION TO ALL THE STUDENTS WHO FEEL THAT COMPUTER SCIENCE IS NOT THEIR CUP OF TEA...

MY AIM IS TO SOLVE YOUR QUARIES AND HELP YOU IN ANY WAY POSSIBLE.

COMMENT YOU DOUBTS YOUR QUARIES OR ANY QUESTION WHICH YOU WANT TO SOLVE OR CLARIFY.....

ASK QUESTIONS DOUBTS QUARIES OR DEMAND ANY ASSISTANCE........I'LL BE THERE FOR YOUR HELP.

A PROGRAM MAY BE CREATED IN MANY DIFFERENT WAYS i.e BY USING CLASS,FUNCTIONS, THROUGH CONSTRUCTORS OR ANY OTHER THING...THEREFORE IF YOU FEEL THAT YOU NEED SOME SORT OF ASSISTANCE BE FREE TO COMMENT IT BELOW....




THANKS.

FIBONACCI SERIES - HSC FAVORITE QUESTION

NOW WHAT IS FIBONACCI SERIES....

0,1,1,2,3,5,8,13........

followed the trend???Noo??

A series of number in which each number is the sum of preceding two numbers is known as Fibonacci series.
This is a very important and the most asked question of HSC.


#include<iostream.h>

#include<conio.h>



void main()

{

clrscr(); \\clears the previous output



int a=0, b=1, n, c;  \\first two numbers of fibonacci series is 0 and 1

cout<<"Enter the number of terms till you want fibonacci series";

cin>>n; \\User choice, till wherever he wants to print the series

cout<<a<<"\t"<<b;



for(int i=1; i<=(n-2);i++)           \\ you must be thinking why (n-2), its because first two terms i.e 0                                                            and 1  are already included

{

cout<<a<<"\t"<<b<<"\t";       \\it'll print 0 and 1 which is first two numbers

c=a+b;                \\now as we go by definition fibonacci series is evaluated by adding the
                                                                                        previous two numbers....here that number is c

cout<<c;

b=c; \\now upgrade the values of variable.... think yourself y

a=b;

}

getch();

}




TRAVERSING IN AN ARRAY- LINEAR SEARCH EXAMPLE

TRAVERSING AN ARRAY SIMPLY MEANS TO GO OVER THE ENTIRE DATA AT LEAST ONCE. SUPPOSE YOU ARE SEARCHING FOR A CHAPTER IN TEXTBOOK.....WHAT YOU DO IS GO TO INDEX AND SEARCH FOR THE CHAPTER.....SUPPOSE YOU FOUND THE CHAPTER AT 5TH ENTRY YOU'LL NOT GO FURTHER.......
HOWEVER THIS ISN'T THE CASE OF TRAVERSING. IN TRAVERSING ALL THE ENTRIES ARE CHECKED EVEN THOUGH YOU DON'T WANT THE REST....
THIS IS HOW THE CONCEPT OF TRAVERSING WORKS...................


NOW A QUICK PROGRAM FOR LINEAR SEARCH A NUMBER.


#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int arr[10]; \\THIS WILL CREATE AN ARRAY VARIABLE 'arr' containing 10 digits in

data type integer

int search;

for(int a=0;a<10;a++) \\for loop for accepting 10 numbers from the user

{

cout<<"Enter the"<<(a+1)<<" numer \t"; \\here (a+1) means 1

cin>>arr[a];

}

cout<<"Enter the number to be searched";

cin>>search;

for(int i=0;i<10;i++)

{

if(arr[i]==search) \\ in c++ == means equals to
{

cout<<"The number is"<<arr[i]<<"found at"<<(i+1)<<"th position";

                    \\here we used (i+1)th position because the index of the array starts from 0

}

else

{

cout<<"The number you are searching is not found";

}

getch();

}

}

USING IF-ELSE-IF Ladder STATEMENT

//THIS PROGRAM ILLUSTRATES THE USE OF IF STATEMENTS IN C++(USEFUL IN HSC)

NOW IF STATEMENT IS A CONDITIONAL STATEMENT IN C++ i.e WHERE YOU WANT TO MAKE ANY CHOICE IN RUN TIME(i.e DURING EXECUTION OF PROGRAM).

LETS SEE AN EASY EXAMPLE TO COMPARE THREE NUMBER ENTERED BY THE USER. FINDING OUT WHICH ONE IS LARGEST AMONG THEM


#include<iostream.h>

#include<conio.h>

 void main()

{

clrscr();

int a,b,c;                      \\this is variable declartion

cout<<"Enter three numbers";     \\Print the content as it is on output screen

cin>>a;

cin>>b;

cin>>c;              \\you can also take input in one step as cin>>a>>b>>c;



if(a>b && a>c)       \\if statement... Now here we have 2 condition to check i.e

                       \\ a>b and a>c. In c++ and is written as two emphasand symbol.

{

cout<<a<<"Is the largest number"; \\be clever and think like a programmer

}

else if(b>a && b>c)

{

cout<<b<<"Is the largest number";

}

else

{

cout<<c<<"Is the largest number";

}

getch();

}

PROGRAM ON ARRAY

\\An array is a set of similar data types having contagious memory location.


#include<iostream.h>

#include<conio.h>

void main()

{

clrscr(); \\To clear previous output

int arr[10]; \\declaration of array of data type int

int a,sum=0;

cout<<"Enter 10 numbers to print the sum";


for(a=0;a<10;a++)

{

cin>>arr[a];

sum=sum+arr[a];

}

cout<<sum;

getch();


}



PROGRAM ON FOR LOOP

#include<iostream.h>

#include<conio.h>

void main()

        {


clrscr();   \\this will clear the previous output generated

int a; \\this is called as variable declaration

int sum = 0; \\this is variable  initialization

for(a=0 ; a<=10; a++) \\the general syntax of for loop is for(variable intialization; condition;                                                    increment/decrement operator)

{

sum=sum+a;

}

cout<<sum; \\this will print the value of variable sum

getch();

}

PROGRAM TO SWAP NUMBERS

#include<iostream.h>

#include<conio.h>


void main()

{

clrscr();                        \\this will clear the previous output generated

int a,b;                           \\ this is know as variable declaration. 'int' is a data type(a keyword)

int c;                                               \\another variable declaration

cout<<"Enter the value of a";   \\this command will generate the message as it is on the screen

cin>>a;         \\this is taking input from the user

cout<<"Enter the value of b"; \\this command will generate the message as it is on the screen

cin>>b;          \\this is taking input from the user



cout<<"The entered value of a and b is";

cout<<a<<"\t"<<b;                          

c=b;                                                  \\we introduced a variable 'c' to copy contents of b to c

b=a;                                                    then a to b......now value of a is transferred to b

a=c;                                                   then again c to a(c contains value of b)

                                                        \\ in short we used another variable (c) to exchange contens.

cout<<"The new value after swap is ";

cout<<a<<"\t"<<b;

getch();                                              

}

C++ : MY FIRST PROGRAM (Basics)

#include<iostream.h>

#include<conio.h>


void main()

{

clrscr();

cout<<"Hello There! This is my first Program";

getch();


}




This is the most basic program which will print a simple output that you entered through the text within the program...