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

difference between a structure and a class

P: n/a
I have been programming in C++ for over 4 years. I *think* I knew that
a struct could have a constructor but I decided to dig into it a little
more today, and found that there is very little difference between a
struct and a class in C++. Both have inheritance, access modifiers,
etc. The only diff I see is the default access level is public vs.
private.

I have always just used structs as a minimal class, as that is the way
they were used in textbooks.

Do structs have all these features in pure C? If not then why did c++
change them, when they were adding the Class type? Are there more
differences than those I have listed? Performance differences? I have
always just used structs as a minimal class, as that is the way they
were used in textbooks.

Thoughts, Ideas?

~S
Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

"Shea Martin" <sa**************@hotmail.com> wrote in message
news:h1********************@ursa-nb00s0.nbnet.nb.ca...
I have been programming in C++ for over 4 years. I *think* I knew that
a struct could have a constructor but I decided to dig into it a little
more today, and found that there is very little difference between a
struct and a class in C++. Both have inheritance, access modifiers,
etc. The only diff I see is the default access level is public vs.
private.
That is indeed the only difference. Note that also included
in 'access' is the default inheritance: class has 'private',
and struct has 'public'. That's why you'll typicall see
derived classes declared as:

class B : public A
I have always just used structs as a minimal class, as that is the way
they were used in textbooks.
Anything that can be done with a class can also be
done with a struct.

Do structs have all these features in pure C?
No.

If not then why did c++
change them,
Because C++ is not C.
when they were adding the Class type?
'class' is not a type. 'class' (and 'struct') is a keyword
the programmer can use to create custom (aggregate) types.

Are there more
differences than those I have listed?
No.
Performance differences?
That's an implementation detail not addresses by the
language. See "Quality of Implementation".
I have
always just used structs as a minimal class, as that is the way they
were used in textbooks.
Objects created from a 'struct' definition are no
less than those created from a 'class' definition,
so 'minimal' does not apply.
Thoughts, Ideas?


Since the 'mechanics' are the same, I suggest using
the different keywords 'class' and 'struct' as
indicators of your intentions: I.e. use 'class'
when you're creating abstract types with special
functionality (e.g. polymorphism), use 'struct'
if simply grouping related simple data items (e.g.
a pair of integer objects). But ultimately, the
choice is yours (or often that of your employer/
client).
-Mike
Jul 23 '05 #2

P: n/a
"Shea Martin" <sa**************@hotmail.com> wrote in message
news:h1********************@ursa-nb00s0.nbnet.nb.ca
I have been programming in C++ for over 4 years. I *think* I knew
that a struct could have a constructor but I decided to dig into it a
little more today, and found that there is very little difference
between a struct and a class in C++. Both have inheritance, access
modifiers, etc. The only diff I see is the default access level is
public vs. private.

I have always just used structs as a minimal class, as that is the way
they were used in textbooks.

Do structs have all these features in pure C?
No. They don't have any of those features. Structs in C are just containers
for data types.
If not then why did c++ change them, when they were adding the Class type?
I suppose you would have to ask Stroustrup (or read his books), but you can
use structs exactly as they are used in C or you can use them with the added
features. Your choice. Isn't that better that only being able to use them as
in C?
Are there more
differences than those I have listed? Performance differences? I have
always just used structs as a minimal class, as that is the way they
were used in textbooks.


Default inheritance, in addition to access, is public with structs. I think
that is about all the differences, but maybe there is one more.
--
John Carson

Jul 23 '05 #3

P: n/a
On Fri, 03 Jun 2005 16:03:44 +0000, Mike Wahler wrote:
"Shea Martin" <sa**************@hotmail.com> wrote in message
news:h1********************@ursa-nb00s0.nbnet.nb.ca...
I have been programming in C++ for over 4 years. I *think* I knew that
a struct could have a constructor but I decided to dig into it a little
more today, and found that there is very little difference between a
struct and a class in C++. Both have inheritance, access modifiers,
etc. The only diff I see is the default access level is public vs.
private.
That is indeed the only difference. Note that also included
in 'access' is the default inheritance: class has 'private',
and struct has 'public'. That's why you'll typicall see
derived classes declared as:

class B : public A

[...] Since the 'mechanics' are the same, I suggest using
the different keywords 'class' and 'struct' as
indicators of your intentions:

[...]

