Professional Documents
Culture Documents
What Is The Difference Between Defining and Declaring A Variable
What Is The Difference Between Defining and Declaring A Variable
The basic types of variable in C are namely char, int, double and float. First the
variables have to be declared. This is done as follows:
First any of the data type defined above must be mentioned and this is followed by the
name of the variable. For instance if wants to declare a variable v1 of type char it is
done as follows
char v1;
While declaring a variable what happens is the data type is described to the compiler
and no space or memory allocation takes place. In other words just the data types are
mentioned without any memory storage for the data types. While defining the variable
the declaring the data type of the variable along with space allocation for the variable
takes place.
Declaring a variable means describing its type to the compiler but not allocating any
space for it. Defining a variable means declaring it and also allocating space to hold the
variable. You can also initialize a variable at the time it is defined.
In declaration we just mention the type of the variable and it's name. We do not initialize
it. But defining means declaration + initialization.
e.g String s; is just a declaration while String s = new String ("abcd"); Or String s =
"abcd"; are both definitions.
int x;
int x = 10;
void foo() {
System.out.println("Hello");
}
In interfaces you can define variables (but they are really all constants). In interfaces
you can only declare methods, but not define them.
In abstract classes, some methods may be only declared and others declared and
defined..
By using declaration we can the name and type of a variable.But it does not allocate
space.
By using definition we can allocate space for the variable.
When we declare a variable we give only the name and data type for that variable but
when we define a variable we give the name datatype for that variable and also give a
particular value to that variable. For exemple in C language:
int a;//declaration
int a=10;//intialisation
int a;
a=10;//defnition
Class: Class is a successor of Structure. By default all the members inside the class are
private.
Asked by: Guest on July 05, 2005 Last Updated: March 11, 2011
1Share
Subscribe
There should be no confusions. The only difference between a structure and a class is
that all members in a class are private by default whereas they are public in a
structure.
Above answer was rated as good by the following members:
OSaienni, vjy_2107, senthilganesh1988, ravisankarvn, veejaynaikar
Sponsored Links
July 05, 2005 00:42:34 #1 .
girish
structure member are public in default and class members are privare in default.
1:By default the members of structures are public while that for class is private
2: strutures doesn't provide something like data hiding which is provided by the classes
3: structures contains only data while class bind both data and member functions
Is this answer useful? Yes | No Overall Rating: -6
0 6
In a Structure all the data types are public but in class they are private.
In a Structure we can't initilse the value to the variable but in class variable we assign the
values.
There should be no confusions. The only difference between a structure and a class is
that all members in a class are private by default whereas they are public in a structure.
#include <iostream>
using namespace std;
struct Whatever
{
virtual void foo() { }
virtual ~Whatever() { }
};
private:
int value; // private data member
};
int main()
{
Whatever *pW = new Derived(10);
pW->foo(); // virtual call
delete pW;
return 0;
}