Class Derivation Example
Example 6 – Class Derivation
FILENAME: example6.cpp
Program:
/*
* This example shows how a CosVec class may be derived
* from the class RWMathVec<double>. Class CosVec objects are
* double precision vectors that have been initialized
* with cosines with a specified number of integral
* cycles.
*/
 
// Include the RWMathVec<double> class header file:
#include <rw/math/mathvec.h>
#include <iostream>
 
/*
* Start derived class declarations:
* RWMathVec<double> is declared as a public base class
* for CosVec:
*/
 
class CosVec : public RWMathVec<double> {
 
public:
 
// unadorned constructor:
CosVec();
 
/* This is the useful constructor. We first use the
* RWMathVec<double> constructor
* RWMathVec<double>(n, val, by);
* to create a vector of phases. Taking the cosine of
* this vector will yield the desired initial values for
* the base class of CosVec.
*/
CosVec (unsigned N, int cycles = 1) :
( cos(RWMathVec<double>(N,0,2*M_PI*cycles/N)) )
{ }
 
// Copy constructor:
CosVec (const CosVec& v) :
(v)
{ }
};
 
int main()
{
/*
* Initialize a CosVec object with 12 elements and one
* cycle:
*/
CosVec c(12,1);
 
/*
* Output the vector: note that class CosVec inherits
* operator<< from the base class RWMathVec<double>.
*/
 
std::cout << c;
}
 
 
Sample Input:
None required.
Sample Output (note that the exact numbers will depend on machine precision):
[
1 0.866025 0.5 0 -0.5
-0.866025 -1 -0.866025 -0.5 0
0.5 0.866025
]