This question came to my mind when I learned C++ with a background of C. Even if there was a struct
why did Stroustrup felt it was necessary to introduce the class
keyword? I tried asking people at that time but couldn't get a satisfactory answer. So can the Stack Overflow community answer it?
The class keyword is used to create a class. Every line of code that runs in Java must be inside a class. A class should always start with an uppercase first letter, and the name of the java file must match the class name. A class is like an object constructor.
C mostly uses functional/structural programming instead of implementing Object Oriented Programming as in languages like C++ , Java , Python etc. which use classes .
No,C does not have any class as it is procedural language(i.e,function based language) not object oriented language . Only object oriented programming language have classes like c++,java etc.
The class keyword is used to create a class called MyClass . The public keyword is an access specifier, which specifies that members (attributes and methods) of the class are accessible from outside the class.
As David says, structs are public by default, classes are private by default. The larger point is that adding object orientation to C was a big change, and giving developers ways to express themselves accurately is an important part of designing a language.
As it turns out, the distinction between struct and class is quite minor from a technical point (default-public vs default-private), but in programmers' minds, the distinction is quite large. Adding the keyword was an important way to emphasize the OO nature of C++.
In The Design and Evolution of C++, while describing how C++'s object model and virtual functions were developed, he writes (p. 76):
At this point, the object model becomes real in the sense that an object is more than the simple aggregation fo the data members of a class. An object of a C++ class with a virtual function is a fundamentally different beast from a simple C `struct`. Then why did I not at this point choose to make structs and classes different notions?
My intent was to have a single concept: a single set of layout rules, a single set of lookup rules, a single set of resolution rules, etc... I was convinced that if `struct` came to mean "C and compatibility" to users and `class` came to mean "C++ and advanced features," the community would fall into two distinct camps that would soon stop communicating. Beingn able to use as many or as few language features as needed when designing a class was an important idea to me. Only a single concept would support my ideas of a smooth and gradual transition from "traditional C-style programming," through class abstraction, to object-oriented programming. Only a single concept would support this notion of "you only pay for what you use" ideal.
So it sounds like the class
keyword was introduced to indicate C++-specific object orientation and then its compatibility with the struct
keyword was introduced.
EDIT:
Although the following speculation is plausible, the reason for the two keywords was probably practical in nature: by keeping the syntax and semantics of the struct backward compatible, it was possible to introduce C++ into existing programs easily (as opposed to say revisit all structs and add the keyword 'public' to them...).
[Speculation] The fact that me have two keywords, may possibly be associated with the genesis of the new languge, whereby initially the OO features were exclusively associated with the new keyword, "class". As this matured it was decided that it would be convenient to introduce some OO features to structs as well, and to keep these...
The struct is for fully or mostly transparent "objects" with no/little data hiding or behavior, in a continuation of its non-object-oriented use (although such transparent constructs have their place in the the broader context of OO programs). Whereby classes, were intended for introducing of data-hiding and other OO practices.
An alternative may have been to use the struct keyword for both usages, requiring programmers intending it in its 'class' sense to explicitly define the private members. At a time when OO concepts were not broadly understood in the programmers' community (cf other responses in this post) it was probably felt that a separate keyword would better help 'socialize' the new features/concepts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With