0
how can it get acces to class members?
in this code the person class take an object of a birthday class as its parameter without asking for specific accces to one of its members or variables, and get access to three of its variables at once? why and how? https://code.sololearn.com/cHaz82lnpfAk/?ref=app
17 Respuestas
0
It does, because it's not initialized with your constructor.
As my answer stated, it's initialized with the copy constructor. Since you didn't define a copy constructor, the compiler generates one for you and call that one for the initialization of bd.
Call counts:
Your constructor: 1
Compiler generated copy constructor: 1
+ 2
You use a public method of the object birthday and this works. This is such as a getter. But example bb.day; cannot work.
0
It did not have access to class member DIRECTLY.
Person class accessed its VALUES through a public function
Hope u get my point
0
i understand but as long as i know to ge access i need to write "bb.month". why here it doesn't need it?
0
when the person object is created, what exactly is happening with the bb parameter? the bb got created earlier, and now it just got called, without getting called of any of its members?
0
bb.month would get the value of variable month and assigned to an another int variable
Int m = bb.month;
In this example it can't be done like that because of the variable month being private
That's why a public function is created in birthday class to PRINT (and not access it DIRECTLY) the value of its variables.
0
When Person object p is created,
It call it constructor
Constructor calls object bb of birthday class
Bb calls its constructor in which the numbers are passed
0
shouldn't the constructor work only when it's created, which happend in the line 35?
0
Yes it did work then that object is passed as parameter to person object
0
im sorry i still dont understand.
0
Person has a Birthday member named bd, which stores a copy of parameter b from Person(string n, Birthday b). Why is it stored is that you use member initializer-list.
You see, bd is a 'public' member. Birthday has a 'public' constructor. Person.printinfo() is 'public'. Inside of it a 'public' method from Birthday class, printDate(), is called.
Every processes is public, meaning the process can be used by everyone.
0
1.in the line 22 the bd object that just got created, take one parameter instead of three, how does it work?
2. that only parameter include one constructor one function and three variables, why that parameter give only the three variables?
0
1. It stores the copy of the parameter. It's as same as
Birthday bd = b;
In that sense, everything in b is copied to bd. it looks natural and straight forward, doesn't it?
2. Except the constructor doesn't appears to belong to the class since that quite doesn't make sense to construct an object by another object, the other methods are still copied to the object, or why can you access it in printinfo()?
0
when the bb is copied to bd, by taking it as its parameter, does the bd skip on its constructor, and copy the variables directly?
if not, how does exactly that constructor work in this case?
0
If you don't know, there are actually 3 types of constructors the compiler can 'generate' for you.
1. Default constructor - takes nothing and does nothing.
This explain why you can create an object of class A which is defined as
class A { };
2. Copy constructor
It defined how the object is initialized with another object (define A a = b where b is an object of class A)
You can make a user-defined copy constructor with the following:
A(const A& a);
Note neither const or & can be omit, or it will just be another constructor rather than the copy one.
3. Move constructor (C++11)
Move constructor is more complicated (in my opinion) compare to the others. Rather than making an object from parameters or other object, you 'move' an object to the other object. But since your code doesn't involve it. I'm just going to mention it. If you find C++ interesting you can search it yourself.
So why does your code work? Because a compiler generates a copy constructor for your class. Just that simple.
0
i understand that it copies directly the object, and doesn't copy the variables one by one.
but does it skip on that line? (int m, int d, int y) : month(m),day(d), year(y)
0
thank you