Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > difference between a structure and a class

Reply
Thread Tools

difference between a structure and a class

 
 
Shea Martin
Guest
Posts: n/a
 
      06-03-2005
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


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      06-03-2005

"Shea Martin" <(E-Mail Removed)> wrote in message
news:h1%ne.41394$(E-Mail Removed)...
>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


 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      06-03-2005
"Shea Martin" <(E-Mail Removed)> wrote in message
news:h1%ne.41394$(E-Mail Removed)
> 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

 
Reply With Quote
 
Jordan DeLong
Guest
Posts: n/a
 
      06-03-2005
On Fri, 03 Jun 2005 16:03:44 +0000, Mike Wahler wrote:
> "Shea Martin" <(E-Mail Removed)> wrote in message
> news:h1%ne.41394$(E-Mail Removed)...
>>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
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      06-03-2005

"John Carson" <(E-Mail Removed)> wrote in message
news:d7pv7h$rde$(E-Mail Removed)...

> 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


 
Reply With Quote
 
Peter Julian
Guest
Posts: n/a
 
      06-03-2005

"Shea Martin" <(E-Mail Removed)> wrote in message
news:h1%ne.41394$(E-Mail Removed)...
> 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;
}

 
Reply With Quote
 
EventHelix.com
Guest
Posts: n/a
 
      06-04-2005
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

 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      06-04-2005
"EventHelix.com" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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

 
Reply With Quote
 
Alan Johnson
Guest
Posts: n/a
 
      06-04-2005
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
 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      06-04-2005
"Alan Johnson" <(E-Mail Removed)> wrote in message
news:d7s5r3$1uj$(E-Mail Removed)
> 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., a non-trivial destructor (12.4), or a non-trivial
copy assignment operator (13.5.3, 12. cannot be a member of a union, nor
can an array of such objects."

--
John Carson

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between c structure and c++ structure raghunandan_1081@yahoo.com C++ 9 11-11-2011 07:34 AM
difference between structure and string? suaib C Programming 4 04-06-2010 03:09 PM
difference between structure and union ,, can anyone help me? manish sahu C++ 5 02-14-2008 11:23 AM
what is the difference between abstract class and pure abstract class? skishorev@yahoo.co.in C++ 4 05-17-2006 08:07 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM



Advertisments