C++ Classes

less than 1 minute read

Inheritance:

class Accelerometer : public Vec3 { 
  //...

Virtual functions allow dynamic dispatch so that we can override a function even when we cast the inherited class to its base class.

  • Requires additional memory
  • Takes extra time to do the vtable lookup
class Vec3 {
public:
  float x,y,z;
  virtual float Norm();
}

class Accelerometer {
public:
  float Norm() override;
}

Pure virtual functions allow us to create interface specifications:

class IVec {
public:
  virtual float Norm() = 0;
}

Member Initializer Lists

class Acceleration {
private:
	float x;
	float y;
	float z;
public:
  Acceleration()
		: x(0.0), y(0.0), z(9.807) // Should be in the order they are declared
	{
		\\...
	}
}