Assignment 1) What is the difference between a regular array declarations and a
ID: 658731 • Letter: A
Question
Assignment
1) What is the difference between a regular array declarations and a static array declaration ?
Code one of each and print out the contents to verify your explanation is correct
2) What is the difference between a regular class data member and a class static data member.
Code several each type and print out the contents to verify your explanation is correct.
3) What is the difference between a regular class function and static class function.
Code several of each type and use then and print out the results to verify your explanation is correct.
4) What is the difference between a regular local variable declarations and a static local variable declaration ?
Code one of each and print out the contents to verify your explanation is correct
5) What is bad about extern or global variables
Explanation / Answer
1)
Important Note:
static is keyword which has special meaning in a c++
program. Storage of static variables is once in a program
execution and it is accessible to the static functions throughout
the program in static functions.
static keyword is used in the following cases
Static variable in functions
Static Class Objects
Static member Variable in class
Static Methods in class
When it comes to declaration of normal array and static array.
There is no array like static array, there is only static variable
concept and static method concepts.
To distinguish arrays , there are two types static arrays
(it doesnot mean array declaration start with static)
and dynamic array.
Static arrays are fixed type of memory size.
Dynamic arrays are variable size.
For example,
int arr[5]; //It is fixed size array and it is called as static
//No static keyword is required to declare fixed size arrray
Dynamic arrray creation:
Type 1: Creates an array of usersize using malloc function
int *ptr=(int*)malloc(usersize*sizeof(int));
Type 2: Creates an array of usersize using new keyword
int *ptr=new int[usersize]; //Dynamic array using new keyword
--------------------------------------------------------------------------------------------------------------------------------
2)
Difference between regular class variable and static class variable
Regular class variable is accessed by the object of the class
A static variable declared in the class do not need object of the class to access.
//sample proram for non static variable
#include<iostream>
using namespace std;
class AClass
{
private :
int value;
public:
void counter()
{
//local variable to the class
value=0;
cout<<"value : "<<value<<endl;
//value gets incremented by one stored in a stack
//Value is not accesseble since the value stored in stack
//gets released it content.
value=value+1;
}
};
int main()
{
//create an AClass object
AClass aobject;
for(int i=0;i<5;i++)
aobject.counter();
system("pause");
return 0;
}
output:
value : 0
value : 0
value : 0
value : 0
value : 0
//sample program for static variable
#include<iostream>
using namespace std;
class AClass
{
private :
//storage of static variables is on heap memory (free stograge space)
static int value;
public:
static void counter()
{
cout<<"value : "<<value<<endl;
//value gets incremented by one stored in a stack
//Value is accesseble since the value stored in heap
value=value+1;
}
};
//Direct access of static variable method using :: scope resolution operator
int AClass::value=0;
int main()
{
//call method counter which is static that is accessbile to class name
//without class object
for(int i=0;i<5;i++)
AClass::counter();
system("pause");
return 0;
}
sample output
value : 0
value : 1
value : 2
value : 3
value : 4
--------------------------------------------------------------------------------------------------------------------------------
3)
Difference between regular class function and static class function
Regular class function can access class member variables
A static function only can access static type of variables
//sample proram for regular class function
#include<iostream>
using namespace std;
class AClass
{
private :
int value;
public:
//function of class
void counter()
{
//local variable can access in this method
value=0;
cout<<"value : "<<value<<endl;
//value gets incremented by one stored in a stack
//Value is not accesseble since the value stored in stack
//gets released it content.
value=value+1;
}
};
int main()
{
//create an AClass object
AClass aobject;
for(int i=0;i<5;i++)
aobject.counter();
system("pause");
return 0;
}
output:
value : 0
value : 0
value : 0
value : 0
value : 0
//sample proram for static function
#include<iostream>
using namespace std;
class AClass
{
private :
//static variable
static int value;
public:
//static method can only access static variables of class
static void counter()
{
cout<<"value : "<<value<<endl;
//value gets incremented by one stored in a heap
//Value is accesseble since the value stored in heap
value=value+1;
}
};
//Direct access of static variable method using :: scope resolution operator
int AClass::value=0;
int main()
{
//call method static counter method which is static that is accessbile to class name
//without class object
for(int i=0;i<5;i++)
AClass::counter();
system("pause");
return 0;
}
sample output
value : 0
value : 1
value : 2
value : 3
value : 4
--------------------------------------------------------------------------------------------------------------------------------
4)
Difference between static and non -static or local variable
Static variable once declared in the method that will be updated when the method is called.
Local variables will not updated and values not accessble to the outside of the class and
each time function calls gets reset to normal values.
//Example program to illustrate the local and static variables in two difference function
#include<iostream>
using namespace std;
void printStatic()
{
//Declaration of static will be updated each time the function
//printStatic is called
static int count=0;
cout <<"Count : "<<count<<endl;
//increment count value by one
count++;
}
void printNonStatic()
{
//Declaration of local variable is not update and will not accessbile
//to the outside of the function
int count=0;
cout <<"Count : "<<count<<endl;
//increment count value by one but it becomes zero each time the function
//printNonStatic
count++;
}
int main()
{
cout<<"Printing non-static values "<<endl;
for(int i=0;i<5;i++)
printNonStatic();
cout<<"Printing static values "<<endl;
for(int i=0;i<5;i++)
printStatic();
system("pause");
return 0;
}
sample output:
Printing non-static values
Count : 0
Count : 0
Count : 0
Count : 0
Count : 0
Printing static values
Count : 0
Count : 1
Count : 2
Count : 3
Count : 4
--------------------------------------------------------------------------------------------------------------------------------
5)
Difference between extern and global variable
Global variables are accessed throughout the program in a project.
extern variables are declared in a separate header files.
To use the extern variables, just include the header file where
this extern variable is declared in a same project or different project.
//sample program
//Header file save as Extern.h
//Extern.h
//Store a extern variable value=5
#ifndef EXTERN_H
#define EXTERN_H
//create a extern stoage class variable and set value as 5
extern int value=5;
#endif EXTERN_H
//Tester program
#include<iostream>
//To access extern variable declared in Extern.h header file
#include "Extern.h"
using namespace std;
void print();
//global variable which is accessible to any method in the current program
int globalValue=10;
int main()
{
cout<<"Accessing value in Extern.h header file in current program : ";
cout<<value<<endl;
//calling print to print both global and extern storage variable
print();
system("pause");
return 0;
}
void print()
{
cout<<"In print method"<<endl;
cout<<"Accessing global value in current program : ";
cout<<globalValue<<endl;
cout<<Accessing extern value in current program print method also: ";
cout<<value<<endl;
}
//sample output:
Accessing value in Extern.h header file in current program: 5
In print method
Accessing global value in current program: 10
Accessing extern value in current program print method also: 5
--------------------------------------------------------------------------------------------------------------------------------
6)
Register storage class
register declared variable generally automatic variables which are accessible
inside a function.
when variable is declared as register then the variable is stored in a available register.
Registers are high speed access than main memory.
Need of register variable:
To minimalize the number of cycles to access the values stored at given address.
The & is not used to the store values since register is not stored in main memory.
Direct address is required without storing in a main memory then the register variables
are used.
Register variables are suitable for hardware component programming or low level device programming.
--------------------------------------------------------------------------------------------------------------------------------
7)
Scope is the space in which a variable is declared that is accessible to the block or program.
There are two type of scope of a variables in c++ programs.
Local variables are only accessible to the block in which the variable is declared.
Outside of the block or method is not accessible.
Local variables declared in a function block.
example:
//Local variable example
#include<iostream>
using namespace std;
int main()
{
//local variable accessible to this main block only.
//Outside of the main block which is not accessible
int x=10;
cout<<"x : "<<x<<endl;
return 0;
}
Global declared variables are accessible throughout the program.
Global variable can be accessed in and around the program until exit from the program.
Global variables are declared at the top of the main function begins
//Local variable example
#include<iostream>
using namespace std;
//function prototype
void print();
//global variable accessible entire program
int x=10;
int main()
{
//Access x in a main method
cout<<"x : "<<x<<endl;
return 0;
}
//Function print that access the global variable x
void print()
{
//Access x in a print method
cout<<"x : "<<x<<endl;
}
Hope this helps you...