Sie sind auf Seite 1von 13

More about

Classes
COS110: Chapter 14
Dr. Mard Helbig

Instances
Each instance of a class has its own copies of the
classs variables
class Wizard
{
private:
string name;
string school;
public:
Wizard();
~Wizard();
void setName(string);
void setSchool(string);
double getName() const;
double getSchool() const;
};

In main.cpp:
Wizard wizard1;
harry.setName(Harry);
harry.setSchool(Griffendor);
Wizard wizard2;
Malfoy.setName(Malfoy);
Malfoy.setSchool(Slitheren);

wizard1 object

wizard2 object

name

Harry

name

Malfoy

school

Griffendor

school

Slitheren

Instances
Member variables of a class is called instance
variables each instance of class has its own copies
class Wizard
{
private:
string name;
string school;
public:
Wizard();
~Wizard();
void setName(string);
void setSchool(string);
double getName() const;
double getSchool() const;
};

Instance variables

wizard1 object

wizard2 object

name

Harry

name

Malfoy

school

Griffendor

school

Slitheren

Static members
Can create a member variable or member function
that does not belong to an instance of a class
=> the class has only one copy of the member
Known as static member variables or static member
functions
When storing a value in a static variable, it is not store
in an instance of the class
An instance of class does not have access to a static
member variable
Static member functions can only operate on static
member variables

Static members
Think of normal private class variables as gholf balls
Each instance of class (GholfPlayer) has its own ball
that s/he is playing with

Think of static members as a soccer ball or rugby ball


Each instance of class (SoccerPlayer) only uses the
ball and the whole team (all instances of class) use
only one ball

Static member variables


A member variable declared as static:
o Only 1 copy of variable no matter how many instances of
class
o Single copy of classs static variable is shared by all instances
of the class
o Lifetime of static member variable is lifetime of program
o Comes into existence before any class instances are created
o Declared in the class
o Defined outside the class declaration

Wizard.h
Wizard.cpp

Static member functions


A member variable declared as static:
o Can only access static member variables
o Cannot access any non-static member variables
o Static member functions can be called before any instances
of a class exist
o Static member functions can access a classs static member
variables before any instances of class are defined in
memory
o Can create specialized setup routines for class objects

Wizard.h
Wizard.cpp

Static member functions


We declare static member function in class definition
(.h file):
static returnType FunctionName (ParameterTypeList);
Example: static int getNumInvites();

We dont use const with static member functions

Static member functions


We define static member function in .cpp file:
int Wizard::getNumInvites()
{

No static keyword!

Friends of a class
Private members of a class cannot be accessed from
outside the class, except through public member
functions
A friend is a function or class that is not a member of
a class, but has access to the private members of the
class
A friend is treated like a member of the class
Define it as follows:
friend returnType functionName (ParameterListType)

Wizard.h
Wizard.cpp

Dwarf.h
Dwarf.cpp

Friends of a class
class Wizard
{
private:
string name;
string school;
static int acceptedInvites;
static int maxAcceptance;
public:
Wizard();
~Wizard();
void setName(string);
void setSchool(string);
string getName() const;
string getSchool() const;
void acceptInvite(bool);
static void wizardInvites(int);
static int numAcceptedInvites();
friend void Dwarf::acceptInvite(bool,
&Wizard);
};

Static member functions

Static member functions

Friend member function

Friends of a class
class Wizard;

class Dwarf
{
private:
string name;
public:
Dwarf();
Dwarf(string);
void setName(string);
string getName() const;
void acceptInvite(bool, &Wizard);
};

Forward declaration of
class Wizard

Member function declared as


friend of class Wizard

Friends of a class
void Dwarf::acceptInvite(bool ans, &Wizard wiz)
{
if (ans)
if (wiz.acceptedInvites <
wiz.maxAcceptance)
wiz.acceptedInvites++;
};

Definition of friend
function in Dwarf.cpp

Das könnte Ihnen auch gefallen