-
It doesn't really matter because in C++ a struct has all those nice features that a class has. It's really just a matter of opiniopn. The reason structs still exist is for backwards compatability.
Usually people use structs when they want to use the struct solely for holding data without functions (though you'll find that that rarely happens in practice because if you are storing data then you are using it for something.
-
The only reason I'm doing that is because I'm currently taking a course in C++ and the instructor requires this in a project! Personally I think using them together is not beneficial *shurg *(at this point)...but what do I know I've only been doing this for 3 months. Thanks for the help! MCorn
-
I stand corrected. I'm so used to looking at legacy C code (that's the only place I see structs anymore) that I just went with it. Good call guys.
-
Datainjector,
Classes are majorly different then structs. They appear the same but classes have the ability to contain their functions inside them, where structures do not. The entire point of a class is encapsulation or it's portability. If you are making outside refrences (as required by structures) you would be breaking that encapsulation right off the get.
hth :eek:
X
-
Sorry, Xaviar Khan. I repeat, the ONLY difference between structs and classes in C++ is the default member access.
Copy, compiler, and run the following program. If I haven't screwed up someplace it will run just fine, functions and all in the struct.
Code:
#include <iostream>
struct example
{
example(int);
void increment();
int getNum() const;
private:
int num;
}
example::example(int input) : num(input) {}
example::increment();
{
num += 24;
}
int example::getNum()
{
return num;
}
struct example2 : public example
{
void increment();
}
void example2::increment()
{
num += 1;
}
int main()
{
example ex(2);
cout << ex.getNum();
ex.increment();
cout << ex.getNum();
example2 ex2(2);
ex2.increment();
cout << ex2.getNum();
return 0;
}