Anda di halaman 1dari 7

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.

What is the difference between defining and declaring a variable in java?

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.

The following is a variable declaration:

int x;

The following is a variable definition:

int x = 10;

The following are method declarations:

abstract void foo();


native void foo();

method definitions is when what the method does is defined:

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

What is the difference between declaring a variable and defining


a variable?
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.

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 num (this is declaration of variable)

int num=2 (this is defining of variable)


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.

int a;//declaration

int a=10;//intialisation

int a;
a=10;//defnition

What is the difference between "procedure" and "function"?

Go To First | Previous Question | Next Question


C++ | Question 4 of 230 Print

What is the difference between class and structure?


Structure: Initially (in C) a structure was used to bundle different type of data types
together to perform a particular functionality. But C++ extended the structure to
contain functions also. The major difference is that all declarations inside a structure
are by default public.

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

Total Answers and Comments: 15

Best Rated Answer



Submitted by: Gagan

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

Sorting Oldest First « Fir


Page 1 of 2 2> Last »
Options st 1

Sponsored Links
July 05, 2005 00:42:34 #1 .
girish

RE: What is the difference between class and structure?

structure member are public in default and class members are privare in default.

Is this answer useful? Yes | No

March 20, 2006 04:46:33 #2 .


Sharath

RE: What is the difference between class an...

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

November 17, 2006 00:17:33 #3 .


sunil yadav

RE: What is the difference between class an...

Structure dosen't support the polymorphism inheritance and initilization.

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.

Structuer is a collection of the different data type.

Is this answer useful? Yes | No Overall Rating: -5


0 5

February 05, 2007 12:04:22 #4 .


Gagan
RE: What is the difference between class an...

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.

Is this answer useful? Yes | No Overall Rating: +1


2 1

July 16, 2007 08:58:23 #5 .


Arun

RE: What is the difference between class an...

Try inheritance polymorphism overloading encapsulation etc.


all these are possible.........

1. struct data is public by default.. but its private in a class


2. class is successor of struct in heirarchy..
3. struct is ovrloaded.. in C++ its not same with class..

Is this answer useful? Yes | No Overall Rating: -1


0 1

October 22, 2007 14:46:43 #6 .


Sanjay Kapoor

RE: What is the difference between class an...


Originally posted by rajalekshmy+r
1)can we provide datahiding with structures? if yes how?
2)can structures suport run time polymorphism? If yes how? By writing code? Again
whatever you can do in a class you can do in a struct. Why not write code and see for
yourself?

#include <iostream>
using namespace std;

struct Whatever
{
virtual void foo() { }
virtual ~Whatever() { }
};

struct Derived : Whatever


{
void foo() { cout << "value=" << value; }
Derived(int n) : Whatever() value(n) { }

private:
int value; // private data member
};

int main()
{
Whatever *pW = new Derived(10);
pW->foo(); // virtual call
delete pW;
return 0;
}

Anda mungkin juga menyukai