03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5.5. EXERCISES 185<br />

5.5 Exercises<br />

5.5.1 Vector class<br />

Revisit the vector example from §??.<br />

Make an expression <strong>for</strong> a scalar times a vector:<br />

class scalar times vector expressions {<br />

} ;<br />

that inherits from base vector. Use the inheritance mechanism to assign scalar times vector expressions<br />

into vector.<br />

5.5.2 Vector expression template<br />

Make a vector concept, which you call Vector. Make a vector class (you can use std::vector)<br />

that satisfies this concept. This vector class should have at least the following members:<br />

class my vector {<br />

public:<br />

typedef double value type ;<br />

public:<br />

my vector( int n ) ;<br />

// Copy Constructor from type itself<br />

my vector( my vector& ) ;<br />

// Constructor from generic vector<br />

template <br />

my vector( Vector& ) ;<br />

// Assignment operator<br />

my vector& operator=( my vector const& v ) ;<br />

// Assignment <strong>for</strong> generic Vector<br />

template <br />

my vector& operator=( Vector const& v ) ;<br />

value type& operator() ( int i ) ;<br />

public: // Vector concept<br />

int size() const ;<br />

value type operator() ( int i ) const ;<br />

} ;<br />

Make an expression <strong>for</strong> a scalar times a vector:<br />

template <br />

class scalar times vector expression{<br />

} ;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!