C++primer The book again under the groove, too much nonsense. If I were to translate, I would definitely cut a bunch of useless---just to complain.
Do not know whether the classic practice
A member declaration in a class is declared in a header file, which is defined (I prefer to call implementation) in the source file, which is used when the header file is imported.
However , it is not stated here that the source file needs to import the header file, and the header file does not need to import the source file !!!
As to why such an import could be executed successfully, I guess it was the compiler's credit-but not the book.
The code is as follows:
//header file of the person class, declaring the members of the class#ifndef Person_h#definePerson_h#include<iostream>#include<string>using namespacestd; // This is not recommended classPerson {Private: stringname; intAge ; Public: Person (); Person (Const string&_name); Person (Const string&_name,Const int&_age); Public: ~Person ();};#endif //Person_h
//the source file of the person class, BT AH#include"Person.h"using namespacestd; // This is not recommended Person::P Erson (): Name ("Anonymous"), Age ( -) {cout<<"default constructor called"<<Endl;} Person::P Erson (Const string&_name): Name (_name) {cout<<"Person (const string&) constructor called"<<Endl;} Person::P Erson (Const string&_name,Const int&_age): Name (_name), age (_age) {cout<<"Person (const string&, const int&) constructor called"<<Endl;} Person::~Person () {cout<<"before default destructor"<<Endl;}
// the use of the person Class!!! "person.h"<iostream>usingnamespace std; int Main (intChar *argv[]) { {person person;} " Hello world! " << Endl; return 0 ;}
The above example should be obvious, more concise than a bunch of examples on the web!!!
All kinds of resentment continue to drift over ~~~~~~
C + + class header file, implementation, usage