 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Pratts Guest
|
Posted: Mon Feb 28, 2005 3:34 am Post subject: Why we need classes |
|
|
I am a new one who have joined u plz try to help me bcoz i could not
find ny sutiable answer foer this Question
Qus>>why do we need classes when structures provide similar
functionality??
|
|
| Back to top |
|
 |
Kelly Mandrake Guest
|
Posted: Mon Feb 28, 2005 3:41 am Post subject: Re: Why we need classes |
|
|
structures are public by default and classes are private by default.
Unless you use the keywords private, public or protected, any member
funtions or member data in a structure will be public. The idea behind
encapsulation is to hide the details, it is desireable to make private
data and provide public interface methods that the user of the class
can use.
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Mon Feb 28, 2005 4:15 am Post subject: Re: Why we need classes |
|
|
On 27 Feb 2005 19:34:41 -0800, "Pratts" <its_prateek (AT) rediffmail (DOT) com>
wrote in comp.lang.c++:
| Quote: | I am a new one who have joined u plz try to help me bcoz i could not
find ny sutiable answer foer this Question
Qus>>why do we need classes when structures provide similar
functionality??
|
A better way to ask this question would be:
"Why do we need structs in C++ when we have classes?"
There is a difference in default access, but this is really trivial.
Structs are needed for backwards compatibility with C code, of which
there were millions of lines in existence as C++ came into being.
Classes are little more than a documentation mechanism.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
|
|
| Back to top |
|
 |
osmium Guest
|
Posted: Mon Feb 28, 2005 4:25 am Post subject: Re: Why we need classes |
|
|
"Pratts" writes:
| Quote: | I am a new one who have joined u plz try to help me bcoz i could not
find ny sutiable answer foer this Question
Qus>>why do we need classes when structures provide similar
functionality??
|
If you mean why do we need the keyword "class" in addition to stuct, the
answer is, we don't. It's just for convenience and, as typically used,
improves documentation.
|
|
| Back to top |
|
 |
DHOLLINGSWORTH2 Guest
|
Posted: Mon Feb 28, 2005 7:29 am Post subject: Re: Why we need classes |
|
|
"Pratts" <its_prateek (AT) rediffmail (DOT) com> wrote
| Quote: | I am a new one who have joined u plz try to help me bcoz i could not
find ny sutiable answer foer this Question
Qus>>why do we need classes when structures provide similar
functionality??
|
structures do not provide anywhere near the functionality you get from
classess.
Classess have constructors, that can be modified./overloaded.
Classes have destructors, that can be modified/ overloaded.
Classes allow for operator Overloads.
Classes have member functions, static functions.
Public, Private, and Protected Data and code.
Classes incapsulate. Structures Arrange.
The differences are vast. You will have to write some code to begin to
understand the impact.
I struggled with it for several months.
You gain Power, Robustness. A level of Protection, while increasing the
longevity of your code.
I can send you some examples done with both structures and class object, if
you'd like to see the difference.
dan
[email]DHOLLINGSWORTH2 (AT) cox (DOT) net[/email]
|
|
| Back to top |
|
 |
440gtx@email.com Guest
|
Posted: Mon Feb 28, 2005 8:49 am Post subject: Re: Why we need classes |
|
|
| Quote: | Classess have constructors, that can be modified./overloaded.
Classes have destructors, that can be modified/ overloaded.
Classes allow for operator Overloads.
Classes have member functions, static functions.
Public, Private, and Protected Data and code.
Classes incapsulate. Structures Arrange.
|
struct was upgraded in C++ and can do all of that too. Try it.
I personally stopped using the class keyword and went back to struct.
The reason is I can eliminate that silly "public:" statement at the
beginning needed to expose the constructor. In the end, "class" proved
to be just an unnecessary annoyance with no value add for me. I do use
public, protected, and private extensively to define structs.
|
|
| Back to top |
|
 |
