In programming, a declaration is a statement that informs the compiler or linker of the existence of a new entity. This entity can be a variable, a function, a class, a user-defined type, or any other member that can be used in the code.ù
In programming, a definition is a statement that reserves memory space for a variable or function/method with or without arguments. Some types of definitions include the body of the function or member while others define only in one line such as static variables.
declarations are usually done inside the class and definitions are done in the class source file
Let's imagine we've just created a class with these inherited members as public
class MyClass
{
public:
int x;
static int y;
void HelloWorld();
int AddInt(int a,int b);
};Here we have created a class and declared some members, some members need to be defined before they can be used
the first member we declared, that is, the variable
int x;it does not need to be defined externally because being non-static it is a variable shared in all instances of the class, in reality if you assign it a value or initialize it the declaration will also act as a definition automatically
int x = 0;Here the variable is also defined by assigning it an initial value
the second member we declared, that is a static variable
static int y;The code to define a static variable is as follows: (this is an example for any variable)
VarType ClassName::VarName;so now to define our variable we need to write the code to define it just below the #include directives in the source file:
int MyClass::x;with this code our static variable x is defined and a space in memory is reserved and therefore there will be only one copy of that variable among all the objects of the class
in addition to defining the variable, it is also possible to initialize it by assigning it an initial value, here is the example:
int MyClass::x = 0;this way when you go to run the application and go to use the variable it will have that initial value until it is modified somewhere
The important thing is that the code to define a static variable does not require a body like methods.
the third member we declared, that is a void method
void HelloWorld();The code to define a method member is as follows with the body:
void MyClass::HelloWorld()
{
}and inside this body we can add all the operations that the method will have to perform when it is called (being a non-static method, it will be necessary to first declare an object to the class before calling it)
Example:
MyClass obj;
obj.HelloWorld(); //here the function is calledthe fourth member we declared, that is a int method
int AddInt(int a,int b);The code to define a method member is as follows with the body and arguements:
int MyClass::AddInt(int a, int b)
{
return a + b;
}in this way when we go to call the method we will also have to fill the arguments which in this case are integers
MyClass obj;
cout << obj.AddInt(1, 1);This code will print the sum of the two numbers entered as two arguments in the function
Always remember:
It must be remembered that when in front of any member that has () at the end it is not a variable but a method that will require in the definition the body with the arguments if they are inside the brackets
If you declare a method or variable in a C++ class, but don't define it, the compiler or linker will give you an error at compile time or link time. This is known as an “unresolved external symbol” error.
For methods, if you declare a method (i.e., specify its prototype) but don't provide a definition (i.e., the method body), the compiler will give you an error saying it can't find the method definition. This is because the compiler needs to know what to do when the method is called.