Class design question

A

away

If there are several equipments of the same type, but made by different
companies, and they are used for the same purpose, measuring temperature,
moisture, etc.

By "the same type" it means that their functionalities are very similar to
each other:

int ReadTemperature();
int ReadMoisture();
....

So far I see two options of designing classes for it:

Option 1:

Design a base class with:

virtual int ReadTemperature();
virtual int ReadMoisture();
....

Then derive each equipment from each company as a derived class.

Option 2:

Use template...

Can anyone point out what're advantages and disadvantages of either option?

Besides the two options above is there any other way to design the case?

In general, when should use template and when better use inheritance?

Thanks!
 
P

Phlip

away said:
Option 2:

Use template...

Can anyone point out what're advantages and disadvantages of either option?

Besides the two options above is there any other way to design the case?

In general, when should use template and when better use inheritance?

How do clients of these classes need them to appear? The information your
provided with the question "base class or template" is meaningless. You
can't pick a design without knowing what behaviors the servant classes have,
and what interface the client classes need.
 
D

Daniel T.

away said:
If there are several equipments of the same type, but made by different
companies, and they are used for the same purpose, measuring temperature,
moisture, etc.

Besides the two options above is there any other way to design the case?

Based on what little you have told us so far...

class Equipment {
string manufacturer;
public:
int ReadTemperature() const;
int ReadMoisture() const;
};
In general, when should use template and when better use inheritance?

I suggest you use template when the code must be able to work with types
that already exist, otherwise use inheritance.
 
J

JKop

away posted:
If there are several equipments of the same type, but made by different
companies, and they are used for the same purpose, measuring
temperature, moisture, etc.

By "the same type" it means that their functionalities are very similar
to each other:

int ReadTemperature();
int ReadMoisture();
...

So far I see two options of designing classes for it:

Option 1:

Design a base class with:

virtual int ReadTemperature();
virtual int ReadMoisture();
...

Then derive each equipment from each company as a derived class.

Option 2:

Use template...

Can anyone point out what're advantages and disadvantages of either
option?

Besides the two options above is there any other way to design the
case?

In general, when should use template and when better use inheritance?

Thanks!


Why not use both!...


Half-baked code:


template<const char* const t_parameter>
class Thermometer
{
private:

static const char* const product_name;

public:

virtual float ReadTemperature() = 0;

const char* GetProductName() const { return product_name; }
};

template<const char* const t_parameter>
const char* const Thermometer<t_parameter>::product_name = t_parameter;
//Not sure if a static variable is necessary, don't know if the
//member functions can work directly with "t_parameter"?

class Mitsubishi_Thermometer : public Thermometer<"Mitsubishi Thermometer">
{
public:

virtual float ReadTemperature()
{
return 37.5;
}
};


-JKop
 
A

away

JKop said:
away posted:



Why not use both!...


Half-baked code:


template<const char* const t_parameter>
class Thermometer
{
private:

static const char* const product_name;

public:

virtual float ReadTemperature() = 0;

const char* GetProductName() const { return product_name; }
};

template<const char* const t_parameter>
const char* const Thermometer<t_parameter>::product_name = t_parameter;
file://Not sure if a static variable is necessary, don't know if the
file://member functions can work directly with "t_parameter"?

class Mitsubishi_Thermometer : public Thermometer<"Mitsubishi Thermometer">
{
public:

virtual float ReadTemperature()
{
return 37.5;
}
};


-JKop

This is interesting, the code gives compiling error pointing at "public
Thermometer<"Mitsubishi Thermometer">" and its argument.

Could you identify it if it's a known pattern? Thanks!
 
J

JKop

This is interesting, the code gives compiling error
pointing at "public
Thermometer<"Mitsubishi Thermometer">" and its argument.

Could you identify it if it's a known pattern? Thanks!

....a known pattern? WWhhaa?


Anyway, here you go:


#include <iostream>

extern char const Mitsubishi_Thermometer_Text[] =
"Mitsubishi Thermometer";

template<const char* const product_name>
class Thermometer
{
public:

virtual float ReadTemperature() const = 0;

const char* GetProductName() const { return
product_name; }
};

class Mitsubishi_Thermometer : public Thermometer
<Mitsubishi_Thermometer_Text>
{
public:

virtual float ReadTemperature() const
{
return 37.5;
}
};

int main()
{
Mitsubishi_Thermometer blah;

std::cout << blah.GetProductName() << "\nTemperature:
" << blah.ReadTemperature();
}


-JKop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top