IT Community - Software Programming, Web Development and Technical Support

Declaration and definition in C++.

This is a discussion on Declaration and definition in C++. within the C and C++ Programming forums, part of the Software Development category; Differentiate between declaration and definition in C++. A declaration introduces a name into the program; a definition provides a unique ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C and C++ Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 09-22-2007, 02:21 AM
vijayanand vijayanand is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 293
vijayanand is on a distinguished road
Default Declaration and definition in C++.

Differentiate between declaration and definition in C++.

A declaration introduces a name into the program; a definition provides a unique description of an entity (e.g. type, instance, and function). Declarations can be repeated in a given scope, it introduces a name in a given scope. There must be exactly one definition of every object, function or class used in a C++ program.
A declaration is a definition unless:
  • it declares a function without specifying its body,
  • it contains an extern specifier and no initializer or function body,
  • it is the declaration of a static class data member without a class definition,
  • it is a class name definition,
  • it is a typedef declaration.

A definition is a declaration unless:
  • it defines a static class data member,
  • it defines a non-inline member function.
__________________

J.Vijayanand
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-19-2008, 01:09 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default Re: Declaration and definition in C++.

Difference between declaration and Definition

Declaration
It just indicate the presence of a variable or memberfunction and ends with a semi-colon.

For eg;
int i;-->declaration
add();--->declaration

Definition
It explains the behaviour of the function.If it is a variable it defines its value.

For eg:
int i=2;--->It is definition of a variable
add()--------->Definition
{
int a=2,b=3,c;
c=a+b;
}
__________________
The KINGMAKER
Makes Every Thing Possible

Stuffs (My Blog)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-25-2008, 01:44 PM
ammulu ammulu is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 105
ammulu is on a distinguished road
Default Re: Declaration and definition in C++.

It is really simple to differentiate between a definition and declaration. Good examples you have provided, but a starter might really be confused at the beginning with these terms.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-06-2008, 11:50 PM
poornima poornima is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 189
poornima is on a distinguished road
Smile Re: Declaration and definition in C++.

Hai,
The above declaration is correct.

If u cant understand,refer this...

Consider a normal C++ program,

class a
{
add();->Function call
}
void a:add()->Function Definition
{
int c1;->declaration of a variable
cout<<"Enter a1";
cin>>a1;
cout<<"Enter b1";
cin>>b1;
c1=a1+b1;->Definition of a variable
cout<<c1;
}


This is incase of C++ Program..


If u use C,then
void main()
{
int c;->Declaration(Since u didnt assigned value)
c=add();->(Here c is defined(i.e..,Value is assigned through function)
printf("%d",c);
}
int add()
{
int a1,b1,c1;
printf("Enter two numbers");
scanf("%d%d",&a1,&b1);
c1=a1+b1;
return(c1);
}

In the above function the value returned by 'add()' is assigned to 'c'
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-03-2008, 03:31 AM
seesamjagan seesamjagan is offline
D-Web Programmer
 
Join Date: Aug 2007
Location: Chennai
Posts: 66
seesamjagan is on a distinguished road
Send a message via AIM to seesamjagan Send a message via Yahoo to seesamjagan
Thumbs down Re: Declaration and definition in C++.

hi poornima,
i think your definition is too complex.... i think kingmaker has done a clear job, its more than enough to explain the context
__________________
SeeSamJagan
- Sky is not the "LIMIT", Death is not the END, There is still something beyond....
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-03-2008, 05:08 AM
GDevakii GDevakii is offline
D-Web Sr.Programmer
 
Join Date: Aug 2007
Posts: 138
GDevakii is on a distinguished road
Smile Re: Declaration and definition in C++.

int i;
struct rat { public: int num; int denom; };
int inc(int count) { count = count + 1; }

However the following are only declarations.

extern int i;
struct rat;
extern int inc(int count);
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-14-2008, 05:58 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: Declaration and definition in C++.

declaration:
its mean the variables or functions have been just created, yet they are not ready for use.
example
int i;
int j;
void doAnything();

Definition

if we need to use those functions and variables we must initialize the variables or build the function. the process to initialize or building functions called definitions.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-06-2008, 10:38 PM
wainrob wainrob is offline
D-Web Trainee
 
Join Date: Jul 2008
Posts: 3
wainrob is on a distinguished road
Default Re: Declaration and definition in C++.

Thanks guys! I was really looking for this topic on the net, and thank God I found this thread. I had my confusions on how to declare the variables.

babyProducts-online / Great education Deals
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Gah... definition help!! vadivelanvaidyanathan Software Testing 0 01-23-2008 03:46 AM
Definition of Ciphers & types of Ciphers? H2o Other Web Programming Languages 1 07-22-2007 10:25 PM


All times are GMT -7. The time now is 06:04 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0