Hello! I got one quick question. For what tasks will I use classes? Why classes and not structures?
Please give me example, where I can solve the task only with classes and not structures.
struct and class are identical in every way, except that when declaring methods and/or data members in a class, by default, they are treated as private members of the class. In a structure, by default, all methods and/or data are public.
Naturally, you can change the permission levels of methods and data members using the appropriate label of "public:", "protected:", or "private:".
Many s/w developers that program in C++ tend to use a class vs. a struct for modeling their objects. When only data members need to be encapsulated is a struct used. But that is just based on developer's preference.
Thanks for the posts. And can I ask you what will I need the private: for? Is there any specific reason?
Thank you.
Mostly to ensure the safety and proper use of your class. As a really simple example, say your class has a data member that can only store a value between 1 and 10. Making that member private ensures that other users of that class can't set an inappropriate value. You'd make a method to set the value public and have it reject inappropriate values.
Think of it this way. You've got this big, complicated algorithm. It's frankly ugly, and relies on all sorts of assumptions about the input data. Trying to incorporate something like that into a larger bit of code would be pretty daunting-----who knows what might break it.
But, if you can make all that functionality class-private, and throw a simple, clean, easy-to-understand interface on the class.....suddenly all the messiness is hidden from the rest of the code. It's abstracted away, and all you have to worry about is validating arguments to the interface. It makes everything a lot easier.
Most of the people talk differentiate classes and structure on basis of setting varriables etc as public or private by default.
the thing here to understand is that procedures can be calles , used whatever in class but it can't be used in structures.
Representation of data might seem easy in structure but when it comes to utilizing there are lot of limitations and use of classes can really help you alot in the long run
Most of the people talk differentiate classes and structure on basis of setting varriables etc as public or private by default.
the thing here to understand is that procedures can be calles , used whatever in class but it can't be used in structures.
Representation of data might seem easy in structure but when it comes to utilizing there are lot of limitations and use of classes can really help you alot in the long run
What are you talking about? There's nothing* that the struct class-key can't do when compared to the class class-key.
*The only exception, of course, is the special treatment of the class keyword when it comes to template parameters.
I think you understood OP's confusion but not what's been said here.
I do too think that when the OP asked "why structure" in place of "class",
he was actually thinking about the struct in C, and not the struct in C++
In C++, the keyword struct and class are used interchangeably,
the difference of which is what's been said by Plasmator and others.
For example, some use the keyword struct for POD type,
so it really is a matter of perference.
You could do a simple foo class using struct and compile with C++ compiler (not C compiler).
to see this for yourself.
Bookmarks