Composite classes

C

cmb99

I have a Symbol class, "MSFT". It has a bid and offer. "Dell" is also
an instance of such a class. Sometimes, though, I want to combine two
Symbol's into one, e.g. "MSFT" * "DELL", so that, I can do sym.bid()
and it returns the product of the two bids for an underlying
combination. How do I construct the class hierarchy?

class Symbol
{
public:
double bid() { return _bid; };
double offer();
private:
double _bid,
_offer;
};

ComplexSymbol : public Symbol
{
public:
ComplexSymbol();
~ComplexSymbol();
virtual double bid() {
return syms[0].bid() * syms[1].bid();
};
virtual double offer();
private:
double _bid,
_offer;
vector<Symbol> syms;
};

Or do I create a SimpleSymbol and ComplexSymbol all derived from an
ancestor? Basically I have objects which can have multiple parents.
 
S

Salt_Peter

I have a Symbol class, "MSFT". It has a bid and offer. "Dell" is also
an instance of such a class. Sometimes, though, I want to combine two
Symbol's into one, e.g. "MSFT" * "DELL", so that, I can do sym.bid()
and it returns the product of the two bids for an underlying
combination. How do I construct the class hierarchy?

class Symbol
{
public:
double bid() { return _bid; };
double offer();
private:
double _bid,
_offer;
};

ComplexSymbol : public Symbol
{
public:
ComplexSymbol();
~ComplexSymbol();
virtual double bid() {
return syms[0].bid() * syms[1].bid();
};
virtual double offer();
private:
double _bid,
_offer;
vector<Symbol> syms;
};

Or do I create a SimpleSymbol and ComplexSymbol all derived from an
ancestor? Basically I have objects which can have multiple parents.

Don't use underscores at the beginning of your member's names.
Always, always initialize your variables, specially containers.
Think about the implications of what happens if you multiply a bid =
0.0 by another bid (result is always 0.0). Are you sure you didn't mean
add bids together?
You have not explained what you need and the relationship between
classes is murky at best.

class Symbol
{
double bid_, offer_;
public:
Symbol(double b = 0.0, double of = 0.0)
: bid_(b), offer_(of) { }
// copy ctor ??
// assignment op ??
double bid() const { return bid_; }
double offer() const { return offer_; }
// op<<
friend std::eek:stream&
operator<<(std::eek:stream& os, const Symbol& r_s)
{
os << "bid = " << r_s.bid_;
os << "\toffer = " << r_s.offer_;
return os << std::endl;
}
};

You can implement a functor to get what you need in the case you need
to modify the elements in a container like the syms vector using an
algorithm:

// functor
template< typename S = Symbol >
struct Add
{
Add(const S& r_s) : s(r_s) { }
void operator()(S& r_symbol) const
{
r_symbol = S((s.bid() + r_symbol.bid()), s.offer());
}
private:
const S s;
};

class ComplexSymbol : public Symbol
{
std::vector< Symbol > syms;
public:
ComplexSymbol(size_t sz = 0) : syms(sz) { }
void add_each(const Symbol& r_symbol)
{
std::for_each( syms.begin(),
syms.end(),
Add< >(r_symbol) ); // using functor
}
// op<<
friend std::eek:stream&
operator<<(std::eek:stream& os, const ComplexSymbol& r_cs)
{
std::copy( r_cs.syms.begin(),
r_cs.syms.end(),
std::eek:stream_iterator< Symbol >(os) );
return os << std::endl;
}
};

int main()
{
ComplexSymbol complex(5);
std::cout << complex;

Symbol obj(1.1, 2.2);

complex.add_each( obj );
std::cout << complex;

complex.add_each( obj );
std::cout << complex;
}

/*
bid = 0 offer = 0
bid = 0 offer = 0
bid = 0 offer = 0
bid = 0 offer = 0
bid = 0 offer = 0

bid = 1.1 offer = 2.2
bid = 1.1 offer = 2.2
bid = 1.1 offer = 2.2
bid = 1.1 offer = 2.2
bid = 1.1 offer = 2.2

bid = 2.2 offer = 2.2
bid = 2.2 offer = 2.2
bid = 2.2 offer = 2.2
bid = 2.2 offer = 2.2
bid = 2.2 offer = 2.2
*/

Thats probably not what you want but hopefully its a start.
 
C

cmb99

Salt_Peter said:
I have a Symbol class, "MSFT". It has a bid and offer. "Dell" is also
an instance of such a class. Sometimes, though, I want to combine two
Symbol's into one, e.g. "MSFT" * "DELL", so that, I can do sym.bid()
and it returns the product of the two bids for an underlying
combination. How do I construct the class hierarchy?

class Symbol
{
public:
double bid() { return _bid; };
double offer();
private:
double _bid,
_offer;
};

