Skip to content

Instantly share code, notes, and snippets.

@ghthor
Last active January 25, 2018 10:59
Show Gist options
  • Save ghthor/30f2baba922f89c3166d8205aa2825dc to your computer and use it in GitHub Desktop.
Save ghthor/30f2baba922f89c3166d8205aa2825dc to your computer and use it in GitHub Desktop.
A C++ example I made to demonstrate polymorphism to David Rogers
// Base Class
class BaseClass
{
public:
virtual void myFunc()
{
cout << "myFunc From BaseClass";
}
void overLoaded()
{
cout << "overLoaded from BaseClass";
}
};
// derived Class
class TacosClass : public BaseClass
{
int tacosInt;
public:
virtual void myFunc()
{
cout << "myFunc from TacosClass";
}
void overLoaded()
{
cout << "overLoaded from TacosClass";
}
};
void main()
{
BaseClass* tamborines = new TacosClass();
BaseClass* baseClass = new TacosClass();
TacosClass* tacosClass = baseClass;
tamborines.myFunc(); // output: myFunc from TacosClass
baseClass.overLoaded(); // output: overLoaded from BaseClass
tacosClass.overLoader();// output: overLoaded from TacosClass
cout << tamborines.tacosInt; // This is an error becuase BaseClass didn't declare a tacosInt
delete tamborines;
return 0;
}
void BaseClass :: funcCall()
{
myFunc();
}
// Need to add This Header to the hl2_player.cpp file
#include "hl2mp_player.h"
void CHL2_Player::StartSprinting( void )
{
CHL2MP_Player* testingForMP = dynamic_cast<CHL2MP_Player*>(this); // This line casts the this pointed up to a CHL2MP_Player*
if(testingForMP == 0) // this is not a CHL2MP_Player
{
if( m_HL2Local.m_flSuitPower < 10 )
{
// Don't sprint unless there's a reasonable
// amount of suit power.
CPASAttenuationFilter filter( this );
filter.UsePredictionRules();
EmitSound( filter, entindex(), "HL2Player.SprintNoPower" );
return;
}
if( !SuitPower_AddDevice( SuitDeviceSprint ) )
return;
CPASAttenuationFilter filter( this );
filter.UsePredictionRules();
EmitSound( filter, entindex(), "HL2Player.SprintStart" );
SetMaxSpeed( HL2_SPRINT_SPEED );
m_fIsSprinting = true;
}
else // this is a CHL2MP_Player
{
testingForMP->StartSprinting();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment