开发者

In a "has-a" class relationship does the contained class have to be implemented inside the class . .

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 im开发者_如何学Cplement it within class Pen?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