Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

People say definition is used when the declared stuff has some memory allocated. If I declare a class

class fish
{
  char* name;
  int type;
  //someting i dont know about fish
}; 
int main()
{ 
   fish a;
   return 0;
}

Should it mean this is a declared fish or a defined fish ?

Let me try first!

it is called a defined fish!

I now leave a breapoint at //something..... (that is a valid declaration of something I just dont know) Do you know what is there that at //someting the debugger may never reach ? Does that also mean there is no memory allocation for it. I'll be deeply sad to hear a no.

share|improve this question

3 Answers 3

No, it's more like:

// declaration
class Fish {
  void swim();
}

// definition
void Fish::swim() {
  // some code for swimming
}

Then actually using an object of class Fish is called instantiation:

int main() {
  Fish nemo;
}

Because you create an instance of the Fish Class.

You first declare in the header, that something like a Fish exists. Then in the .cpp file you define how this Fish thing actually works. The memory allocation is not that directly linked to declaration vs definition in this case.

The line:

// something i dont know about fish

doesn't do anything at all, since the // marks it as a comment and the compiler will simply ignore it.

The debugger will never reach any part of a declaration at all. It's not code that gets executed at all. It only declares that there is something that can be used by other parts of the code.

share|improve this answer

In C++ (nearly verbatim from the standard),

  • A declaration is the introduction of a name specifying its interpretation and attributes (a declaration may also redeclare an already declared name)

  • Declarations are definitions, expected:

    • function declared without a body,
    • it uses extern or a linkage specification and neither an initializer nor a function body
    • it declares a static data member in a class declaration
    • it is a class name declaration, a typedef declaration, a using-declaration or a using-directive.

So in your example, all declarations are also definitions.

share|improve this answer

There are four concepts in the world of OOP:

  1. Definition (when you define the class)

    public class Human
    {
        private string firstName;
    
        public string FirstName { get; set; }
    }
    
  2. Declaration (when you declare that a variable is of the type of a class)

    Human saeed;
    
  3. Instantiation (when you allocate memory to your reference variable through using new keyword)

    saeed = new Human();
    
  4. Initialization (when you initialize the variable to its initial values, or default values)

    saeed.FirstName = "Saeed";
    

Now, sometimes you can declare, instantiate, and initialize a variable in one line of code:

Human saeed = new Human() { FirstName = "Saeed" };
share|improve this answer
3  
You might want to improve this answer : in C++ you can also declare a class by just giving it's name (forward declaration). –  Klaim Aug 9 '11 at 7:41
2  
Also, in C++, you can instantiate a class without using new. Your idea of "OOP" is "Java", and the two are not the same thing. –  DeadMG Aug 9 '11 at 9:28
    
@DeadMG, thanks for that. –  Saeed Neamati Aug 9 '11 at 9:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.