The design habit of C + + class and the solution of the problem of duplication of header files

Source: Internet
Author: User
Tags definition header sleep

When designing a class, the definition of the class and the declaration of the class member function are usually placed in the header file (that is, the. h file), and the implementation of the member function in the class is placed in the source file (that is,. cpp). For the animal class, the animal.h and animal.cpp two files are required, as are fish.h and fish.cpp for fish classes. For the main () function, we put it separately in the EX10.cpp file.

There are two ways to add a header file (. h file) or source (. cpp) file to an existing project: In an open project, click "File" → "New", and in the Files tab on the left, select C + + header file or C + + source file. Then, in the File text box on the right, enter the filename of the header or source file, such as Animal.h or Animal.cpp, and click the OK button. As shown in Figure 2.16.

Figure 2.16 New header file or source file

Another way is in the EX10 Engineering directory, right-click, select new → text document, and then rename "new text document. txt" to "animal.h" (Because. h and. cpp files are text-formatted files), in the same way, establish animal.cpp, FISH.H, fish.cpp three files, then in the Open Project, select "Project" → "Add to Project" → "Files", select Animal.h, Animal.cpp, FISH.H, fish.cpp These four files, click "OK" button, as shown in Figure 2.17.

Figure 2.17 Adding header files and source files to the project

The code is shown in example 2-24.

Example 2-24

Animal.h
To include the definition of a class and the declaration of a class member function in a header file
Class Animal
{
Public
Animal ();
~animal ();
void Eat ();
void Sleep ();
virtual void breathe ();
};
Animal.cpp
To include 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 what animal is, so include animal.h so that the compiler knows that animal is a type of class
#include <iostream.h>//What is the difference between,<> and "" When the header file is included? <> and "" indicates that the compiler searched the header file in a different order,<> means to start the search from the system directory and then search the directory listed by the PATH environment variable, without searching the current directory;
"" is to indicate that the directory is first searched from the current directory, then the system directory and the PATH environment variable. So if we know that the header file can be used directly in the system directory, it will speed up the search <&gt.
Animal::animal ()//:: Called scope identifier, used to indicate which class A function belongs to, or which class the data member belongs to.
:: Front if not with class name, representing is global
{function (that is, not a member function) or global data
}
Animal::~animal ()
{
}
void Animal::eat ()//NOTE: Although we did not write anything in the body of the function, we still implemented the function
{
}
void Animal::sleep ()
{
}
void Animal::breathe ()//Note that when virtual is added to the header file (. h file), there is no need to add virtual to the source file (. cpp file).
{
cout<< "Animal Breathe" <<endl;
}
Fish.h
#include "animal.h"//Because the fish class inherits from the animal class, to let the compiler know that animal is a type of class, it is necessary to include the Animal.h header file
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);
}

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.