Martijn Mulder Guest
|
Posted: Mon Feb 28, 2005 9:18 am Post subject: Re: Why we need classes |
|
|
| Quote: | Classess have constructors, that can be
modified./overloaded. Classes have destructors, that can
be modified/ overloaded. Classes allow for operator
Overloads.
Classes have member functions, static functions.
Public, Private, and Protected Data and code.
Classes incapsulate. Structures Arrange.
|
| Quote: | struct was upgraded in C++ and can do all of that too.
Try it.
I personally stopped using the class keyword and went
back to struct. The reason is I can eliminate that silly
"public:" statement at the beginning needed to expose the
constructor. In the end, "class" proved to be just an
unnecessary annoyance with no value add for me. I do use
public, protected, and private extensively to define
structs.
|
Indeed. I think that 'class' and 'struct' where meant to be equivalent. I never
touched another class after reading what Bjarne Stroustrup wrote about it in the
Annotated C++ Reference Manual, Addison-Wesly Publishing Company, 1995,
Paragraph 11.2:
<quote>
(...) For example, novices often don't know about access specifiers and get
confused by this:
class X { public: f(); };
class Y : X { }; // no access specifier
// private by default
void g(Y* p)
{
p->f(); // error
}
Even experts can get caught. A compiler can be most helpful by issuing a warning
for the missing access specifier.
Having private as the default was chosen to reflect the general view that
things that are not explicitly declared public are private. Defining a default
access specifier was probably a mistake.
</quote>
He says that the default access specifier 'private' for classes was a mistake.
There should have been _no_ default access specifier, ie class should be
'public' by default, just like struct is. The difference between class and
struct is based on a mistake. A class was meant to act just like a struct does.
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Mon Feb 28, 2005 4:22 pm Post subject: Re: Why we need classes |
|
|
"Pratts" <its_prateek (AT) rediffmail (DOT) com> wrote
| Quote: | I am a new one who have joined u plz try to help me bcoz i could not
find ny sutiable answer foer this Question
Qus>>why do we need classes when structures provide similar
functionality??
we r n nglsh lang nwsgrp her, not IM |
try complt wrds nxt tm
|
|
| Back to top |
|
 |
Pratts Guest
|
Posted: Thu Mar 10, 2005 5:12 am Post subject: Re: Why we need classes |
|
|
buddy very sorry to say that
u must go through the things again bcoz structures can do all that
constructor
destructors
operator Overloads and every thing that class can do
plz try this out
byEEEEEEEEEEE
|
|
| Back to top |
|
 |
DHOLLINGSWORTH2 Guest
|
Posted: Fri Mar 11, 2005 5:12 am Post subject: Re: Why we need classes |
|
|
"Pratts" <its_prateek (AT) rediffmail (DOT) com> wrote
| Quote: | buddy very sorry to say that
u must go through the things again bcoz structures can do all that
constructor
destructors
operator Overloads and every thing that class can do
plz try this out
byEEEEEEEEEEE
|
Can structures have pure virtual Functions?
Can a structure prevent a programmer from erroneously ovewriting data
elements?
Can a structure have Private data?
Can a structure have Protected data?
The truth is I'm not sure, I use structs to set up Common data that the
Class objects will be using, and sharing with the outside world.
|
|
| Back to top |
|
 |
Karl Heinz Buchegger Guest
|
Posted: Fri Mar 11, 2005 9:55 am Post subject: Re: Why we need classes |
|
|
DHOLLINGSWORTH2 wrote:
| Quote: |
"Pratts" <its_prateek (AT) rediffmail (DOT) com> wrote in message
news:1110431570.048996.266740 (AT) l41g2000cwc (DOT) googlegroups.com...
buddy very sorry to say that
u must go through the things again bcoz structures can do all that
constructor
destructors
operator Overloads and every thing that class can do
plz try this out
byEEEEEEEEEEE
Can structures have pure virtual Functions?
Can a structure prevent a programmer from erroneously ovewriting data
elements?
Can a structure have Private data?
Can a structure have Protected data?
|
The answer to all your questions is a loud and sound: Yes
--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]
|
|
| Back to top |
|
 |
