473,326 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

difference between a structure and a class

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
11 5681

"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
"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
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

"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

"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
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
"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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Ook | last post by:
We have had a discussion on the differences between a class and a structure, and no one is in agreement. As I understand it, a structure defaults to public, a class defaults to private. There are...
6
by: nick | last post by:
I have tried finding an answer to this, but most people just explain classes as a more modular way to program. It seems to me that (forgetting OO programming which I don't quite understand) the...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
6
by: Ashish Sheth | last post by:
Hi All, In C#, How can I get the difference between two dates in number of months? I tried to use the Substract method of the DateTime class and it is giving me the difference in TimeSpan,From...
2
by: SpotNet | last post by:
Hello NewsGroup, More out of curiosity, now I'd love to know which is a better way, if one can think in such terms in this context. If I implement the ChooseColor API function, the documentation...
7
by: raghunandan_1081 | last post by:
Hi guys, can you please tell me what is the Difference between c structure and c++ structure
3
by: AppleBag | last post by:
I'm new to the vb.net scene, (from vb6) and would appreciate if someone could explain the difference between the two? From all I've read they seem to be the exact same thing? And as an expansion...
4
by: eBob.com | last post by:
In my class which contains the code for my worker thread I have ... Public MustInherit Class Base_Miner #Region " Delegates for accessing main UI form " Delegate Sub DelegAddProgressBar(ByVal...
9
by: jinendrashankar | last post by:
Hi All, Can any one have any idea about this issue what is Difference between structure and class in C++ Other then by default structure have public scope and class have private scope For...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.