// second_class.h
#ifndef SECOND_CLASS_H_INCLUDED_
#define SECOND_CLASS_H_INCLUDED_
// the second class has a non-static member of type first_class
// using in name will not suffice; we need the definition of first_class
#include "first_class.h"
struct second_class
{
int bar() const ;
void foo( int a ) ;
private: first_class fc ; // object of type first_class: non-static member
};
#endif // SECOND_CLASS_H_INCLUDED_
// first_class.cpp
#include "first_class.h" // include the header
int first_class::value() const { return val ; }
void first_class::value( int new_value ) { /* validate etc. */ val = new_value ; }
Implementation of second_class in second_class.cpp
1 2 3 4 5 6 7 8 9 10
// second_class.cpp
#include "second_class.h"
int second_class::bar() const { return fc.value() ; }
void second_class::foo( int a )
{
constint newv = bar() + a ;
fc.value( newv ) ;
}