I'm just starting to program and I'm trying to figure out what the difference in a struct and a class is? I've tried google, but I'm still confused. Could someone give me a hand?
Printable View
I'm just starting to program and I'm trying to figure out what the difference in a struct and a class is? I've tried google, but I'm still confused. Could someone give me a hand?
Struct makes more sense.
A struct holds data only
classes hold data AND functions
By default a structures members are public and a class's members are private. Class's support the concept of inheritance, yada, yada, yada.
Take a look at this tutorial
Thanks for the responses....I supposed I shouldn't even bother to ask about what you mean by ineritance yet...
As far as you are concerned, there is no difference. There is one tiny difference, but don't worry about it now. In C++, you use classes.
Ok cool.
So with classes then I put all my variables in one place instead of at the top of my program?....
Did you read the tutorial? That is a very vague question.
Nevermind, I'm dumb. I just found the answer in the tutorial. \
Thanks andyhunter!
You aren't making sense, but that's part of being a beginner. Maybe provide an example of what you mean.Quote:
So with classes then I put all my variables in one place instead of at the top of my program?....
No I have it all figured out now i think....I was confusing how classes worked with global variables. If I use classes then I don't have to have global variables right?
thanks for all of the help!!!!!!
...so do structs. There is no yada, yada, yada.Quote:
Originally Posted by andyhunter
Code:#include<iostream>
using namespace std;
struct A
{
int x;
};
struct B: public A
{
int y;
};
int main()
{
B b;
b.x = 10;
b.y = 20;
cout<<b.x<<endl<<b.y<<endl;
return 0;
}
Whoa, whoa, whoa. Look at that, and here I though the whole inheritance thing was brought about with C++. So the new C std does allow for inheritance.
I would hazard to guess that you shouldn't be using global variables at all as a beginner. Classes are much different than variables of type int, float, string, etc.Quote:
If I use classes then I don't have to have global variables right?
I posted a simple example of inheritance above. Basically, if one class inherits another class, it has all it's variables and functions.
I doubt it. Structs in C++ are classes with public access by default, i.e. they do everything classes do including member functions, constructors and inheritance. In C, they hold only variables/other structs.Quote:
So the new C std does allow for inheritance.