C++多文件结构和预编译命令

  • C++的一般组织结构

1、一个工程可以被划分为多个源文件:类声明文件(.h文件)

类实现文件:(.cpp文件)

类的使用文件(main()所在的.cpp文件)

2、利用工程来组合各个文件

例如:

//文件1,类的定义,Point.h
class Point { //类的定义
public:          //外部接口
       Point(int x = 0, int y = 0) : x(x), y(y) { count++; }
       Point(const Point &p);
       ~Point() { count--; }
       int getX() const { return x; }
       int getY() const { return y; }
       static void showCount();          //静态函数成员

private:         //私有数据成员
       int x, y;
       static int count; //静态数据成员
};

//文件2,类的实现,Point.cpp
#include "Point.h"
#include 
using namespace std;
 
int Point::count = 0;            //使用类名初始化静态数据成员
Point::Point(const Point &p) : x(p.x), y(p.y) {
       count++;
}

 
void Point::showCount() {
      

你可能感兴趣的:(C++学习,C++的多文件结构和预编译)