Library of Classes

Pages: 1... 567
Heehee. Did what you guys said. Here's what I decided to make out of this.
So far I've Got:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdlib>
#include <iostream>

class unit //base class for all other classes
{
      public:
             bool stdunit() {return false;}
             double convfact() {return 1.0;}
};

class prefix : public unit
{
      public:
             class kilo : public unit
             {
                    bool stdunit() {return false;}
                    double convfact() {return 1000.0;}
             };
Close. Your unit class need to declare stdunit() and convfact() as virtual methods, and then you override in prefix.

Check out the classes tutorial @ this site.
Ok, another update:

https://ideone.com/XCVkX

This one is much more improved and there were some changes. I'll name some:

1. Unit is no longer the base class. FactorBase is now the base class for Unit and for Prefix.
2. I think I got resolved the whole unit + memory allocation thing.
3. The class Unit provide a member variable m_prefix of type Prefix* that points to a prefix object. All prefixes are singletons and cannot be created (private constructor), so all pointers point to const objects in static variables. I did have to use a const_cast<> here so I can change the prefix at any given time. :-(
4. The class Unit now overrides GetSymbol() from FactorBase to provide prefix symbol + class symbol. Class symbol comes from a protected virtual function called GetSymbolNoPrefix().
5. I was able to implement a global operator<< for all dimension objects! Thanks to CreativeMFS.
6. Added virtual comparison operators to Dimension<T>, the base for all dimensions.

As it currently is, my design will allow to:

1. Create compound units from mathematical operations * and / once I provide those operators to the Unit class. This means I will be able to divide length by time and get speed, or mass times acceleration to get force!! :-)
2. Create a UserDefinedUnit class so users of the library can define their own units like "apples", "oranges", etc. and be able to sum apples with apples but not with oranges! This class will also allow the developer to create a base unit class like FruitUnit should he/she decides it is ok to sum apples with oranges.

Also, related thread: http://www.cplusplus.com/forum/general/42889/.
Nice! I am still working on a solution which treats different units as (immutable) objects of the same type. The code for mine is shorter (if not quite finished), but I think it will lack some of the more elegant details which yours has, especially allowing custom user units which are not based on Mass, Length and Time. :)
I don't know if people are still working on this one, but I just came across a C++0x feature which would be pretty cool for it.

"User Defined Literals"
http://www2.research.att.com/~bs/C++0xFAQ.html#UD-literals
Hello Xander. I am still working on it, but time constraints prevent me from moving forward too fast. I am decided to host the open source project in www.codeplex.com because I don't need yet another username/password (I can use my Live ID), and I can also use Subversion. I offered people willing to join to PM me their Live ID's, but I got nothing, so I assume there is nobody willing to be collaborator.

As for your finding: That's awesome! Thanks for sharing. I guess I'll need to know the macro that defines a C++0x compiler to conditionally include that functionality. If it is defined already, please let me know. :D
Unlucky about that :/ I would offer to help but I'd probably only get in the way as I have loads of other stuff on so I never have much time...

Unfortunately, now I've actually had the chance to try it, it seems that is one of the features not supported by Visual Studio. Didn't seem to work in Code::Blocks either, and my MinGW is fairly up to date.
Topic archived. No new replies allowed.
Pages: 1... 567