0

In a "has-a" class relationship does the contained class have to be implemented inside the class that contains it or can it be written entirely separately in a different file?

For example:

let's say:

class Pen
{
   public:
   .
   .
   .

   private:
      Ball point;
};

Can class Ball be in a separate header file or do I have to implement it within class Pen?

2 Answers2

1

Yes, Ball can be in it's own file:

Ball.h

class Ball
{
} // eo class Ball

Pen.h

#include "ball.h"

class Pen
{
private:
    Ball point;
} // eo clas Pen
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
1

It can be either way. If class Ball is not for use only within class Pen you should implement it separately - as a separate class in the same file or in another file.

Anyway C++ doesn't care how many files the implementations reside. Do as it feels convenient.

sharptooth
  • 167,383
  • 100
  • 513
  • 979