Why do we need a “class” in C ++ when “struct” can be used to achieve the same?

Using struct , we can achieve all the functionality of class constructors: (which can be changed / overloaded), destructors (which can be changed / overloaded), operator overloads, instance methods, static methods, public / private / protected fields / methods.

Why do we need a class then?

Note. I do not need an answer saying that in the struct fields / methods the default is public .

+6
source share
5 answers

You don't need classes, the language just gives you one more choice. Technically, you're right, you can achieve everything that a class can do with struct .

Besides the default access level, there is also a meaning that most programmers associate with two - struct , as a rule, means a lightweight, usually POD , data type with almost no functionality. A class usually associated with something more.

+8
source

As Tal Pressman said in When should you use the vs struct class in C ++? :

From the C ++ FAQ:

The members and base classes of the structure are public by default, and in the class they are private by default. Note. You should make your base classes explicitly public, private, or protected, rather than relying on the default values.

struct and class are otherwise functionally equivalent.

Well, enough of this creaky clean techno talk. Emotionally, most developers make a strong distinction between class and structure. The structure just looks like an open bunch of bits with very little encapsulation or functionality. The class feels like a living and responsible member of a community with intelligent services, a strong encapsulation barrier and a well-defined interface. Since most people already have it, you should probably use the struct keyword if you have a class that has very few methods and has public data (such things exist in well-designed systems!), But otherwise you should probably use the keyword class.

http://www.parashift.com/c++-faq-lite/classes-and-objects.html#faq-7.8

I think one addition to this reason could be that C already had structures. When Bjarne Stroustrup developed C ++, he wanted to add classes with all the functionality you listed in the original question, functions that aren't in C. When implementing these functions, he probably realized that it makes no sense to make two separate implementations for the struct and class (except public / private default).

TL / DR: in C ++, structures / classes describe the programmer’s intention to have POD or more complex abstractions, and introducing a class keyword is possible here for historical reasons (add an additional keyword to create functional classes, then run these functions in the struct keyword, because it’s more pragmatic to implement).

+3
source

In short, class was not really required. It changes the defaults to those that are possibly safer and more applicable to OO programming, but you can use struct (as defined by C ++) for any place you currently use class (if you want to get cute and meta about it, you can call struct base class class that satisfies LSP).

At the same time, the misunderstanding of struct in C ++ is inexorable, and class quite suitable for the proposed concept, which is much easier to explain. New users often seem to find this at least slightly more understandable - a reasonable goal in itself.

+1
source

class is simply the common name in OO for the type used to create objects. When introducing the OO paradigm in C ++, it was considered less unexpected to use class instead of struct .

struct is stored for maximum backward compatibility with C.

Today, the use of these two corresponds to this: struct most often used for POD types of type C, while class used for the concept of OO classes.

+1
source

There is no such difference between a C ++ struct and a C ++ class, you can perform almost all functions with a structure, as you can do with a class, but struct is the C keyword, which is gradually modified / developed in C ++ and is called class. And we are in C ++, it is better to use a class rather than a struct.

Let’s take an example if you have done some coding in C ++, and someone who works in Java came in 2 months to look at your code, and how convenient it is to understand the code using "struct" or code with "class"?

0
source

Source: https://habr.com/ru/post/950503/


All Articles