fellow programmers. I've seen it a common practice to put the class definition inside the global scope in a source file, above the main()
function I'm not worrying about the details of the class definition for now.
class Dog { ... };
int main() {
Dog dog1;
...
return 0;
}
Why should we do this? Is it a rule or just a common convention? Can we also put the class definition inside the main()
function? What are the advantages/disadvantages for doing this?
Consider the following two lines of code:
class Dog { ... } dog1;
int dog1;
To me it seems that class Dog { ... }
is just a data type just like int
is.
If I am going to use objects of some class inside the main()
function only, why not put the class definition inside the main()
function also? Is this bad programming practice? Or is this something that only C programmers do with structs? What is the reason to put the class definition in the global scope?
int main() {
class Dog { ... };
Dog d1;
...
return 0;
}