C + + class header file, implementation, usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.