home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2001 October
/
Chip_2001-10_cd1.bin
/
chplus
/
cpp
/
vektor3.cpp
< prev
Wrap
C/C++ Source or Header
|
2001-09-12
|
2KB
|
75 lines
////////////////////////////////////////////////////////////////////////////
//
// soubor: vektor2.cpp
// autor: Jaroslav Franek
// copyright? proc ne: (c) 2001 Jaroslav Franek
//
// Spratelene funkce
// reseni 2
//
////////////////////////////////////////////////////////////////////////////
#pragma hdrstop
template <class T> class Vektor;
template <class T> Vektor<T> operator+(const Vektor<T> & v1, const Vektor<T> & v2);
//---------------------------------------------------------------------------
template <class T> class Vektor
{
friend Vektor<T> (::operator +)(const Vektor<T> & v1, const Vektor<T> & v2);
public:
//
// ctr & dtr
//
Vektor(int s) : size_(s), data_(new T [s]) {}
Vektor(const Vektor<T> & v);
~Vektor() { delete [] data_; }
//
// iface
//
T & operator [](int i) { return data_[i]; }
T operator [](int i) const { return data_[i]; }
private:
T * data_;
int size_; // velikost
};
//---------------------------------------------------------------------------
template <class T>
Vektor<T>::Vektor(const Vektor<T> & v)
: size_(v.size_),
data_(new T [v.size_])
{
for ( int i = 0 ; i < size_ ; ++i )
{
data_[i] = v.data_[i];
}
}
//---------------------------------------------------------------------------
template <class T>
Vektor<T> operator +(const Vektor<T> & v1, const Vektor<T> & v2)
{
// predpokladame, ze v1 a v2 maji stejnou velikost
Vektor<T> pom(v1.size_);
for ( int i = 0 ; i < pom.size_ ; ++i )
{
pom.data_[i] = v1.data_[i] + v2.data_[i];
}
return pom;
}
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
Vektor<double> a(2);
Vektor<double> b(2);
a+b; // (!)
return 0;
}
//---------------------------------------------------------------------------