C
crea
Lets say we are programming a wheather data analysing and display program
(of course using VC!).
We have already classes:
- CChart, which draws wheather data
- CWheather, which contains wheather data and all the functions regarding
analysing data. So this data (it contains the data) can be passed to CChart
to draw it.
Now we want combine these two to draw a chart with data in it. Is it better
to create a new class which inherits from them like this:
1)
class CWheatherChart : public CChart, public CWheather
{
....
};
or create a class which inludes them as members like this:
2)
class CWheatherChart
{
CChart m_Chart;
CWheather m_Wheather;
....
};
(The point is, that later on we can create specific classes for example to
moon-wheather , earth wheather, summer wheather... etc. which are inherited
from CWheatherChart:
class CMoonWheatherChart : public CWheatherChart)
These two classes must communicate with eatch others sometimes as doing
steps, like when the chart draws a certain data value (x,y) it might ask
something from CWheather class in the middle of its drawing. So using the
design 1) we could use virtual functions to handle this (having virtual
function for data draw in CChart and then have also it on CWheatherChart ).
Which way is better? I guess 1) makes things a bit easier to program (can
use virtual functions to communicate between CChart and CWheather ), but on
the other hand 2) gives possibility to change easily chart or data on the
fly.
(of course using VC!).
We have already classes:
- CChart, which draws wheather data
- CWheather, which contains wheather data and all the functions regarding
analysing data. So this data (it contains the data) can be passed to CChart
to draw it.
Now we want combine these two to draw a chart with data in it. Is it better
to create a new class which inherits from them like this:
1)
class CWheatherChart : public CChart, public CWheather
{
....
};
or create a class which inludes them as members like this:
2)
class CWheatherChart
{
CChart m_Chart;
CWheather m_Wheather;
....
};
(The point is, that later on we can create specific classes for example to
moon-wheather , earth wheather, summer wheather... etc. which are inherited
from CWheatherChart:
class CMoonWheatherChart : public CWheatherChart)
These two classes must communicate with eatch others sometimes as doing
steps, like when the chart draws a certain data value (x,y) it might ask
something from CWheather class in the middle of its drawing. So using the
design 1) we could use virtual functions to handle this (having virtual
function for data draw in CChart and then have also it on CWheatherChart ).
Which way is better? I guess 1) makes things a bit easier to program (can
use virtual functions to communicate between CChart and CWheather ), but on
the other hand 2) gives possibility to change easily chart or data on the
fly.