Re: Trouble with constructor(cod e included)
On 9 Apr 2004 22:13:13 -0700, abc_99_420@hotm ail.com (San) wrote:
[color=blue][color=green]
>>//-----------------------------------------------------------------------------[/color][/color]
Hi,
Looking better. Here are some tips:
[color=blue]
>Here's the class definition:
>//-----------------------------------------------------------------------------
>#ifndef Student_h
>#define Student_h
>
>#include <string>
>
>namespace stud{
>
>class Student
>{
> private:
> static const long LATEST_ID = 99999999;
> string student_name;
> long student_id;
> string student_address ;
> long student_phone;
> string student_email;
>
> public:
> Student();
>
> Student(std::st ring name);
>
>}; //class student
>} //namespace stud
>
>#endif[/color]
[color=blue]
>
>I changed the implementation or cpp file to include the foll:
>
>#include "Student.h"
>#include <string>[/color]
It's convention to put the standard headers /first/, and you user-defined
headers last. Not absolutely required, but A Good Idea.
[color=blue]
>
>namespace stud{
>
>Student::Stude nt(std::string name)
>{
> student_name = name;
> student_id = LATEST_ID + 1;[/color]
Since LATEST_ID is a static const, what are you thinking here by assigning
the /same/ value to each student_id? It would make more sense if LATEST_ID
were really latest_id, non-const, initialized to whatever, and incremented
in each constructor (if you haven't dealt with the issue of initializing
static data yet, note that you must declare such data in-class and still
define and initialize it outside the class, usually in the implementation
file.)
Also, student_phone, being a long, does need to be initialized. If I
omitted it earlier in my example initialization list, it is simply because
I didn't notice it there among all the strings, and/or I figured it would
be a string. In fact, it would be better off as a string. I don't think
you'd be able to represent my full phone area code/number with a 32-bit
long ;-)
[color=blue]
>}
>
>Student::Stude nt()
>{
> student_name = "";
> student_id = LATEST_ID + 1;[/color]
Same story with LATEST_ID and student_phone.
[color=blue]
>}
>} //namespace stud
>
>//-----------------------------------------------------------------------------
>
>
>The main file code contains the foll:
>
>#include "Student.h"
>using namespace std;
>using namespace stud;
>
>int main()
>{
> Student stud_1;
> Student stud_2("San");
> return 0;
>}
>
>
>
>I am aware of the advantages using initialization before the
>constructor body and did start out with that. But when I came across
>the errors, I thought maybe I was doing something wrong in the
>initializati on part and so resorted to assigment in the body.[/color]
Hope my earlier explanation of that made enough sense.
[color=blue]
>
>If I have to initialize a string variable with an empty string and a
>long integer with 0 isnt this the right way to do it?
>Student::Stude nt():student_na me(""),student_ phone(0){}[/color]
In /your/ case, this works, but you'd be better off allowing the string to
be default-initialized, which you can do by simply omitting the initializer
altogether.
[color=blue]
>
>
>Thanks for all the advice. The Student stud_1(); thing was really
>silly of me :-)[/color]
No, it wasn't. I, and most other C++ programmers (I dare say) have done it,
and perhaps still do it, on a regular basis ;-)
[color=blue]
>
>
>As an aside, I have just realised that posting to Usenet using Google
>incurs a long delay. Being new to the usenet community, I am trying to
>find newsreaders that update posts faster. Kindly bear with me.[/color]
Try Agent. There's Free Agent, but the full-blown version is well worth the
money.
-leor
[color=blue]
>
>Thanks,
>Santosh[/color]
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
On 9 Apr 2004 22:13:13 -0700, abc_99_420@hotm ail.com (San) wrote:
[color=blue][color=green]
>>//-----------------------------------------------------------------------------[/color][/color]
Hi,
Looking better. Here are some tips:
[color=blue]
>Here's the class definition:
>//-----------------------------------------------------------------------------
>#ifndef Student_h
>#define Student_h
>
>#include <string>
>
>namespace stud{
>
>class Student
>{
> private:
> static const long LATEST_ID = 99999999;
> string student_name;
> long student_id;
> string student_address ;
> long student_phone;
> string student_email;
>
> public:
> Student();
>
> Student(std::st ring name);
>
>}; //class student
>} //namespace stud
>
>#endif[/color]
[color=blue]
>
>I changed the implementation or cpp file to include the foll:
>
>#include "Student.h"
>#include <string>[/color]
It's convention to put the standard headers /first/, and you user-defined
headers last. Not absolutely required, but A Good Idea.
[color=blue]
>
>namespace stud{
>
>Student::Stude nt(std::string name)
>{
> student_name = name;
> student_id = LATEST_ID + 1;[/color]
Since LATEST_ID is a static const, what are you thinking here by assigning
the /same/ value to each student_id? It would make more sense if LATEST_ID
were really latest_id, non-const, initialized to whatever, and incremented
in each constructor (if you haven't dealt with the issue of initializing
static data yet, note that you must declare such data in-class and still
define and initialize it outside the class, usually in the implementation
file.)
Also, student_phone, being a long, does need to be initialized. If I
omitted it earlier in my example initialization list, it is simply because
I didn't notice it there among all the strings, and/or I figured it would
be a string. In fact, it would be better off as a string. I don't think
you'd be able to represent my full phone area code/number with a 32-bit
long ;-)
[color=blue]
>}
>
>Student::Stude nt()
>{
> student_name = "";
> student_id = LATEST_ID + 1;[/color]
Same story with LATEST_ID and student_phone.
[color=blue]
>}
>} //namespace stud
>
>//-----------------------------------------------------------------------------
>
>
>The main file code contains the foll:
>
>#include "Student.h"
>using namespace std;
>using namespace stud;
>
>int main()
>{
> Student stud_1;
> Student stud_2("San");
> return 0;
>}
>
>
>
>I am aware of the advantages using initialization before the
>constructor body and did start out with that. But when I came across
>the errors, I thought maybe I was doing something wrong in the
>initializati on part and so resorted to assigment in the body.[/color]
Hope my earlier explanation of that made enough sense.
[color=blue]
>
>If I have to initialize a string variable with an empty string and a
>long integer with 0 isnt this the right way to do it?
>Student::Stude nt():student_na me(""),student_ phone(0){}[/color]
In /your/ case, this works, but you'd be better off allowing the string to
be default-initialized, which you can do by simply omitting the initializer
altogether.
[color=blue]
>
>
>Thanks for all the advice. The Student stud_1(); thing was really
>silly of me :-)[/color]
No, it wasn't. I, and most other C++ programmers (I dare say) have done it,
and perhaps still do it, on a regular basis ;-)
[color=blue]
>
>
>As an aside, I have just realised that posting to Usenet using Google
>incurs a long delay. Being new to the usenet community, I am trying to
>find newsreaders that update posts faster. Kindly bear with me.[/color]
Try Agent. There's Free Agent, but the full-blown version is well worth the
money.
-leor
[color=blue]
>
>Thanks,
>Santosh[/color]
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
Comment