cpp
, but is usually invisibly invoked by the g++
/gcc
compiler scripts.
#include
directive
#define
directive
#define PI 3.1415 #define MAX(x, y) x > y ? x : y #define TWICE(x) x * 2 // should be (x) * 2 … try it out on TWICE(3+1)
#if
, #ifdef
, #ifndef
,
#else
, #endif
,
//#define OSX #define WIN32 … … … #ifdef WIN32 // Windows 32-specific code … #else // otherwise (OSX) … #endif
.h
file provides (for the most part) the interface
.cpp
file comprises the implementation
file.cpp
has a #include
of its own .h
File.h
and .cpp
files in sync
main.cpp
array.h
array.cpp
array.cpp
(compilerarray.cpp)
array.h
Array
) for scope resolution (Array::)
main.cpp
main.cpp
(compilermain.cpp)
array.h
Array
object, when main.cpp contains an Array
declaration
Array
when main.cpp
calls an Array
function
array.cpp
main.o
and array.o
.h
files, as well as .cpp
files need to see such information.
// file person.h #include "name.h" // see declaration ofname
below class person { public: … private: Name name; // compiler needs to see declaration of Name class — inname.h
… };
.c
files did the #include
#include
's
.h
's as well as .c
's) include whatever headers it required
.h
fileclass C { ... }; ... class C { ... };
This presents the following problem:
Student
class
Student
object is a list of courses
Vector
class
Vector
class' header file, vector.h
// file student.h ... #include "vector.h" // for the vector of courses below ... class Student { … private: … Vector courses(100); };
Student
objects and thus has a#include "student.h"
Student
objects) and thus has
its own #include vector.h
// file main.cpp ... #include "student.h" // We're a Student application #include "vector.h" // For the vector of students below ... Vector students(1000); ...
_H
#ifndef VECTOR_H // if VECTOR_H has not already been defined #define VECTOR_H // define it ... #endif