Go VC + + Class header file

Source: Internet
Author: User

This article transferred from: http://blog.csdn.net/forevertali/article/details/4370602

Animal.h

Include the definition of the class and the declaration of the class member function in the header file

Class Animal

{

Public

Animal ();

~animal ();

void Eat ();

void Sleep ();

virtual void breathe ();

};

Animal.cpp

Contains the implementation of a member function in a class in a source file

#include "animal.h"//Because when compiling animal.cpp, the compiler does not know animal exactly

is what, so to include animal.h, so that the compiler knows animal

Is the type of a class

#include <iostream.h>//What is the difference between,<> and "" When you include a header file? <> and "" represents the compiler

The order in which the header files are searched is different,<> means to start the search from the system directory,

Then search the directory listed in the PATH environment variable, do not search the current directory;

"" is the first search from the current directory, then the system directory and the PATH environment

The directory to which the variable is listed. So if we know the header file is in the system directory

You can use <> directly, so you can speed up your search.

Animal::animal ()//:: Called a scope identifier that indicates which class or a function belongs to

The number of Members belonging to which class. :: Front if not with class name, the representation is global

{function (that is, non-member function) or global data

}

Animal::~animal ()

{

}

void Animal::eat ()//NOTE: Although we did not write anything in the function body, it was still implemented

This function

{

}

void Animal::sleep ()

{

}

void Animal::breathe ()//note that after adding virtual in the header file (. h file), the source text

(. cpp file), you don't have to add virtual

{

cout<< "Animal Breathe" <<endl;

}

Fish.h

#include "animal.h"//Because the fish class inherits from the animal class, let the compiler know

Animal is a type of class that will contain animal.h header files

Class Fish:public Animal

{

Public

void Breathe ();

};

Fish.cpp

#include "fish.h"

#include <iostream.h>

void Fish::breathe ()

{

cout<< "Fish Bubble" <<endl;

}

EX10.cpp

#include "animal.h"

#include "fish.h"

VOID fn (Animal *pan)

{

Pan->breathe ();

}

void Main ()

{

Animal *pan;

Fish FH;

pan=&fh;

FN (pAn);

}

Now we can press the F7 function key on the keyboard to compile the whole project, and compile the result as follows:

Why do you get a class-repeating definition error? Please read the EX10.cpp file carefully and include the two header files for animal.h and fish.h in this file. When the compiler compiles the EX10.cpp file, because the Animal.h header file is included in the file, the compiler expands the header file, knows that the animal class is defined, and then expands the FISH.H header file, and FISH.H is included in the Animal.h header file, and the animal.h is expanded again, so an iMAL This class is defined repeatedly.

The reader can test that if we include the iostream.h header file multiple times, there will be no error above. To resolve the problem that the header file contains repeatedly, you can use conditional preprocessing directives. The modified header file is as follows:

Animal.h

#ifndef Animal_h_h//We generally define a macro with a # # to be used in a program so that the process

Simpler and easier to maintain, but here we're just trying to determine

#define ANIMAL_H_H Animal_h_h is defined to avoid class duplication, so we do not have a

It defines a specific value. When choosing a macro name, use some infrequently used names,

Class animal because our programs are often integrated with programs written by others, if you choose a very common

(For example: X), it may cause some unnecessary errors

{

Public

Animal ();

~animal ();

void Eat ();

void Sleep ();

virtual void breathe ();

};

#endif

Fish.h

#include "animal.h"

#ifndef Fish_h_h

#define Fish_h_h

Class Fish:public Animal

{

Public

void Breathe ();

};

#endif

Let's look at the EX10.cpp compilation process. When the compiler expands the animal.h header file, the conditional preprocessing directive determines that animal_h_h is undefined, then defines it and then proceeds to define the animal class And then expand the FISH.H header file, and in the FISH.H header file also contains animal.h, again expand Animal.h, this time the conditional preprocessing instructions found Animal_h_h has been defined, then jump to #endif, execution end.

Through analysis, we found that in this compilation process, the animal class was defined only once.

Go VC + + Class header file

Related Article

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.