Beruflich Dokumente
Kultur Dokumente
Exam Revision
A Guide only
Pointers
• Pointers should always be initialised
Type * PointerName = 0;
ptrToAge = &age;
int age(21);
int *ptrToAge;
ptrToAge = &age;
int main(){
cout << CalculateTax(32000) << endl;
return 0;
}
} optional variable/s;
}
Abstract Member Functions
• Also know as pure virtual member functions.
• A function that has no implementation.
• It is virtual as subclasses must override it.
virtual type MemberFunctionName() = 0;
• When declared within a class it prevents the class from being
instantiated (an abstract class).
• Only derived classes that implement the pure virtual member
functions can be instantiated.
class Person { class Soldier : public Person {
public: public:
virtual void Move() = 0; void Move(){ }
}; };
General
padlock : private
key : protected
neither : public
Static Data Members
• Static members can be either variables or functions.
• Static variables (class variables) belong to the class. A single value
for all objects instantiated from the class.
• Similar to global variables but within the scope of the class and
must obey access modifier rules.
static type identifier;
class Player{
public:
static int instances;
Player(){
instances++;
}
};
class Group{
private:
GameObject** objects;
int size;
public:
Group(int n):size(n){
objects = new GameObject*[size];
for(int n=0;n<size;n++)
objects[n] = new GameObject(n);
}
void Display(){
for(int n=0;n<size;n++)
objects[n]->Display();
}
};
Polymorphism
Model’s Draw member class Model{
public:
function is virtual. This virtual void Draw(){
cout << "Draw Model \n";
ensures objects of type Car }
will invoke the Car’s Draw };
class ClassName{
friend returnType functionName(parameter/s);
}
• In the next example the friend function creates an array of
all the unMounted weapons in a game. isMounted is a
private member so the function is declared as a friend of
the class.
Friend Function Example
class Weapon{
private:
bool isMounted;
public:
string name;
Weapon(string desc,bool mounted):name(desc),isMounted(mounted){}
friend Weapon** UnmountedWeapons(Weapon **,int* );
};
delete pointer;
delete[] pointer;