|
-
Sep 16th, 2003, 02:06 PM
#1
Thread Starter
Addicted Member
Struct vs Class
Hey All,
I need to know where I can find some documentation on the fundamental differences between C++ structures and classes. I know that with structures everything is public, and with classes everything is private by default. So data and functions can be hidden. But I'm not finding much more than that, as far as differences. So can somebody point me in the right direction. I would really appreciate it.
Disclaimer:
* The preceding message was in no means meant to be critical, mean spirited, insincere, or facetious.
Disclaimer for disclaimer:
The preceding disclaimer may in fact be facetious in nature.
Thanks,
Jim
-
Sep 16th, 2003, 02:47 PM
#2
In C++, there is no difference other than the one you stated: structs are public access by default, whereas classes are private. I think the general rule is to use a class when you have member functions and / or need to hide data; uses structs for simple things that are all public:
Code:
struct Force
{
int x;
int y;
double angle;
int magnitude;
};
Code:
class MovingObject
{
public:
MovingObject(int initialx, int initialy);
void Move();
void ApplyForce(const Force& force);
private:
int x, y, dx, dy;
};
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Sep 17th, 2003, 05:15 AM
#3
-
Sep 17th, 2003, 05:37 AM
#4
Monday Morning Lunatic
You can do all of that with structs in C++
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 17th, 2003, 12:12 PM
#5
The one other difference I forgot is the default inheritance modifier;
Code:
on structs, saying
struct foo : bar
{
};
is equal to
struct foo : public bar
{
};
whereas for classses:
class foo : bar
{
};
is the same as
class foo : private bar
{
};
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Sep 17th, 2003, 02:21 PM
#6
Thread Starter
Addicted Member
Wow, thanks to all who replied... ...this is great, I'm coming from a Cobol background, so this is all new to me. Inheritance, is this along the lines of the polymorphism concept. Sunburnt, what is bar in your example, another struct or class?
Thanks again.
And Parksie is correct about structs in C++, you can assign member functions.
Disclaimer:
* The preceding message was in no means meant to be critical, mean spirited, insincere, or facetious.
Disclaimer for disclaimer:
The preceding disclaimer may in fact be facetious in nature.
Thanks,
Jim
-
Sep 17th, 2003, 05:43 PM
#7
yup; that second part isn't very important; if you always specify what type of inheritance you want (public / private) you'll never run into problems, and your code will probably be clearer.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|