Used

xiaoxiao2021-03-06  62

How to use a defined class when several classes are defined in different files?

When I started, I am in different files in different files.

Then use the place where this class is used, and the result is analogously, very

Multiple errors. Later, I asked my colleagues, my colleague said that I want to put the statement of the class in a first text.

In the pieces, then implement it in the class file. When using it,

head File. I made a Demo as follows:

Suppose this demo has two classes: A, B, defined in classa.cpp and classb.cpp

In, there is a main.cpp, used to call A, B, two classes, a MyHeader.h,

To store a statement of A and B. The source code is as follows:

1.myHeader.h:

Class A

{

PUBLIC: A () {}

Void print ();

}

Class B

{

PUBLIC: B () {}

Void print ();

}

2.classa.cpp

#include #include "MyHeader.h" Void A :: Print () {cout << "I am New Object of class a!" << endl;}

3.classb.cpp

#include #include "MyHeader.h" Void B :: Print () {cout << "I am New Object of Class B! << endl;} 4.main.cpp

#include "myHeader.h" main () {a * a = new a (); b * b = new b (); A-> print (); b-> print (); return 0;} The execution result is as follows :

转载请注明原文地址:https://www.9cbs.com/read-111521.html

New Post(0)