A的头文件A.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#ifndef _A
#define _A
#include
"B.h"
class
A
{
private
:
int
a;
B objectb;
//A的头文件导入了B的头文件,在调用B的时候就可以不用指针
public
:
A();
int
geta();
void
handle();
};
#endif _A
|
B的头文件B.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#ifndef _B
#define _B
extern
class
A;
//注意1:需要用extern声明A
class
B
{
private
:
int
b;
A* objecta;
//注意2:调用A的时候需要用指针
public
:
B();
int
getb();
void
handle();
};
#endif _B
|
A的定义文件A.cpp:
|
#include <iostream>
#include
"A.h"
using namespace std;
A::A()
{
this
->a=
100
;
}
int
A::geta()
{
return
a;
}
void
A::handle()
{
cout<<
"in A , objectb.b="
<<objectb.getb()<<endl; pre=
""
<=
""
}=
""
><br>
}
B的定义文件B.cpp:
#include <iostream>
<strong>#include
"B.h"
#include
"A.h"
//注意3:在B.cpp里面导入A的头文件
using namespace std;
B::B()
{
this
->b=
200
;
}
int
B::getb()
{
return
b;
}
void
B::handle()
{
}
#include <iostream>
#include <cstdlib>
using namespace std;
void
main()
{
A a;
a.handle();
B b;
b.handle();
system(
"pause"
);
}
|