+ 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?
11 ответов
+ 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.
+ 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
😊🙏
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;
}
0
~ swim ~
¿any implementation or usage?
0
Ok
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.
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.