Declarations and Definitions
Hi :)
The following text is from a book:
Quote:
Declarations and Definitions
Let’s digress for a moment to note a subtle distinction between the terms definition and declaration as applied to variables.
A declaration introduces a variable’s name (such as var1) into a program and specifies its type (such as int). However, if a declaration also sets aside memory for the variable, it is also called a definition. The statements
int var1;
int var2;
in the INTVARS program are definitions, as well as declarations, because they set aside memory for var1 and var2. We’ll be concerned mostly with declarations that are also definitions, but later on we’ll see various kinds of declarations that are not definitions.
I will tell you what I understand of definition and declaration. In the following code
Code:
struct POINT
{ float x, y; };
is a declaration because it doesn't set aside memory. It's just a blueprint. But is a definition because it sets aside memory space for the variables. Do I have it right? Please let me know. Thanks.
Code:
// adding two points p1, p2
#include <iostream>
#include <cstdlib>
using namespace std;
/////////////////////////////////////////////////////////
struct POINT
{ float x, y; };
/////////////////////////////////////////////////////////
int main()
{
POINT p1, p2, p3;
cout << "enter the x-coord of first point: "; cin >> p1.x;
cout << "enter the y-coord of first point: "; cin >> p1.y;
cout << endl;
cout << "enter the x-coord of second point: "; cin >> p2.x;
cout << "enter the y-coord of second point: "; cin >> p2.y;
cout << endl;
p3.x = p1.x + p2.x;
p3.y = p1.y + p2.y;
cout << "the third point is: " << p3.x << ", " << p3.y << endl;
cout << endl;
system("pause");
return 0;
}