By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,814 Members | 1,678 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,814 IT Pros & Developers. It's quick & easy.

Why we need classes

P: n/a
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??

Jul 23 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
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.

Jul 23 '05 #2

P: n/a
On 27 Feb 2005 19:34:41 -0800, "Pratts" <it*********@rediffmail.com>
wrote in comp.lang.c++:
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/~a...FAQ-acllc.html
Jul 23 '05 #3

P: n/a
"Pratts" writes:
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.
Jul 23 '05 #4

P: n/a

"Pratts" <it*********@rediffmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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
DH*************@cox.net


Jul 23 '05 #5

P: n/a
> 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.

Jul 23 '05 #6

P: n/a
>> 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.

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.

Jul 23 '05 #7

P: n/a

"Pratts" <it*********@rediffmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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

Jul 23 '05 #8

P: n/a
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

Jul 23 '05 #9

P: n/a

"Pratts" <it*********@rediffmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.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 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.
Jul 23 '05 #10

P: n/a
DHOLLINGSWORTH2 wrote:

[ ... ]
structures do not provide anywhere near the functionality you get
from classess.
Not true.
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.

Jul 23 '05 #11

P: n/a
"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
....
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: do***********************@hotmail.com
Above views may belong only to me.
Jul 23 '05 #12

P: n/a

"Larry Brasfield" <do***********************@hotmail.com> wrote in message
news:%N**************@news.uswest.net...
"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.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
Jul 23 '05 #13

P: n/a
"Howard" <al*****@hotmail.com> wrote in message
news:eV********************@bgtnsc04-news.ops.worldnet.att.net...

"Larry Brasfield" <do***********************@hotmail.com> wrote in message news:%N**************@news.uswest.net...
"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.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: do***********************@hotmail.com
Above views may belong only to me.
Jul 23 '05 #14

P: n/a
> "Howard" <al*****@hotmail.com> wrote in message
news:eV********************@bgtnsc04-news.ops.worldnet.att.net...

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. ;-)


A good book to read at this time is a book about the logical thinking
machine, by lewis carrol.
I'm sorry I cant remember the Title, but logic was in the title, the cover
is pink and blue.

You'll communicate with your computer better when your done.
Jul 23 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.