Naren posted:
Hello All,
I am confused on the object creation.
Is it the constructor or something else?
When constrcutor is called , Is space allocated for the object alraedy
and only intialisation to object members done.
What if the object is create using new?
Could anyone give me a detailed explaination on this?
Thanks in advance.
Regards,
Naren.
Please read this using a monospace font if possible.
class Hello
{
public:
int j; //Let's say int is 32-bit = 4 bytes
double p; //Let's say double is 64-Bit = 8 bytes
char t; //Let's say char is 8-bit = 1 byte
Hello(void)
{
j = 5;
p = 43.4;
t = 's';
}
double GiveMeSecretNumber(void)
{
return j + p;
}
};
There's the "Hello" class. It has three public member variables: j p t. It
has a contructor, "Hello(void)", and it has a public member funciton,
"GiveMeSecretNumber(void)".
Now lets make an object of that class:
int main(void)
{
int numbr;
Hello greeting;
return 0;
}
What you've done is declared a variable, very simply. Just as how I've
declared "int numbr". Our variable is of type "Hello" and it's name is
"greeting". When a variable's type is a class, we call it an object! The
first thing that happens is that memory is allocated for the member
variables, ie. j p t, totalling 13 bytes. Every time you make an object of
the class, eg.:
Hello gh;
Hello rs;
Hello nm;
Each object takes up 13 bytes in memory, no more, no less.
From there, the appropriate constructor is called.
Then the program moves on to the next line of code.
Now when you call one of the member functions, eg.:
greeting.GetSecretNumber();
All that happens is that the function "Hello::GetSecretNumber" is called,
and within that function, when it mentions j p t, it's talking about the
member variables of the object "greeting".