declaring data in classes - what's better? | | |
Hi,
what would you say - which way of building a class is better? Why?
/**** 1 ****/
class Person {
public:
Person(char *n, char *sn, int d);
~Person();
protected:
char *name;
char *second_name;
int date_of_birth;
};
/**** 2 ****/
class Person {
char name;
char second_name;
int date_of_birth;
public:
Person(char n, char sn, int d);
~Person();
}; | | | | re: declaring data in classes - what's better?
On 22 Apr 2006 03:40:43 -0700, "alternativa" <alternativa.4@wp.pl>
wrote:
[color=blue]
>Hi,
>what would you say - which way of building a class is better? Why?
>
>/**** 1 ****/
>class Person {
>public:
> Person(char *n, char *sn, int d);
> ~Person();
>protected:
> char *name;
> char *second_name;
> int date_of_birth;
>};
>
>/**** 2 ****/
>class Person {
> char name;
> char second_name;
> int date_of_birth;
>public:
> Person(char n, char sn, int d);
> ~Person();
>};[/color]
Depends on how many people you know with first and second names that
are only one letter long.
Look up std::string. | | | | re: declaring data in classes - what's better?
Sorry, I forgot to change the second one.. Of course it should look
like this:
/**** 2 ****/
class Person {
string name;
string second_name;
int date_of_birth;
public:
Person(char n, char sn, int d);
~Person(); | | | | re: declaring data in classes - what's better?
Sorry, I forgot to change the second one.. Of course it should look
like this:
/**** 2 ****/
class Person {
string name;
string second_name;
int date_of_birth;
public:
Person(string n, string sn, int d);
~Person(); | | | | re: declaring data in classes - what's better?
alternativa wrote:
[color=blue]
> class Person {
> public:
> Person(char *n, char *sn, int d);
> ~Person();
> protected:
> char *name;
> char *second_name;
> int date_of_birth;
> };[/color]
When you read the first one, you read the public things first. If the class
is useful, you shouldn't even need to read the private part.
And don't use protected until you run out of alternatives. It's not a
language feature so much as a workaround, like mutable or explicit.
--
Phlip http://www.greencheese.org/ZeekLand <-- NOT a blog!!! | | | | re: declaring data in classes - what's better?
alternativa wrote:[color=blue]
> Hi,
> what would you say - which way of building a class is better? Why?
>
> /**** 1 ****/
> class Person {
> public:
> Person(char *n, char *sn, int d);
> ~Person();
> protected:
> char *name;
> char *second_name;
> int date_of_birth;
> };
>
> /**** 2 ****/
> class Person {
> char name;
> char second_name;
> int date_of_birth;
> public:
> Person(char n, char sn, int d);
> ~Person();
> };[/color]
The second (with corrections) is better because the first implies the
possibility of some very broken form of structural inheritence, which
itself is bogus in almost all cases. | | | | re: declaring data in classes - what's better?
"alternativa" <alternativa.4@wp.pl> wrote in message
news:1145702443.021786.249950@e56g2000cwe.googlegr oups.com...[color=blue]
> Hi,
> what would you say - which way of building a class is better? Why?
>
> /**** 1 ****/
> class Person {
> public:
> Person(char *n, char *sn, int d);
> ~Person();
> protected:
> char *name;
> char *second_name;
> int date_of_birth;
> };
>
> /**** 2 ****/
> class Person {
> char name;
> char second_name;
> int date_of_birth;
> public:
> Person(char n, char sn, int d);
> ~Person();
> };
>[/color]
class Person
{
private:
std::string m_name;
std::string m_second_name;
int m_date_of_birth;
public:
Person(std::string const &i_name, std::string const &i_second_name, int
i_date_of_birth) :
m_name(i_name), m_second_name(i_second_name),
m_date_of_birth(i_date_of_birth) {}
std::string const &name() const {return m_name;}
std::string const &second_name() const {return m_second_name;}
int date_of_birth() const {return date_of_birth;}
// no destructor
};
std::string should be preferred over char * in modern C++ programming,
mostly because it just makes things so much easier and less error prone.
It is usually better to initialize a std::string with another std::string.
If you have a char * it still works fine:
Person p("Elmer", "Fudd", 19898973);
and if you already have std::strings you can just write
Person q(astring, bstring, date);
rather than the awkward
Person q(astring.c_str(), bstring.c_str(), date);
If you need a destructor (which you don't when using std::string) you
probably also need a copy constructor and assignment operator. Google for
"C++ rule of 3".
Some programmers reflexively write get/set pairs for each data item. I think
that's a bad habit. You can use the constructor itself to change the data
values:
p = Person("Daffy", "Duck", 890);
This usage emphasizes the idea that a Person is an object rather than just
three related values.
Cy | | | | re: declaring data in classes - what's better?
Cy Edmunds wrote:[color=blue]
> "alternativa" <alternativa.4@wp.pl> wrote in message
> news:1145702443.021786.249950@e56g2000cwe.googlegr oups.com...
>[color=green]
>>Hi,
>>what would you say - which way of building a class is better? Why?
>>
>>/**** 1 ****/
>>class Person {
>>public:
>>Person(char *n, char *sn, int d);
>>~Person();
>>protected:
>>char *name;
>>char *second_name;
>>int date_of_birth;
>>};
>>
>>/**** 2 ****/
>>class Person {
>>char name;
>>char second_name;
>>int date_of_birth;
>>public:
>>Person(char n, char sn, int d);
>>~Person();
>>};
>>[/color]
>
>
> class Person
> {
> private:
> std::string m_name;
> std::string m_second_name;
> int m_date_of_birth;
> public:
> Person(std::string const &i_name, std::string const &i_second_name, int
> i_date_of_birth) :
> m_name(i_name), m_second_name(i_second_name),
> m_date_of_birth(i_date_of_birth) {}
> std::string const &name() const {return m_name;}
> std::string const &second_name() const {return m_second_name;}
> int date_of_birth() const {return date_of_birth;}
> // no destructor
> };
>[/color]
If you are going to provide accessors to all members, why not just use a
struct and make the members const?
--
Ian Collins. | | | | re: declaring data in classes - what's better?
"Ian Collins" <ian-news@hotmail.com> wrote in message
news:4b04hdFv6c7fU2@individual.net...[color=blue]
> Cy Edmunds wrote:[color=green]
>> "alternativa" <alternativa.4@wp.pl> wrote in message
>> news:1145702443.021786.249950@e56g2000cwe.googlegr oups.com...
>>[color=darkred]
>>>Hi,
>>>what would you say - which way of building a class is better? Why?
>>>
>>>/**** 1 ****/
>>>class Person {
>>>public:
>>>Person(char *n, char *sn, int d);
>>>~Person();
>>>protected:
>>>char *name;
>>>char *second_name;
>>>int date_of_birth;
>>>};
>>>
>>>/**** 2 ****/
>>>class Person {
>>>char name;
>>>char second_name;
>>>int date_of_birth;
>>>public:
>>>Person(char n, char sn, int d);
>>>~Person();
>>>};
>>>[/color]
>>
>>
>> class Person
>> {
>> private:
>> std::string m_name;
>> std::string m_second_name;
>> int m_date_of_birth;
>> public:
>> Person(std::string const &i_name, std::string const &i_second_name,
>> int
>> i_date_of_birth) :
>> m_name(i_name), m_second_name(i_second_name),
>> m_date_of_birth(i_date_of_birth) {}
>> std::string const &name() const {return m_name;}
>> std::string const &second_name() const {return m_second_name;}
>> int date_of_birth() const {return date_of_birth;}
>> // no destructor
>> };
>>[/color]
> If you are going to provide accessors to all members, why not just use a
> struct and make the members const?
>
> --
> Ian Collins.[/color]
Don't think of the member functions as accessors to members. Think of them
as inspectors of the object state. I might change the implementation to read
a database instead of storing locally. As long as I don't change the
interface you shouldn't have to worry about it.
Cy |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,383 network members.
|