一:组件
组件:一个组件恰好由一个.h和一个.c文件组成,组件是自我包含的、内聚的、潜在可重用的设计单位。
各自的作用?
以此为基础,研究内外连接,以规范平时写代码的规范。
二:思考
思考 static extern的区别?内连接、外链接。
内外连接的判断标准。
检测:外部定义一样的东西,看看是否冲突。
三:举例
1:数据
2:函数
(1)见程序
(2)项目中的const文件
抢答:
不同的.c可以定义相同的类名吗?
同类名的类中有同样的函数行吗?
程序如下:
CA.h
#ifndef CA_H
#define CA_H
class CA
{
public:
CA();
private:
//void fun();
};
#endif // CA_H
CA.cpp
#include "CA.h"
#include "Const.h"
// (1)普通数据 外
//int n = 1;
// (2)枚举 内
//enum eType
//{
// e1 = 1,
// e2 = 2
//};
// (3) typedef 内
//typedef int MyInt;
// (4) define 内
//#define MAX 100;
// (5) struct/class 内----
//struct MyStruct
//{
// int a;
//};
//class MyClass
//{
// int a;
//};
// (6)const 内
//const int a = 0;
// (7)static 内
//static int a =0;
//class C
//{
//public:
// //C():a(0) { } // 常量外部初始化不行
//private:
// // (1)普通数据 内
// //int n;
// // (2)枚举 内
enum eType
{
e1 = 1,
e2 = 2
};
// // (3) typedef 内
// //typedef int MyInt;
// // (4) define 内
// //#define MAX 100;
// // (5) struct/class 内
struct MyStruct
{
int a;
};
class MyClass
{
int a;
};
// // (6)const 内
// //const int a;
// // (7)static 外-----
// //static int a;
//};
int C::a = 1;
// (1)普通函数 外
//void fun() { }
// (2)inline普通函数 内
//inline void fun() { }
// (3)static普通函数 内
//static void fun() { }
CA::CA()
{
}
(4)普通成员函数 内???? 错
//void CA::fun()
//{
//}
//class CC
//{
//private:
// // (4)普通成员函数 外
// //void fun();
// // (5)inline成员函数 内
void fun1() { }
inline void fun2();
void fun3();
// // (6)static成员函数 外----
// //static void fun();
//};
void CC::fun() { }
void CC::fun2() { }
inline void CC::fun3() { }
void CC::fun() { }
CB.h
#ifndef CB_H
#define CB_H
class CB
{
public:
CB();
private:
//void fun();
};
#endif // CB_H
CB.cpp
#include "CB.h"
#include "Const.h"
// (1)普通数据 外
// int n = 1;
// (2)枚举 内
//enum eType
//{
// e1 = 1,
// e2 = 2
//};
// (3) typedef 内
//typedef int MyInt;
// (4) define 内
//#define MAX 100;
// (5) struct/class 内----
//struct MyStruct
//{
// int a;
//};
//class MyClass
//{
// int a;
//};
// (6)const 内
//const int a = 0;
// (7)static 内
//static int a =0;
//class C
//{
//public:
// //C():a(0) { } // 常量外部初始化不行
//private:
// // (1)普通数据 内
// //int n;
// // (2)枚举 内
enum eType
{
e1 = 1,
e2 = 2
};
// // (3) typedef 内
// //typedef int MyInt;
// // (4) define 内
// //#define MAX 100;
// // (5) struct/class 内
struct MyStruct
{
int a;
};
class MyClass
{
int a;
};
// // (6)const 内
// //const int a;
// // (7)static 外----
// //static int a;
//};
int C::a = 1;
// (1)普通函数 外
//void fun() { }
// (2)inline普通函数 内
//inline void fun() { }
// (3)static普通函数 内
//static void fun() { }
CB::CB()
{
}
(4)普通成员函数1 内????
//void CB::fun()
//{
//}
//class CC
//{
//private:
// // (4)普通成员函数 外
// //void fun();
// // (5)inline成员函数 内
void fun1() { }
inline void fun2();
void fun3();
// // (6)static成员函数 外----
// //static void fun();
//};
void CC::fun() { }
void CC::fun2() { }
inline void CC::fun3() { }
void CC::fun() { }
main.cpp
#include "CB.h"
#include "CA.h"
using namespace std;
int main()
{
CA a;
CB b;
return 0;
}
注意,.h中如果在类外直接定义成员函数,要加inline,否则虽然类不同,函数相同,在.h中定义也会引起 连接错误(冲突问题),.h中定义成员函数必须用inline才行