?
=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Hi,
Consider this code:
--- beginning of code ---
#include <iostream>
using namespace std;
class Child{
public:
Child(const int& n); // constructor
void Show();
private:
int ChildNmb; // the childs number in the sequence of siblings
const int& NmbOfSibls; // the number of siblings (including itself)
};
// constructor:
Child::Child(const int& n)
:ChildNmb(n), NmbOfSibls(n){ } // initialisation - const int& NmbOfSibls
= n,
// NmbOfSibls becomes an alias for the actual argument variable.
void Child::Show() {
cout << "I am child number: " << ChildNmb << ". I have " <<
NmbOfSibls-1 << " brothers and sisters." << endl;
}
//************************************
int main(){
int n=0; // Number of children
++n; Child c1(n);
++n; Child c2(n);
++n; Child c3(n);
c1.Show(); c2.Show(); c3.Show();
++n; Child c4(n);
c1.Show(); c2.Show(); c3.Show(); c4.Show();
}
--- end of code ---
I'm a bit confused about this code... For instance:
Using "class Child{" gives *EXACTLY* the same result as using "struct
Child{" - both compiles and runs as well... Why? And why is there both
type struct and type class, when it seems like there's no difference?
Another thing is that I'm a bit confused about the constructor:
"Child::Child(const int& n)" - because I can easily understand if n in
this case becomes an alias for another variable. But if one pass a
number such as Child c5(5); to the program, then 5 is not a variable...
So 5 is stored in memory somewhere and therefore it doesn't need an
alias... Or am I missing something?
Best regards / Med venlig hilsen
Martin Jørgensen
Consider this code:
--- beginning of code ---
#include <iostream>
using namespace std;
class Child{
public:
Child(const int& n); // constructor
void Show();
private:
int ChildNmb; // the childs number in the sequence of siblings
const int& NmbOfSibls; // the number of siblings (including itself)
};
// constructor:
Child::Child(const int& n)
:ChildNmb(n), NmbOfSibls(n){ } // initialisation - const int& NmbOfSibls
= n,
// NmbOfSibls becomes an alias for the actual argument variable.
void Child::Show() {
cout << "I am child number: " << ChildNmb << ". I have " <<
NmbOfSibls-1 << " brothers and sisters." << endl;
}
//************************************
int main(){
int n=0; // Number of children
++n; Child c1(n);
++n; Child c2(n);
++n; Child c3(n);
c1.Show(); c2.Show(); c3.Show();
++n; Child c4(n);
c1.Show(); c2.Show(); c3.Show(); c4.Show();
}
--- end of code ---
I'm a bit confused about this code... For instance:
Using "class Child{" gives *EXACTLY* the same result as using "struct
Child{" - both compiles and runs as well... Why? And why is there both
type struct and type class, when it seems like there's no difference?
Another thing is that I'm a bit confused about the constructor:
"Child::Child(const int& n)" - because I can easily understand if n in
this case becomes an alias for another variable. But if one pass a
number such as Child c5(5); to the program, then 5 is not a variable...
So 5 is stored in memory somewhere and therefore it doesn't need an
alias... Or am I missing something?
Best regards / Med venlig hilsen
Martin Jørgensen