Friday, May 14, 2010

purchase

Avatar (Two-Disc Blu-ray/DVD Combo) [Blu-ray]

Wednesday, May 5, 2010

c , c++ , program and notes , inheritance, class object

Q1. progame in cpp for find the list of Fibonicci no. using recurision
/*Error Free programe*/
#include // header file


int fibonacci(int n) // function protoype
{
if(n==0) return 0;
else
if(n==1) return 1;
else
return fibonacci(n-1)+fibonacci(n-2); // using recurision
}

int main() //main programe
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cout<<
return 0;
}


input : 5
output is: 1 1 2 3 5

this is error free program tested by amit sharma

--------------------------------------------------------------------------------

Scope of variable
i take it from this website

http://www.exforsys.com.
for any problem i m not responsible

The scope and lifetime of variables in functions:

The scope and lifetime of the variables define in C is not same when compared to other languages. The scope and lifetime depends on the storage class of the variable in c language the variables can be any one of the four storage classes:

1. Automatic Variables
2. External variable
3. Static variable
4. Register variable.

The scope actually determines over which part or parts of the program the variable is available. The lifetime of the variable retains a given value. During the execution of the program. Variables can also be categorized as local or global. Local variables are the variables that are declared within that function and are accessible to all the functions in a program and they can be declared within a function or outside the function also.

Automatic variables:

Automatic variables are declared inside a particular function and they are created when the function is called and destroyed when the function exits. Automatic variables are local or private to a function in which they are defined by default all variable declared without any storage specification is automatic. The values of variable remains unchanged to the changes that may happen in other functions in the same program and by doing this no error occurs.

/* A program to illustrate the working of auto variables*/
#include
void main()
{
int m=1000;
function2();
printf(“%d\n”,m);
}

function1()
{
int m=10;
printf(“%d\n”,m);
}
function2()
{
int m=100;
function1();
printf(“%d\n”,m);
}


A local variable lives through out the whole program although it accessible only in the main. A program with two subprograms function1 and function2 with m as automatic variable and is initialized to 10,100,1000 in function 1 function2 and function3 respectively. When executes main calls function2 which in turns calls function1. When main is active m=1000. But when function2 is called, the main m is temporarily put on the shelf and the new local m=100 becomes active. Similarly when function1 is called both previous values of m are put on shelf and latest value (m=10) become active, a soon as it is done main (m=1000) takes over. The output clearly shows that value assigned to m in one function does not affect its value in the other function. The local value of m is destroyed when it leaves a function.

External variables:

Variables which are common to all functions and accessible by all functions of aprogram are internal variables. External variables can be declared outside a function.

Example

int sum;
float percentage;
main()
{
…..
…..
}
function2()
{
….
….
}



The variables sum and percentage are available for use in all the three functions main, function1, function2.  Local variables take precedence over global variables of the same name.

For example:

          //top of the main program they are declared with intial value also;
int i = 10;
void example(data)
int data;
{
int i = data;
}

main()
{
example(45);
}


In the above example both the global variable and local variable have the same name as i.
The local variable i take precedence over the global variable. Also the value that is stored in integer i is lost as soon as the function exits.

A global value can be used in any function all the functions in a program can access the global variable and change its value the subsequent functions get the new value of the global variable, it will be inconvenient to use a variable as global because of this factor every function can change the value of the variable on its own and it will be difficult to get back the original value of the variable if it is required.

Global variables are usually declared in the beginning of the main program ie., before the main program however c provides a facility to declare any variable as global this is possible by using the keyword storage class extern. Although a variable has been defined after many functions the external declaration of y inside the function informs the compiler that the variable y is integer type defined somewhere else in the program. The external declaration does not allocate storage space for the variables. In case of arrays the definition should include their size as well. When a variable is defined inside a function as extern it provides type information only for that function. If it has to be used in other functions then again it has to be re-declared in that function also.

Example:

void main()
{
int n;
out_put();
extern float salary[];
……
…..
out_put();
}

void out_put()
{
extern float salary[];
int n;
….
…..
}
float salary[size];


a function when its parameters and function body are specified this tells the compiler to allocate space for the function code and provides type info for the parameters. Since functions are external by default we declare them (in calling functions) without the qualifier extern.

Multi-file programs:

Programs need not essentially be limited into a single file, multi-file programs is also possible, all the files are linked later to form executable object code. This approach is very useful since any change in one file does not affect other files thus eliminating the need for recompilation of the entire program. To share a single variable in multiple programs it should be declared, as external variables that are shared by two or more files are obviously global variables and therefore we must declare them accordingly in one file and explicitly define them with extern in other file. The example shown below illustrates the use of extern declarations in multi-file programs

File1.c

main()
{
extern int j;
int k;
}

function1()
{
int z;

….
}
file2.c

function2()
{
int k;
}
function3()
{
int num;

….
}


the function in main file1 reference the variable j that is declared as global in file 2. Here function1() cannot access j if the statement extern int k is places before main then both the functions could refer to j. this can also be achieved by using extern int j statement inside each function in file1.

The extern specifier tells the compiler that the following variables types and names have already been declared elsewhere and no need to create storage space for them. It is the responsibility of the linker to resolve the reference problem. It is important to note that a multi-file global variable should be declared without extern in one of the files.

Static variables:

IT " RETAIN " THE OLD VALUE REMEMBER

The value given to a variable declared by using keyword static persistes until the end of the program.
A static variable is initialized only once, when the program is compiled. It is never initialized again. During the first call to stat in the example shown below x is incremented to 1. because x is static, this value persists and therefore the next call adds another 1 to x giving it a value of 2. The value of x becomes 3 when third call is made. If we had declared x as an auto then output would here been x=1 all the three times.

main()
{
int j;
for(j=1;j<3;j++)
stat();
}
stat();
{
static int x=0;
x=x+1;
printf(“x=%d\n”,x);
}


Register variables:(same like automatic variable)

A variable is usually stored in the memory but it is also possible to store a varible in the compilers register by defining it as register variable. The registers access is much faster than a memory access, keeping the frequently accessed variables in the register will make the execution of the program faster.

This is done as follows:

register int count;




Since only a few variables can be placed in a register, it is important to carefully select the variables for this purpose. However c will automatically convert register variables into normal variables once the limit is exceeded.



Inheritance :

  1. Single Inheritance:
  2. Multiple Inheritance:
  3. Multilevel Inheritance:
  4. Hybrid Inheritance:
=====================================================
Single Inheritance:
=======================================================
example:
-----------------------------
// use borlandc compiler

#include

/* c++ provides a third visiblility modifier protected
a limited purpose in inheritance. a member declared as protected is
accessible by the member functions within its class and any class
immediately derived from it. it cannot be accessed by the functions
outside these two classes.
multilevel inheritance */

class stud
{
protected:
int rollno;
public:
void getnum(int a)
{
rollno=a;
}
void putnum()
{
cout << "rollno= " << rollno;
}
};
class test : public stud
//first level derivation
{ protected:
float sub1;
float sub2;
public:
void getmarks(float a, float b)
{
sub1=a;
sub2=b;
}
void putmarks()
{
cout << "marks in first subjects" << sub1<<"\n";
cout << "marks in second subjects" <<< "\n";
}
};
class result : public test // second level derivation
{
float total;
public:
void printres(void);
};
void result :: printres()
{
total=sub1+sub2;
putnum();
putmarks();
cout << "total" << total << "\n";
}
int main()
{
result student1;
clrscr();
student1.getnum(111);
student1.getmarks(85,60);
student1.printres();
}
====================================================