Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class definition and class declaration

Tags:

c++

class

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

like image 231
booirror Avatar asked Jun 02 '13 13:06

booirror


People also ask

What is class definition and class declaration with syntax and example?

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.

What is class declaration and definition in Java?

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.

What is class declaration and definition in C++?

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.

What is class define with example?

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.


1 Answers

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.

like image 92
Andy Prowl Avatar answered Oct 22 '22 20:10

Andy Prowl