I am reading programming principles and practice using c++ (Bjarne Stroustrup),
9.4.4 Defining member functions ,page 312, it says
1.
writing the definition of a member function within the class definition has two effects...
2.
Don't put member function bodies in the class declaration unless you know that ...
does the author write wrong? it talks about the same thing, why first sentence is "class definition" and second is "class declaration" ?
thanks
The syntax of a class definition:name_of_the_class is the name of the class and can be treated as the user defined data type. The pair of flower braces indicates the body of the class. The semicolon (;) after the right brace is must. The variables are declared inside and are called data members.
Classes are a blueprint for creating individual objects that contain the general characteristics of a defined object type. A modifier may or may not be used to declare a class.
A class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside the curly brackets and terminated by a semicolon at the end. Declaring Objects: When a class is defined, only the specification for the object is defined; no memory or storage is allocated.
A class is a group of objects that share common properties and behavior. For example, we can consider a car as a class that has characteristics like steering wheels, seats, brakes, etc. And its behavior is mobility.
does the author write wrong?
Not wrong, but imprecise. A class declaration can be a forward declaration, such as:
class X;
This just makes the compiler aware of the existence of that class, but does not specify what members the class has, what base classes, and so on (that's what a class definition does).
However, a declaration can also be a definition, such as:
class X
{
// ...
};
So in a sense, since a definition is also a declaration, the sentence is not wrong.
The sentence would be wrong under the common-sense assumption that by declaration we mean a declaration which is not a definition, but when dealing with such terms it is probably better to keep in mind their formal definition.
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