Using a class template/virtual functions

Here is my situation. I'm currently trying to get a better understanding of graphics, more specifically particles systems, for my intro to graphics course. Unfortunately everything we do is in C++ and most of my experience is with Java, so I'm quickly figuring out that my C++ skills are abysmal. I've got a basic particle system class that I took from our book, 3D Game Engine Design by Eberly, but the class template is the only thing I have and I have no idea how to use it properly since I've never used a class template before. It also has several virtual functions, so I need help understand how virtual functions work as well. I've looked over some tutorials but they are all extremely simplified and don't really help me much. Basically what I want to do is take this class, and put it into a simple program that uses it to make a particle system.

template <class Real, class TVector>
class ParticleSystem
{
public:
ParticleSystem (int iNumParticles, Real fStep);
virtual ~ParticleSystem ();

int GetNumParticles () const;
void SetMass (int i, Real fMass);
Real GetMass (int i) const;
TVector* Positions () const;
TVector& Position (int i);
TVector* Velocities () const;
TVector& Velocity (int i);
void SetStep (Real fStep);
Real GetStep () const;

virtual TVector Acceleration (int i, Real fTime, const TVector* akPosition, const TVector* akVelocity) = 0;

virtual void Update (Real fTime);

protected:
int m_iNumParticles;
Real* m_afMass;
Real* m_afInvMass;
TVector* m_akPosition;
TVector* m_akVelocity;
Real m_fStep, m_fHalfStep, m_fSixthStep;

//temporary storage for solver
typedef TVector* TVectorPtr;
TVectorPtr m_akPTmp, m_akDPTmp1, m_akDPTmp2, m_akDPTmp3, m_akDPTmp4;
TVectorPtr m_akVTmp, m_akDVTmp1, m_akDVTmp2, m_akDVTmp3, m_akDVTmp4;
};
Hi, there is info on the sites tutorial pages on both templates and virtual members
http://www.cplusplus.com/doc/tutorial/polymorphism.html
http://www.cplusplus.com/doc/tutorial/templates.html

The 'simple' expmnation is that a template is a definition of a class which can be used with a number of different classes - you specify which when you declare an instance of it.
In this case you would need a vector class of some kind defined, lets call it MyVector, and would define an instance of ParticleSystem by
1
2
3
4
ParticleSystem<double,MyVector> particleSystemInstance
//^^^Template Class Name
//             ^^^ Substitute for 'Real'
//                     ^^^Substitute for TVector 

When using the class you would then treat everywhere that the template definition has Real as double and TVector as MyVector

Virtual functions are part of polymorphism and indicate the function can be overridden by a descendant class. If you are not trying to define a descendant class you can ignore the fact a function is virtual.
Thanks for the help Faldrax. Ok so I might be able to ignore the fact that some functions are virtual, but looking back at this is it necessary to make this a template or can I get around that? I added more to what the book had after looking over some tutorials and came up with a basic structure for the program, let me know if I'm using the correct syntax. I'm not quite sure what some of the fuctions need to be doing, but once I've gotten that far I'll probably move to another board thats more for OpenGL if I need further assistance.

#include <iostream>
using namespace std;

template <class Real, class TVector>
class ParticleSystem
{
public:
ParticleSystem (int iNumParticles, Real fStep);
virtual ~ParticleSystem ();

int GetNumParticles () const;
void SetMass (int i, Real fMass);
Real GetMass (int i) const;
TVector* Positions () const;
TVector& Position (int i);
TVector* Velocities () const;
TVector& Velocity (int i);
void SetStep (Real fStep);
Real GetStep () const;

virtual TVector Acceleration (int i, Real fTime, const TVector* akPosition, const TVector* akVelocity) = 0;

virtual void Update (Real fTime);

protected:
int m_iNumParticles;
Real* m_afMass;
Real* m_afInvMass;
TVector* m_akPosition;
TVector* m_akVelocity;
Real m_fStep, m_fHalfStep, m_fSixthStep;

//temporary storage for solver
typedef TVector* TVectorPtr;
TVectorPtr m_akPTmp, m_akDPTmp1, m_akDPTmp2, m_akDPTmp3, m_akDPTmp4;
TVectorPtr m_akVTmp, m_akDVTmp1, m_akDVTmp2, m_akDVTmp3, m_akDVTmp4;
};

ParticleSystem::ParticleSystem (int iNumParticles, Real fStep){
m_iNumParticles = iNumParticles;
m_fStep = fStep;
}

ParticleSystem::~ParticleSystem (){
delete m_iNumParticles;
delete m_fStep;
}

int ParticleSystem::GetNumParticles () const{
return m_iNumParticles;
}

Real ParticleSystem::GetMass (int i) const{
return m_afMass;
}

void ParticleSystem::SetMass (int i, Real fMass){
//operations here
}

TVector* ParticleSystem::Positions () const{
//operations here
}

TVector& ParticleSystem::Position (int i){
//operations here
}

TVector* ParticleSystem::Velocities () const{
//operations here
}

TVector& ParticleSystem::Velocity (int i){
//operations here
}

TVector ParticleSystem::Acceleration (int i, Real fTime, const TVector* akPosition, const TVector* akVelocity){
//operations here
}

void ParticleSystem::Update (Real fTime){
//operations here
}

void ParticleSystem::SetStep (Real fStep){
m_fStep = fStep;
m_fHalfStep (fStep)/2;
m_fSixthStep (fStep)/6;
}

Real ParticleSystem::GetStep () const{
return m_fStep;
}

int main()
{
//call functions here
return 0;
}
Topic archived. No new replies allowed.