Questions about c++: classes

Jun 4, 2014 at 4:45pm
Let's say we have a class with a variable of name. When you create two objects and give both a name, does this create two copies of that variable?

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class info 
{ 
private: 
string name; 

public: 
info() 
{ 
string info.setname(string name); 
void info.getname(); 
cout << "Your name is: " << name; 
} 
}; 

int main() 
{ 
info one; 
info two; 

return 0; 
}



Also, can classes see how other classes are implemented?
Last edited on Jun 4, 2014 at 4:48pm
Jun 4, 2014 at 4:49pm
Yes, each info object has its own copy of name.

Also, can classes see how other classes are implemented?

I don't understand.
Jun 4, 2014 at 4:55pm
Yes, each info object has its own copy of name.

So there are two copies? Darn, I thought it was only one!

Sorry, this is confusing for me as well!

Classes usually don't know how other classes are implemented, right? I'm looking at a question on my homework here, the question is:

Classes do not have the property of _____.
a. encapsulating data
b. information hiding
c. containing both data and functions
d. usually knowing how other classes are implemented

By process of elimination I'm absolutely sure that classes can encapsulate data, use information hiding, and can contain both data and functions. But I'm curious if maybe I was wrong. Do classes know how other classes are implemented?
Last edited on Jun 4, 2014 at 4:56pm
Jun 4, 2014 at 4:59pm
So there are two copies? Darn, I thought it was only one!

You can declare name as static. Only one instance of name will exist across all instances of your class.

Do classes know how other classes are implemented?

No.


Jun 4, 2014 at 5:03pm
So, declaring static would make only one, does that mean that they are non-static by default?

Well, at least I'll know it now. Thank you very much for your responses!
Jun 4, 2014 at 5:21pm
So, declaring static would make only one, does that mean that they are non-static by default?
Yes, because it is the way you want most of your class variables be, so it is the default behavior.

Jun 4, 2014 at 5:35pm
In your example info class, I'm not sure why you would want name to be static.

Presumably info contains information about a person, or student, or such. If that assumption is correct, then you would not want name to be static, since each person would have a different name.
Topic archived. No new replies allowed.