1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| #include <string>
#include <iostream>
#include <vector>
class Value
{
int _value ;
std::string _name ;
public:
Value(const std::string& name, int v);
const std::string& getName();
int get();
};
Value::Value(const std::string& name, int v) : _name(name), _value(v) {}
int Value::get() { return _value; }
const std::string& Value::getName() { return _name; }
class Container
{
std::vector<Value*> _values ;
public:
friend std::ostream& operator <<(std::ostream &os,const Container &obj) ;
void add(Value* v);
};
std::ostream& operator <<(std::ostream &os,const Container &obj)
{
std::vector<Value*>::const_iterator it = obj._values.begin() ;
for ( ; it != obj._values.end(); it++ )
os << (*it)->getName() << " : " << (*it)->get() << std::endl ;
return os;
}
void Container::add(Value* v)
{
_values.push_back( v );
}
class Test : public Container
{
Value _v;
public:
Test();
};
Test::Test() : _v("age",12)
{
add( &_v );
}
int main()
{
Test t ;
std::cout << t ;
} |
Partager