pure virtual -> impure virtual function -> plain old function
class Derived: public Base
The public above means that the public members of the base class become public members of the derived class and protected members of the base class become protected members of the the derived class.
If it were private, public members of the base class become private members of the derived class and the protected members of the base class become private members of the derived class.
Protected is less important, but if it were protected, public members of the base class become protected members of the derived class and protected members of the base class become protected members.
For all of the above, private members of a class are never accessible from anywhere except from members of the same class.
IN SUMMATION:
PUBLIC public -> public protected -> protected private -> CAN'T ACCESS THAT SHIT PRIVATE public -> private protected -> private private -> CAN'T ACCESS THAT SHIT PROTECTED public -> protected protected -> protected private -> CAN'T ACCESS THAT SHIT
Virtual functions like the following:
virtual void Method() {}
Make it so that if say, you have an Animal class and a Cat class, and Cat derives from Animal, and they both have walk, and you have the following:
Animal * cat = new Cat(); cat->walk();
The cat will do the catwalk and not the shitty animalwalk.
In mumbo-jumbo, it inherits the interface.
Constructors: B does not inherit constructors from A; Unless B's ctor explicitely calls one of A's ctor, the default ctor from A will be called automatically before B's ctor body (the idea being that A needs to be initialized before B gets created). Destructors: B does not inherit A's dtor; After it exits, B's destructor will automatically call A's destructor.
class Base { Base(int i) {} }; class Derived: public Base {}; Derived d(3);
That won't compile since Base constructor is not inherited.
Most of the time, if you want your destructor to be called, you gotta virtual that shit.
class Base{ public: void foo(){std::cout<<"base";} }; class Derived : public Base { public: void foo(){ Base::foo(); std::cout<<"derived"; } };
Pure virtual means that if, say, Walk() is in Animal, then ALL animals who derive from there HAVE to eat. They are usually implemented something like this:
virtual Walk() = 0;
Asteroid and SpaceShip inherit from GameObject
GameObject *pgo; //dynamic type Asteroid *pa = new Asteroid; //static type SpaceShip *ps = new SpaceShip; //static type
pgo = pa; pgo->Draw(); pgo = ps; ps->Draw();
Let's say GameObject has a virtual function called Draw()
Asteroid will have space for GameObject's Draw() and its own Draw()
pa is pointing to the Asteroid class and ALSO a vptr to a vtbl/virtual table which is an array/container of function pointers.
When you call pgo->Draw() after it's assigned to pa, it's going to dereference vptr, then dereference Asteroid::Draw(). That's one more dereference than normal. Usually you just gotta dereference the function pointer.
All this is done at runtime. So, in summation, there's two main causes of the overhead: The introduction of a vptr for EVERY OBJECT OF THE CLASS, as well as having to dereference one more than usual. Of course, there's other shit like having to figure out which in the vtbl to select, but idunolol.
PS: Different compilers do it differently, so trying to calculate overhead is kinda dum.
An abstract class is a class designed specifically to be used as a base class. It has at least one pure virtual function.
static type is the type its initialized with, dynamic type is the type that it actually is. static types are computed at compile time. dynamic types are computed at run time.
dynamic casting when casting from a parent to a child is called "down casting".
TEMPLATED FUNCTIONS CAN DEDUCE INHERITED TYPES