Or better yet, to avoid putting tons of `public' in your code, you can
just use `struct' instead when you declare classes. Many (most?)
people seem to put public data members/functions at the top of their
class (and private at the bottom); the default access specifiers for
`class' are inconvenient for this style of layout (both for reading and
for writing).

class B : public A, public N, public Q<B> {
public:
void f();
};

vs.

struct B : A, N, Q<B> {
void f();
};

--
Jordan DeLong
fr******@allusion.net

Jul 23 '05 #4

P: n/a

"John Carson" <jc****************@netspace.net.au> wrote in message
news:d7**********@otis.netspace.net.au...
Default inheritance, in addition to access, is public with structs. I
think that is about all the differences, but maybe there is one more.


Yup -- different spelling. :-)

-Mike
Jul 23 '05 #5

P: n/a

"Shea Martin" <sa**************@hotmail.com> wrote in message
news:h1********************@ursa-nb00s0.nbnet.nb.ca...
I have been programming in C++ for over 4 years. I *think* I knew that
a struct could have a constructor but I decided to dig into it a little
more today, and found that there is very little difference between a
struct and a class in C++. Both have inheritance, access modifiers,
etc. The only diff I see is the default access level is public vs.
private.

A struct and a class is exactly the same thing (called an abstract type) so
long as we are confining the definition to the C++ language. The only
difference is the default access given to their members, ctors, dtor and
member functions.

Your statement about constructors is misleading, a class or struct *must*
have a constructor and a destructor, there is no choice offered here. The
question is: will the compiler generate defaults for you or did you choose
to provide the d~tor and ctors yourself? If you supply a single constructor
with arguement(s), for example, the compiler ceases to supply any default,
non-copy constructor. (see code below)

The compiler provides a default shallow copy-ctor and an assignment operator
as well. The compiler will always supply these until you overide the
compiler's responsability to do so. Again: same for struct and class.

I have always just used structs as a minimal class, as that is the way
they were used in textbooks.

Do structs have all these features in pure C? If not then why did c++
change them, when they were adding the Class type? Are there more
differences than those I have listed? Performance differences? I have
always just used structs as a minimal class, as that is the way they
were used in textbooks.


No difference at all between the two. One uses the keyword struct and the
other class. Even inheritence applies to both with respect for their access
defaults.

Note how the following Abstract class is not sufficient to create an array
of Abstract objects. An array requires a default constructor (thats the
law). The default constructor can't be supplied here because the compiler
acknowledges that the creator of the class has taken-over the responsability
to supply all non-copy constructors.

The following won't compile unless you uncomment the default ctor (or
comment-out all ctors for the compiler to generate them for you).

#include <iostream>

class Abstract
{
int a; // private
public:
// ctors
// Abstract() { std::cout << "default ctor invoked\n"; } // uncomment
this ctor
Abstract(int n) : a(n) { std::cout << "ctor invoked\n"; }
// d~tor
~Abstract() { std::cout << "\nd~tor invoked"; }
// copy-ctor
Abstract(const Abstract& r_copy)
{
std::cout << "copy-ctor invoked\n";
a = r_copy.getA();
}
// member function (accessor)
int getA() const { return a; }
};
int main()
{
Abstract array[5]; // an array of 5 default Abstract objects
Abstract abstract(99); // a single Abstract object with member a = 99
Abstract copy_of_abstract(abstract); // copy
array[0] = copy_of_abstract; // assignment

std::cout << "array[0].getA() = " << array[0].getA();

return 0;
}

Jul 23 '05 #6

P: n/a
Classes and structures also differ in terms of the memory
layout of member variables. In a simple structure,
the memory layout is in the order of the fields. In a class
the memory layout is undefined and compiler dependent.

--
EventStudio 2.5 - http://www.EventHelix.com/EventStudio
Auto Layout and Generate Sequence Diagrams in PDF and MS Word

Jul 23 '05 #7

P: n/a
"EventHelix.com" <ev********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com
Classes and structures also differ in terms of the memory
layout of member variables. In a simple structure,
the memory layout is in the order of the fields. In a class
the memory layout is undefined and compiler dependent.


In circumstances in which a struct is guaranteed to have members laid out in
the order of declaration, an otherwise identical class will likewise have
members laid out in the order of declaration provided the class begins with
the keyword public:, as in

class X
{
public:
// stuff
};

In other words, the only difference is one of default access specifiers.
Adding public: changes the class's access and makes it equivalent to a
struct for layout purposes. If a struct has virtual members etc., then its
member layout is no longer guaranteed, just as with a class that has virtual
members etc.

--
John Carson

Jul 23 '05 #8

P: n/a
Shea Martin wrote:
Thoughts, Ideas?


Just a bit of trivia: A union can also have member functions (including
constructors and destructors) in C++. It cannot, however, participate in
inheritance or have virtual functions.

-Alan
Jul 23 '05 #9

P: n/a
"Alan Johnson" <al**@undisclosed.com> wrote in message
news:d7**********@news.Stanford.EDU
Shea Martin wrote:
Thoughts, Ideas?


Just a bit of trivia: A union can also have member functions
(including constructors and destructors) in C++.

True. However:

Section 9.5/1
"An object of a class with a non-trivial constructor (12.1), a non-trivial
copy constructor (12.8), a non-trivial destructor (12.4), or a non-trivial
copy assignment operator (13.5.3, 12.8) cannot be a member of a union, nor
can an array of such objects."

--
John Carson

Jul 23 '05 #10

P: n/a
EventHelix.com wrote:
Classes and structures also differ in terms of the memory
layout of member variables. In a simple structure,
the memory layout is in the order of the fields. In a class
the memory layout is undefined and compiler dependent.

Not true. The ONLY difference is the default access. The
memory layout is the same. Between access specifiers, they
are laid out in increasing memory address (but their may be
interspersed padding).
Jul 23 '05 #11

P: n/a
Hi ,
There is only the access qualifiers that differ in both. In C++ default
is private as Class is designed for data hiding and struct default is
public... Tehre is no difference. It may happen if u are constructing a
compiler for C++ that for both delarations u can replace with a single
name say CLST
like for
class example{
....
}

struct exmple1{
.....
}

both wil be translated to

CLST example{
....
}
and
CLST exmple1{
....
}

but u need to take care for the default access qualifiers.. Thats all
struct was insrduced for the only fact that it was at that time people
or C programmers adore struct more than class.. Introduction of struct
has more to do with mentality of people rather than technical.

Bye

Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.