简体   繁体   中英

Basic question about CLASSES

If I create a class called Person,

the class should only know about the instantiated object? So if I have 5 Person objects, should they know about each other?

A class is simply a definition or a template.

So it is like asking if a blueprint for a house knows about the houses that were created from that blueprint.

Then furthermore it is like asking if 5 houses built from the same blueprint know about each other.

In both cases the answer is no.

No, actually every time an object is created a memory is allocated for storing its variables, But they can share the member functions of that class. Means the member functions will be the same for every objects. But if the variable is declared as STATIC then there will be a single copy. For example if a variable 'count' is declared as static then we can use it to check how many objects are created by incrementing it every time an object is created. This can be done in the constructor of the class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM