What's the difference between a struct and a class ?
http://mahurshi.tripod.com/mainframes.htm
What's the difference between a struct and a class ?
http://mahurshi.tripod.com/mainframes.htm
class members are private by default and struct members are public by default.
Classes can also be put in a hierchy so classses that are made a public to other classes will inherite their varibles and functions.
I am a newbie...
Sorry, I couldn't understand what you're saying..
could you please explain in detailed ?
when is it advantageous to use classes or structs >?
http://mahurshi.tripod.com/mainframes.htm
And so can structs:Classes can also be put in a hierchy so classses that are made a public to other classes will inherite their varibles and functions.
Classes are preferred because the private variables provide encapsulation. Then you use methods for accessing variables.Code:#include <iostream> struct Mouse{ int tail; }; struct Computermouse : public Mouse { int prize; }; int main (){ Computermouse mouse; mouse.tail=10; mouse.prize=130; std::cout<<mouse.tail<<" "<<mouse.prize<<std::endl; return 0; }
with structs, you would have to replace mycar.SpeedUp() with the actual code to speed up the 'car', and it wouldn't be in a nice neat little package.By Quzah
Then you do something like:Code:class Car { public: SpeedUp( ) { engine.Accelerate( ); } SlowDown( ) { engine.Decelerate( ); } Crash( ) { engine.GrindingHalt( ); wheels.FallOff( ); body.BendFrame( ); } private: class Engine engine; class Wheels wheels; class Body body; };
Car mycar;
mycar.SpeedUp( );
mycar.SpeedUp( );
mycar.SpeedUp( );
mycar.SpeedUp( );
mycar.Crash( );
Following code compiles fine:
...which implies that you are wrong.Code:#include <iostream> struct Engine{ private: int speed; public: void Accelerate(){speed++;} void Decelerate(){speed--;} void GrindingHalt(){int foo, bar;foo=bar=0;} }; struct Wheels{ private: bool on; public: void FallOff(){ on=false; } }; struct Body { private: bool foo; public: void BendFrame(){foo=false;std::cout<<"Frame foo"<<std::endl;} }; struct Car { public: void SpeedUp( ) { engine.Accelerate( ); } void SlowDown( ) { engine.Decelerate( ); } void Crash( ) { engine.GrindingHalt( ); wheels.FallOff( ); body.BendFrame( ); } private: Engine engine; Wheels wheels; Body body; }; int main(){ Car mycar; mycar.SpeedUp(); mycar.SpeedUp(); mycar.SpeedUp(); mycar.SpeedUp(); mycar.Crash(); return 0; }
Can you guys make it simple please ?? I am totally confused now..
> class members are private by default and struct members are public by default.
This is correct. In fact, in C++ classes and struct are EXACTLY the same except for that members of structs are public by default.
This means, in:
struct Test
{
int a;
};
the integer 'a' member is public. I.e. I could write:
Test tl
t.a = 5;
If I were to write:
class Test
{
int a;
};
The a member is private, i.e. it cannot be accessed outside the class. When it is said that it is private by default, it means that the 'visibility' of 'a' has not been specified, and therefore defaulted to private.
To write a class which is equivilent to the struct example above, I would need to write:
class Test
{
public:
int a;
};
Here I have explicitly stated that int a should be public.
Simply the members in class could have one of 3 kind of properties. They are public, private and protected.Originally posted by Unregistered
Can you guys make it simple please ?? I am totally confused now..
Struc acts just as the class that all of its members with public property.
This is what I remembered from my book.
please let me know if there is anything wrong.
Never end on learning~
thast probably wrong but if it were corrected would result in something like "1234", however if they were classes not structs, you would get an error saying sommat like i and j were undefined/private etc.Code:struct somestruct { int i = 1; int j = 3; } struct somestruct :: anotherstruct { int k = 2; int l = 4; anotherstruct { cout << i << k << j << l << "."; }
[D3T]
Borland Turbo C++ 3.0 For Dos
*
do { war(); } while (nations == hostile);
... The best way to prevent wars is to have them.
No, you can use words public, private and protected also in structs and they work in exactly the same way than in classes.Struc acts just as the class that all of its members with public property.
This implies that the following mean the same:
class A_class{int variable1;};
struct A_class{private: int variable1;};
I never knew you could put different properties to structs (private, public, protected), I always thought only classes could. I guess we learn something everyday.
And how many years did it take for you to learn that ?
How old are you now ?
well I've been programming in various languages since I was 7, and I'm going to be 14 in 3 days.
This is just one of those small things sometimes you don't ever learn until later, and you wish you would've learned it earlier.