Jerry Coffin Guest
|
Posted: Fri Mar 11, 2005 5:29 pm Post subject: Re: Why we need classes |
|
|
DHOLLINGSWORTH2 wrote:
[ ... ]
| Quote: | structures do not provide anywhere near the functionality you get
from classess.
|
Not true.
| Quote: | Classess have constructors, that can be modified./overloaded.
Classes have destructors, that can be modified/ overloaded.
Classes allow for operator Overloads.
Classes have member functions, static functions.
Public, Private, and Protected Data and code.
Classes incapsulate. Structures Arrange.
|
In C++, all of the above statements apply equally to structs as
classes. The ONLY difference between a struct and a class in C++ is the
default visibility (private for class, public for struct). C++ allows
structs to have ctors, dtors, operator overloads, member functions and
public, private and protected members. You can derive one struct from
another struct, or even derive a struct from a class or a class from a
struct.
As an aside: destructors are only _rarely_ overloaded, and there are
only two overloads: one that is used when exiting a placement new via
an exception, and the usual one (no parameters, no cv-qualifiers)
that's used all the rest of the time.
--
Later,
Jerry.
The universe is a figment of its own imagination.
|
|
| Back to top |
|
 |
Larry Brasfield Guest
|
Posted: Fri Mar 11, 2005 5:44 pm Post subject: Re: Why we need classes |
|
|
"Jerry Coffin" <jcoffin (AT) taeus (DOT) com> wrote
....
| Quote: | As an aside: destructors are only _rarely_ overloaded, and there are
only two overloads: one that is used when exiting a placement new via
an exception, and the usual one (no parameters, no cv-qualifiers)
that's used all the rest of the time.
|
In fact, '_rarely_' is never. You are thinking of
the operator delete overload that will (or should)
match an operator new used in a placement new.
The only time destructors are run automatically
by a correct C++ compiler is after construction
of the object has successfully completed.
There is no such thing as an overloaded destructor.
--
--Larry Brasfield
email: [email]donotspam_larry_brasfield (AT) hotmail (DOT) com[/email]
Above views may belong only to me.
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Fri Mar 11, 2005 5:52 pm Post subject: Re: Why we need classes |
|
|
"Larry Brasfield" <donotspam_larry_brasfield (AT) hotmail (DOT) com> wrote
| Quote: | "Jerry Coffin" <jcoffin (AT) taeus (DOT) com> wrote in message
news:1110562149.085576.244920 (AT) g14g2000cwa (DOT) googlegroups.com...
...
The only time destructors are run automatically
by a correct C++ compiler is after construction
of the object has successfully completed.
|
Hmmm, so when I successfully create an object, the destructor is called?
That kind of screws up my whole design... I was planning on using those
newly constructed objects! Care to re-word that, or explain what you
mean a little better?
-Howard
|
|
| Back to top |
|
 |
Larry Brasfield Guest
|
Posted: Fri Mar 11, 2005 5:59 pm Post subject: Re: Why we need classes |
|
|
"Howard" <alicebt (AT) hotmail (DOT) com> wrote
| Quote: |
"Larry Brasfield" <donotspam_larry_brasfield (AT) hotmail (DOT) com> wrote
"Jerry Coffin" <jcoffin (AT) taeus (DOT) com> wrote in message
news:1110562149.085576.244920 (AT) g14g2000cwa (DOT) googlegroups.com...
...
The only time destructors are run automatically
by a correct C++ compiler is after construction
of the object has successfully completed.
Hmmm, so when I successfully create an object, the destructor is called? That kind of screws up my whole design... I was planning
on using those newly constructed objects! Care to re-word that, or explain what you mean a little better?
|
(I might suggest you read more carefully. ;-)
Q. When are destructors ever run automatically?
A. Sometime after successfull construction of
temporary objects and auto storage class objects.
I did not say that destructors are run automatically
upon all successfully constructed objects, nor did I
suggest they are run immediately after construction.
So you can go ahead and keep using your objects!
My statement could have been more clearly written.
--
--Larry Brasfield
email: [email]donotspam_larry_brasfield (AT) hotmail (DOT) com[/email]
Above views may belong only to me.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|