Thursday, September 9, 2010

J1N Games

Jason Kim's Personal Journey Into Game Development

Archive for the ‘Code Work’ Category

C++ Factory Example

Posted by Jason On September - 29 - 2009

An Example/Tutorial of the Factory Design Method

A factory method is a static method of a class that returns an object of that class’s type.  Unlike a constructor, the actual object it returns might be an instance of a subclass.  It can return existing instances multiple times.  The factory method is basically a “creator” design pattern in C++ for creating sub classes from a base type class.

C++ Factory Example

Popularity: 14% [?]

C++ Virtual Keyword Example

Posted by Jason On September - 29 - 2009

An Example/Tutorial of the Virtual Keyword.

The Virtual Keyword in C++ is used for inheritance and is an important concept in Object Orientated Programming and the Polymorphism buzzword.  The Virtual Keyword in a base class indicates a method can be used by a derived class or be defined or overridden with its own function.

The term “Pure Virtual” comes from when you declare a method in a class as such:  virtual void MyFunction() = 0;
By appending the “= 0″ part this means the virtual member function has no implementation.  This creates an abstract method by which makes the Class an Abstract Class.
An Abstract Class cannot create an instance of itself, only a pointer to itself can apply, however derived classes of the Abstract Class can.

The computer knows which virtual function to call from base and derived classes from the compiler setting up a virtual table, like the symbol table, the virtual table has pointers to each of the virtual functions and classes. The compiler sets up a hidden pointer *__vptr automatically for each class instance so that it points to the virtual table for that class.  It is different from the *this pointer which is actually a function parameter for self references.  the vptr is a real pointer and makes the classes bigger by one pointer.  vptr is also inherited by derived classes

The diamond problem, also comes up with multiple inheritance.  This is where if you have base class A, and derived class B and C, and class D derives from B and C, the compiler will find it ambiguous and does not know which path of derivation to take for class D, does class D derive A from class B or does class D derive from class C from A?  To solve this problem the virtual keyword can also be used here, by creating each derived class B and C like this:
class B : virtual public A
class C : virtual public A
The virtual keyword here will virtually inherit from A, thus solving the ambiguity of class D.

C++ Virtual Keyword Example

Popularity: 14% [?]

C++ Linked List Example

Posted by Jason On September - 29 - 2009

An Example/Tutorial of a Linked List Structure.

Linked Lists are basically a type of data structure that houses in each node the data and a pointer to the next node.  A Singly-Linked List is your most basic Linked List which has each node from start to end with the end node pointing to a Null terminator.  A Doubly Linked List has nodes that point both to the next node and another node.  A Circularly Linked List is one where there is no Null terminator at the end, but instead the end node points to the first node.  The Idea is basically Head->Node->Node->End->NULL

C++ Linked List Example

Popularity: 15% [?]

C++ Overloading Example

Posted by Jason On September - 29 - 2009

An Example/Tutorial on Function and Operator overloading in C++

Function Overloading is having functions of the same name but giving them different number of parameters or types.

Operator Overloading is having operator keywords like “+” or “-” perform another action or function.

C++ Overloading Example

Popularity: 14% [?]

C++ Template Example

Posted by Jason On September - 29 - 2009

An Example/Tutorial on how to use C++ Templates
Templates are special functions to be used with more than one type of class or typename indentifier.

A Template Function looks usually like this format:
template<class MyType>
MyType GetMax(MyType a, MyType b)
{
return (a>b ? a: b);
}
with a call that looks like this:
GetMax<int>(x,y);

Templates can also be used with classes.  See the example:
C++ Template Example/Tutorial

Popularity: 13% [?]

C++ Friend Functions/Classes Example

Posted by Jason On September - 29 - 2009

An Example/Tutorial of how to use Friend Functions/Classes in C++

Friend Functions and Classes are Functions/Classes that can access another Class’s Private Members without having to be a derived class or member function.  This is done by the keyword “friend”

C++ Friend Functions/Classes Example

Popularity: 7% [?]

C++ Class Example

Posted by Jason On September - 29 - 2009

An example or tutorial on how to use Classes in C++

C++ Class Example

Popularity: 7% [?]