Library of Classes

Pages: 1234... 7
I like webJose's idea a little better but it is a little long.
Last edited on
If you were asking me and by functions you meant the prefixes then I would have them inherited from a base class. Since they modify the data the same way across the board we should just inherit them right?
A new class is also needed to create a compound unit.

1
2
3
4
5
6
7
8
9
10
11
12
13
class UnitContext //I don't have a good name for this class right now.
{
    //Member data.
    double m_exponent;
    Unit m_unit;
public:
    //Constructors
    UnitContext(double exponent, Unit const &unit) : m_exponent(exponent), m_unit(Unit)
    { }
    //Properties
    double GetExponent() { return m_exponent; }
    Unit GetUnit() { return m_unit; }
};


The above is not complete, but hopefully you get the idea: A class that takes a unit and an exponent. So you can say:

1
2
3
4
5
6
7
8
9
10
11
class AreaUnit : public CompoundUnit
{
    AreaUnit(Unit const &unit) : CompoundUnit(2, unit)
    { }
};

class SquareMeter : public AreaUnit
{
    SquareMeter() : AreaUnit(Meter)
    { }
};


For more complex types, like Speed:

1
2
3
4
5
6
7
8
9
10
class CompoundUnit : public Unit
{
    std::vector<UnitContext> _units;
public:
    double GetConversionFactor()
    {
        //The conversion of the compound unit is the multiplication of all the conversion factors
        //of the compound unit to their corresponding exponents.
    }
};


I think that works. What do you think?
Last edited on
We also could say (this is much easier for when we use functions):
1
2
3
4
5
6
7
8
9
10
11
template <distance>
class dist_meter
{
   public:
      double meter=1.0 ;
      bool ISU() {return true ;}
      double GCF() {return 1.0 ;}
} unit_std ;
double kilometer=unit_std.meter*1000 ;
double foot=unit_std.meter*0.348 ; //was this the conversion factor to feet?
//etc. 

I think this would be a lot easier to add functions later...

What is a vector?
You guys are headed towards templates. I am avoiding them. hehe.

Anyway, your choice. :-) I just presented some ideas here.
WHAT IS A VECTOR??????
lets use this
1
2
cout<<"Hello world!" <<endl ;
system("PAUSE") ;

heehee just kidding
A meter and a yard are almost equal, so yeah a foot would be about a third.

I would still like to see the prefixes declared as template functions, just because I feel like a hack when I retype the same code.

1
2
3
4
5
6
7
8
9
templace<class Unit>
class Prefix /*Inherit this to the Units classes*/
{
// CODE CODE CODE

      public:
           Unit Kilo(Unit unit) {return 1000 * unit;}
           Unit Mega(Unit unit) {return 1000000 * unit;}
//See What I mean? 

This way instead of the prefixes inheriting the units and the same code having to be typed for each one. We have the units inheriting the prefixes because they effect all of the units the same way.

Am I off base with something?
what is a vector though???
Ok, I re-read my previous-to-last post and I messed up. Correction:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class CompoundUnit : public Unit
{
    std::vector<UnitContext> _units;
public:
    double GetConversionFactor()
    {
        //The conversion of the compound unit is the multiplication of all the conversion factors
        //of the compound unit to their corresponding exponents.
    }
};

class AreaUnit : public CompoundUnit
{
    AreaUnit(Unit const &unit) : CompoundUnit()
    {
        _units.push_back(UnitContext(2, unit));
    }
};

class SquareMeter : public AreaUnit
{
    SquareMeter() : AreaUnit(Meter)
    { }
};


The reason I am running away from templates: Polymorphism, to me, is the better way to go because it comes a time when you have to group objects of different types under the same umbrella. Example: the _units vector in CompoundUnit. I believe you cannot do that with templated classes.
whats a vector? I need to know!!!
A vector is a template for a dynamically sized array. I have to be honest, I don't see why webJose is using it here but I've seen enough of his posts to believe he has a reason and it's peeked my interest.
We are practically nowhere. We haven't even decided how to start hardly.
A compound unit is any unit composed by 2 or more units of the same or different dimensions. Examples: Square meters because it is composed by m^1 * m^1; kilometers per hour because it is composed by km^1 * h^-1. See how I can express any compound dimension as a product of the dimensions to an X exponent? That's the magic worked by the UnitContext class above. The good: The conversion factor is automatic from the base units!!

The vector is used to hold all the units that composed the compound unit.
I like templates so heres a way we can go:
1
2
3
4
5
6
7
8
9
10
11
12
13
template <distance>
class dist_meter
{
   public:
      double meter=1.0 ;
      bool ISU() {return true ;}
      double GCF() {return 1.0 ;}
} unit_std ;
double kilometer=unit_std.meter*1000 ;
int convfactk=1000 ;
double foot=unit_std.meter*0.348 ; //was this the conversion factor to feet?
double convfactft=0.348 //still not sure if this was the factor for feet
//etc. then we can add some functions!!!! 

Last edited on
WHAT ABOUT FUNCTIONS???
My dev-C++ file is blank except for
1
2
#include <cstdlib>
#include <iostream> 
we need to get somewhere in this program
we need to get somewhere in this program
we need to get somewhere in this program
Last edited on
That's good. You must first design before you code. Whatever small code we have exchanged here is for discussion only.
Pages: 1234... 7