Still learning c++

Hi group
Im reading "Effective c++ by scott meyers" and in the book there is a section that says the following.

" C++ doesn't do a very good job of separating interfaces from implementations. A class definition specifies not only a class interface but also a fair number of implementation details. For example: "


class Person
{
public:

Person(const std::string& name);
std::string name() const;

.....
private:
std::string theName; // implementation detail
.....
};

Here, class Person can't be compiled without access to definitions for the classes the Person implementation uses. Such definitions are typically provided through #include directives, so in the file defining the Person class, you are likely to find something like this: more code no posted ....


So where im kinda confused is that i thought the stuff in the header file is called the "declaration, where things are defined" and the file that uses that header with a #include is either a "definition or aka the implementation file" so i guess a definition file meaning the header is what he is referring to? Also when one mentions interfaces im guessing that means the way you can access the objects data via the classes member function, member data and or possibly thru a non member function of the class. I always think of interfaces as a virtual method. Thanks for any answers and your time.


The interface is how the class presents itself from the outside, it's the public methods and members that the user will use.
name() belongs to the interface - it will return the object's name, which is all the user needs to know.
theName however is an implementation detail - it's a variable that stores the name. But name() could have generated the name from some other members, it could be a static string etc. In any case, that little piece of information doesn't concern the user and ideally, s/he shouldn't know about it. However, it's still visible in the class declaration - which is what the user needs to use a library.
so in paragraph before the code when the word definition is used this would mean the header file i take it? so a declaration file and a definition file are the same thing? for some reason i have always thought the .cpp file was the definition file. i know the
mplementation file is the .cpp file as well. thanks
Sounds like your question is about terminology. I'm not certain that I have the correct definitions (no pun intended). But I believe that something like
class Foo
{
// ...
};
is a "class definition". Most class definitions are found in header files. Notice that a class definition need only include *declarations* of the class's methods. The method definitions may also be included in the class definition, e.g.,
class Foo
{
int Bar (void)
{return 42;}
// ...
};
But it's common for method definitions to be found in an associated implementation (i.e., *.cpp) file.

Hope this helps.
Topic archived. No new replies allowed.