Why we need classes

P

Pratts

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??
 
K

Kelly Mandrake

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.
 
J

Jack Klein

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.
 
O

osmium

Pratts said:
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.
 
D

DHOLLINGSWORTH2

Pratts said:
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
(e-mail address removed)
 
4

440gtx

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.
 
M

Martijn Mulder

Classess have constructors, that can be
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.
 
H

Howard

Pratts said:
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
 
P

Pratts

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
 
D

DHOLLINGSWORTH2

Pratts said:
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.
 
K

Karl Heinz Buchegger

DHOLLINGSWORTH2 said:
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
 
J

Jerry Coffin

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.
 
L

Larry Brasfield

....
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.
 
H

Howard

Larry Brasfield said:
...
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
 
L

Larry Brasfield

Howard said:
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.
 
D

DHOLLINGSWORTH2

Howard said:
(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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top