Instatiation of an object | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

Instatiation of an object

class class_name{ access_specifier: members } object_name; What's the purpose of the object_name? Does it have something to do with instatiation?

28th Jul 2019, 1:50 AM
Geral
7 Respostas
+ 4
Hello Geral The object instantiated right after definition makes the object global in program.! eg. You have an object 'object_name' . All public members of this object are accessible throughout the program. //In main function and in other user defined functions also.. But when you instantiate an object inside main its scope and lifetime is limited to main function only.
28th Jul 2019, 5:26 AM
šŸ‡®šŸ‡³OmkaršŸ•‰
šŸ‡®šŸ‡³OmkaršŸ•‰ - avatar
+ 2
~ swim ~ agree with you! But usually simple programs uses class definitions outside any functions. What you are saying is absolutely correct. I just tried to figure out what this question is about. Thanks for clarifying this point šŸ˜ŠšŸ™
28th Jul 2019, 5:34 AM
šŸ‡®šŸ‡³OmkaršŸ•‰
šŸ‡®šŸ‡³OmkaršŸ•‰ - avatar
0
Let me rephrase it. What I'm asking is: Why should I call the object right after class the declaration? usual implementation class class_name { access_specifier: members; } int main (){ class_name a; } "unusual" implementation class class_name { access_specifier: members; } OBJECT_Name; int main (){ class_name a; }
28th Jul 2019, 2:54 AM
Geral
0
~ swim ~ Āæany implementation or usage?
28th Jul 2019, 2:59 AM
Geral
0
Ok
28th Jul 2019, 3:04 AM
Geral
0
The purpose of an object name is to have a variable of a custom data type which is the class_name. When creating a class called class_name, Bear in mind that you are creating a custom data type. When you create an object_name, bear in mind that you are creating a variable name. You can initialize it with a constructor.
28th Jul 2019, 4:43 AM
Franky BrainBox
Franky BrainBox - avatar
0
franky Thanks! The thing here was I couldn't grasp the instatiation right after the class declaration, not the purpose of an object itself.
28th Jul 2019, 4:48 AM
Geral