ComplexSymbol : public Symbol
{
public:
ComplexSymbol();
~ComplexSymbol();
virtual double bid() {
return syms[0].bid() * syms[1].bid();
};
virtual double offer();
private:
double _bid,
_offer;
vector<Symbol> syms;
};

Or do I create a SimpleSymbol and ComplexSymbol all derived from an
ancestor? Basically I have objects which can have multiple parents.

Don't use underscores at the beginning of your member's names.
Always, always initialize your variables, specially containers.
Think about the implications of what happens if you multiply a bid =
0.0 by another bid (result is always 0.0). Are you sure you didn't mean
add bids together?
You have not explained what you need and the relationship between
classes is murky at best.

class Symbol
{
double bid_, offer_;
public:
Symbol(double b = 0.0, double of = 0.0)
: bid_(b), offer_(of) { }
// copy ctor ??
// assignment op ??
double bid() const { return bid_; }
double offer() const { return offer_; }
// op<<
friend std::eek:stream&
operator<<(std::eek:stream& os, const Symbol& r_s)
{
os << "bid = " << r_s.bid_;
os << "\toffer = " << r_s.offer_;
return os << std::endl;
}
};

You can implement a functor to get what you need in the case you need
to modify the elements in a container like the syms vector using an
algorithm:

// functor
template< typename S = Symbol >
struct Add
{
Add(const S& r_s) : s(r_s) { }
void operator()(S& r_symbol) const
{
r_symbol = S((s.bid() + r_symbol.bid()), s.offer());
}
private:
const S s;
};

class ComplexSymbol : public Symbol
{
std::vector< Symbol > syms;
public:
ComplexSymbol(size_t sz = 0) : syms(sz) { }
void add_each(const Symbol& r_symbol)
{
std::for_each( syms.begin(),
syms.end(),
Add< >(r_symbol) ); // using functor
}
// op<<
friend std::eek:stream&
operator<<(std::eek:stream& os, const ComplexSymbol& r_cs)
{
std::copy( r_cs.syms.begin(),
r_cs.syms.end(),
std::eek:stream_iterator< Symbol >(os) );
return os << std::endl;
}
};

int main()
{
ComplexSymbol complex(5);
std::cout << complex;

Symbol obj(1.1, 2.2);

complex.add_each( obj );
std::cout << complex;

complex.add_each( obj );
std::cout << complex;
}

/*
bid = 0 offer = 0
bid = 0 offer = 0
bid = 0 offer = 0
bid = 0 offer = 0
bid = 0 offer = 0

bid = 1.1 offer = 2.2
bid = 1.1 offer = 2.2
bid = 1.1 offer = 2.2
bid = 1.1 offer = 2.2
bid = 1.1 offer = 2.2

bid = 2.2 offer = 2.2
bid = 2.2 offer = 2.2
bid = 2.2 offer = 2.2
bid = 2.2 offer = 2.2
bid = 2.2 offer = 2.2
*/

Thats probably not what you want but hopefully its a start.

Ok. Your code's a little over my head. But I think we are thinking
along the same lines. Complex derived from basic, and a functor to
return the proper result. It gets even more interesting, as I don't
know ahead of time what formula they will enter into to create a
composite.

The key, though, is that only one instance of "MSFT" or "DELL" exist.
 
S

Salt_Peter

Salt_Peter said:
I have a Symbol class, "MSFT". It has a bid and offer. "Dell" is also
an instance of such a class. Sometimes, though, I want to combine two
Symbol's into one, e.g. "MSFT" * "DELL", so that, I can do sym.bid()
and it returns the product of the two bids for an underlying
combination. How do I construct the class hierarchy?

class Symbol
{
public:
double bid() { return _bid; };
double offer();
private:
double _bid,
_offer;
};

ComplexSymbol : public Symbol
{
public:
ComplexSymbol();
~ComplexSymbol();
virtual double bid() {
return syms[0].bid() * syms[1].bid();
};
virtual double offer();
private:
double _bid,
_offer;
vector<Symbol> syms;
};

Or do I create a SimpleSymbol and ComplexSymbol all derived from an
ancestor? Basically I have objects which can have multiple parents.

Don't use underscores at the beginning of your member's names.
Always, always initialize your variables, specially containers.
Think about the implications of what happens if you multiply a bid =
0.0 by another bid (result is always 0.0). Are you sure you didn't mean
add bids together?
You have not explained what you need and the relationship between
classes is murky at best.

