Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need a `class` in C++, when a `struct` can be used to achieve the same? [closed]

Tags:

c++

class

struct

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

Why do we need class then?

Note: I don't want the answer saying that in struct, fields/methods are public by default.

like image 881
someone Avatar asked Jul 29 '13 08:07

someone


3 Answers

You don't need classes, the language just gives you another option to choose from. Technically, you're right, you can achieve anything a class can do with a struct.

Besides the default access level, there's also the meaning most programmers associate with the two - struct generally means a light-weight, typically POD, data-type with little to no functionality. A class is usually associated with something bigger.

like image 146
Luchian Grigore Avatar answered Nov 10 '22 09:11

Luchian Grigore


As Tal Pressman answered at When should you use a class vs a struct in C++?:

From the C++ FAQ:

The members and base classes of a struct are public by default, while in class, they default to private. Note: you should make your base classes explicitly public, private, or protected, rather than relying on the defaults.

struct and class are otherwise functionally equivalent.

OK, enough of that squeaky clean techno talk. Emotionally, most developers make a strong distinction between a class and a struct. A struct simply feels like an open pile of bits with very little in the way of encapsulation or functionality. A class feels like a living and responsible member of society with intelligent services, a strong encapsulation barrier, and a well defined interface. Since that's the connotation most people already have, you should probably use the struct keyword if you have a class that has very few methods and has public data (such things do exist in well designed systems!), but otherwise you should probably use the class keyword.

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 structs. When Bjarne Stroustrup designed C++, he wanted to add classes with all the functionalities you listed in your original question, features which were not present in C. When implementing those features, he probably realised it didn't make sense to make two separate implementations for struct and class (except the public/private default visibility).

TL/DR: in C++ structs/classes describe the intent of the programmer to have POD or more complex abstractions, and the introduction of the class keyword is probably here for historical reasons (add an additional keyword in order to create featurefull classes, then backport those features into the struct keyword because it's more pragmatic to implement).

like image 31
Silex Avatar answered Nov 10 '22 08:11

Silex


class is simply the commonly accepted name in OO for a type used for instantiating objects. When introducing the OO paradigm in C++, it was deemed less surprising to use class instead of struct.

struct was kept to maximize backwards compatibility with C.

Today's usage of the two is in line with this : struct is most commonly used for C-style POD types, while class is used for the OO concept of classes.

like image 41
Sander De Dycker Avatar answered Nov 10 '22 07:11

Sander De Dycker