RE: [Dev-C++] Question about structs...
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Marc H. <ma...@e-...> - 2002-10-23 13:28:18
|
Hi Per W,
You're quite right about the GetXXX() methods. Early morning... Coding not
always best.
Ultimately , I would prefer to use a CString class for name and other string
type properties. Also use TCHAR instead of char, etc. etc. etc. CString
classes generally have a copy constructor so the result of GetName() would
not be able to change the value in the instance.
I did not know that about void. I knew it could be omitted in C++, but is it
convention to omit it?
As for the template container, I did say vector or similar. I guess it
depends on what features you need. Rob was talking about traversing the
"array" using for loops. I think a vector is quite handy for that. The point
was to get away from statically defined arrays.
_____________________________
Marc Heiligers
-----Original Message-----
From: Per Westermark [mailto:pw...@ia...]
Sent: 23 October 2002 02:55 PM
To: Marc Heiligers
Cc: 'Rob Peacock'; dev...@li...
Subject: RE: [Dev-C++] Question about structs...
What use is a Setname(char *szName) when the GetName() have already sent out
the pointer to the m_szName array? The GetName() method should be declared
const char* GetName() const; The First const to say that the returned
pointer may not be used to change the stored name. The second const to say
that the GetName() method doesn't change anything inside the class CEmployee
when called.
In fact, all GetXX() methods should be written as:
type GetXX() const;
Since this is C++, and not C, all methods not taking a parameter should be
written with empty parentheses. It is only in C, that the void keyword is
needed.
If using template containers, it might also be an idea to use the map
container instead, to allow direct sort and location of entries on customer
id.
/Per W
On Wed, 23 Oct 2002, Marc Heiligers wrote:
> Hi,
> If you build an array of structs as you have below, you have a fixed
> number of records. It might be a good idea to build your data
> structure (and I suggest using a class which you can then use to
> validate data and perform operations on your data) and then storing
> your "records" in a STL vector, or similar. This allows you to
> dymaically change the number of employee, etc. etc. etc. So you'd get
> something like:
>
> A file called "employee.h", for example _____________________________
> #include <vector>
>
> using namespace std;
>
> class CEmployee
> {
> public:
> CEmployee(); //constructor - init code
> ~CEmployee(); //destructor - clean up code
> public:
> BOOL SetId(int nId); //Get and set methods for props
> int GetId(void);
> BOOL SetAge(int nAge);
> int GetAge(void);
> BOOL SetSalary(float fSalary);
> float GetSalary(void);
> BOOL SetName(char *szName);
> char* GetName(void);
> private:
> //private properties
> int m_nId;
> int m_nAge;
> float m_fSalary;
> char m_szName[25];
> }
>
> typedef vector<CEmployee*> EMPLOYEES_DATABASE;
>
> _____________________________
> And then use
> _____________________________
> #include <iostream>
> #include "employee.h"
>
> int main(void)
> {
> EMPLOYEES_DATABASE database;
> CEmployee *employee = new CEmployee(); //Create new employee
> employee->SetName("Marc Heiligers"); //Set employee properties
> employee->SetAge(27);
> employee->SetId(0);
> employee->SetSalary(1000000.00);
> database.push_back(employee); //Add employee to
> end of vector
> .
> .
> .
> cout << "Number of employees: " << database.size() << endl;
>
> //Use an iterator to loop through all employees and print their
> names.
> EMPLOYEES_DATABASE::iterator iter;
> for (iter = database.begin(); iter != database.end; iter++)
> {
> employee = *iter;
> cout << "Name: " << employee->GetName() << endl;
> }
> return 0;
> }
> _____________________________
> So, to answer your question (umm, this may have been a bit of
> overkill), yes, you can use employee_name[25] array. You would access
> each "record" with a for loop. But there is no need to loop through
> the name array unless you want to search the name for a particular
> character.
>
> _____________________________
> Marc Heiligers
>
>
> -----Original Message-----
> From: Rob Peacock [mailto:py...@ea...]
> Sent: 22 October 2002 06:35 PM
> To: dev...@li...
> Subject: [Dev-C++] Question about structs...
>
>
> OK, I'm back for more information. I'm looking at a slightly different
> way of building the database I asked about yesterday.
>
> The examples I have seen for a structure are typically a database.
> Here's the one I have seen posted on several newbie sites:
>
> #include <iostream.h>
>
> struct database
> {
> int id_number;
> int age;
> float salary;
> };
>
> int main()
> {
> database employee;
> employee.age=22;
> employee.id_number=1;
> employee.salary=120000.21;
>
> return 0;
> }
>
> My question is this; this struct as it is written will only hold one
> record, and all of the entries are int's. If I wanted to have multiple
> records using the same struct, can I create a struct array (correct
> terminology?) like this:
>
> struct database[100]
> {
> int id_number;
> char employee_name[25];
> int age;
> float salary;
> };
>
> To populate it, or to access the individual records within the array,
> I would use the standard for() loops, correct? Same with the
> emplyee_name[] array?
>
> Thanks all,
>
> --->Rob
>
>
>
>
> -------------------------------------------------------
> This sf.net emial is sponsored by: Influence the future
> of Java(TM) technology. Join the Java Community
> Process(SM) (JCP(SM)) program now.
> http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0002en
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|