class Symbol
{
double bid_, offer_;
public:
Symbol(double b = 0.0, double of = 0.0)
: bid_(b), offer_(of) { }
// copy ctor ??
// assignment op ??
double bid() const { return bid_; }
double offer() const { return offer_; }
// op<<
friend std::eek:stream&
operator<<(std::eek:stream& os, const Symbol& r_s)
{
os << "bid = " << r_s.bid_;
os << "\toffer = " << r_s.offer_;
return os << std::endl;
}
};

You can implement a functor to get what you need in the case you need
to modify the elements in a container like the syms vector using an
algorithm:

// functor
template< typename S = Symbol >
struct Add
{
Add(const S& r_s) : s(r_s) { }
void operator()(S& r_symbol) const
{
r_symbol = S((s.bid() + r_symbol.bid()), s.offer());
}
private:
const S s;
};

class ComplexSymbol : public Symbol
{
std::vector< Symbol > syms;
public:
ComplexSymbol(size_t sz = 0) : syms(sz) { }
void add_each(const Symbol& r_symbol)
{
std::for_each( syms.begin(),
syms.end(),
Add< >(r_symbol) ); // using functor
}
// op<<
friend std::eek:stream&
operator<<(std::eek:stream& os, const ComplexSymbol& r_cs)
{
std::copy( r_cs.syms.begin(),
r_cs.syms.end(),
std::eek:stream_iterator< Symbol >(os) );
return os << std::endl;
}
};

int main()
{
ComplexSymbol complex(5);
std::cout << complex;

Symbol obj(1.1, 2.2);

complex.add_each( obj );
std::cout << complex;

complex.add_each( obj );
std::cout << complex;
}

/*
bid = 0 offer = 0
bid = 0 offer = 0
bid = 0 offer = 0
bid = 0 offer = 0
bid = 0 offer = 0

bid = 1.1 offer = 2.2
bid = 1.1 offer = 2.2
bid = 1.1 offer = 2.2
bid = 1.1 offer = 2.2
bid = 1.1 offer = 2.2

bid = 2.2 offer = 2.2
bid = 2.2 offer = 2.2
bid = 2.2 offer = 2.2
bid = 2.2 offer = 2.2
bid = 2.2 offer = 2.2
*/

Thats probably not what you want but hopefully its a start.

Ok. Your code's a little over my head. But I think we are thinking
along the same lines. Complex derived from basic, and a functor to
return the proper result. It gets even more interesting, as I don't
know ahead of time what formula they will enter into to create a
composite.

The key, though, is that only one instance of "MSFT" or "DELL" exist.

The functor isn't returning the result. Its acting on each of the
elements in the container and dropping the result in the same
container. And since its a function-object (functor) is does it very,
very quickly.

Its too bad your looking at this with a complex project. If you take
out the op<< overloads, there really isn't much there.
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

I have a Symbol class, "MSFT". It has a bid and offer. "Dell" is also
an instance of such a class. Sometimes, though, I want to combine two
Symbol's into one, e.g. "MSFT" * "DELL", so that, I can do sym.bid()
and it returns the product of the two bids for an underlying
combination. How do I construct the class hierarchy?

class Symbol
{
public:
double bid() { return _bid; };
double offer();
private:
double _bid,
_offer;
};

ComplexSymbol : public Symbol
{
public:
ComplexSymbol();
~ComplexSymbol();
virtual double bid() {
return syms[0].bid() * syms[1].bid();
};
virtual double offer();
private:
double _bid,
_offer;
vector<Symbol> syms;
};

Or do I create a SimpleSymbol and ComplexSymbol all derived from an
ancestor? Basically I have objects which can have multiple parents.

Going the common ancestor route is one way. Often the language of the
problem domain helps you to work out what is going on too.

You talk about symbols and combining them with formula into a bid. I
guess that this is for financial transactions for share bids. It's
probable that you have seperate hierarchies for:

* Symbol - A class that holds a stock that might be traded. Might be
better called Instrument (as an abstract class) with various
sub-classes for different instruments. This class maybe only cares
about underlying financial data and the performance of the stock, and
which exchanges it is listed on etc.
* A bid - This reflects a concrete offer and encapsulates the price and
transaction knowledge.
* Complex/derivate - this allows other symbols or complexes to be
combined for the purpose of putting together a bid.

It is probable that you would want an abstract super-class (maybe
Instrument) that both symbol and complex derive from.

How exactly you go about this depends on how exactly you need to use it
and which operations can be done to form new instruments (futres/other
derivatives, indexes etc.) and how the bids are to be structured. You
may find that the bids themselves get turned into a seperate class
hierarchy to handle different common contract terms.

The thing here is that it may be simpler to seperate the knowledge of
the financial instrument in some way from the knowledge required to
construct a bid (or offer). This should help you to make each class a
lot more cohesive by splitting things up into smaller more manageable
chunks.


K
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,279
Latest member
LaRoseDermaBottle

Latest Threads

Top