Вы находитесь на странице: 1из 17

11-1

THE THIS POINTER

 this pointer:
- Implicit parameter passed to a member
function
- points to the object calling the function

11-2
USING THE THIS POINTER

Can be used to access members that may be hidden


by parameters with the same name:
class SomeClass
{
private:
int num;
public:
void setNum(int num)
{ this->num = num; }
};
CONST AND CLASSES
CONSTANT MEMBER FUNCTIONS
 Declared
with keyword const
 When const follows the parameter list,
int getX()const
the function is prevented from modifying the
object.
 When const appears in the parameter list,
int setNum (const int num)
the function is prevented from modifying the
parameter. The parameter is read-only.
CONST MEMBER FUNCTIONS

 A const member function guarantees that it will


never modify any of its class member data.

11-6
EXAMPLE

//Demonstrates const member functions


class aClass
{
private:
int alpha;
public:
void nonFunc() //non-const member function
{ alpha = 99; } //OK
void conFunc() const //const member function
{ alpha = 99; } //ERROR: can’t modify a
member
};
CONST OBJECTS
 Object of a class can be made constant.

 When an object is declared as const, you can’t


modify it.

 It follows that you can use only const member


functions with it, because they’re the only ones
that guarantee not to modify it.

11-8
EXAMPLE
class Distance //English Distance class
{
private:
int feet; float inches;
public: //2-arg constructor
Distance(int ft, float in) : feet(ft), inches(in)
{}
void getdist() //user input non-const func
{
cout << “\nEnter feet: “; cin >> feet;
cout << “Enter inches: “; cin >> inches;
} 11-9
void showdist() const //display distance const func
{
cout << feet << “\’-” << inches << ‘\”’; }
};
////////////////////////////////////////////////////////////////
int main()
{
const Distance football(300, 0);
football.getdist(); //ERROR: getdist() not const
cout << “football = “;
football.showdist(); //OK
cout << endl;
return 0;
11-10

}
11.2 STATIC MEMBERS
 Static member variable:
 One instance of variable for the entire class
 Shared by all objects of the class

 Static member function:


 Can be used to access static member variables
 Can be called before any class objects are created

11-11
STATIC MEMBER VARIABLES
1) Must be declared in class with keyword static:
class IntVal
{
private:
int value;
static int valCount;
public:
intVal(int val = 0)
{ value = val; valCount++ }
int getVal();
void setVal(int);
};

11-12
STATIC MEMBER VARIABLES
2) Must be defined outside of the class:
class IntVal
{
//In-class declaration
static int valCount;

};
//Definition outside of class
int IntVal::valCount = 0;

11-13
STATIC MEMBER VARIABLES

3) Can be accessed or modified by any object of the


class: Modifications by one object are visible to all
objects of the class:
IntVal val1, val2;

valCount
val1 2 val2

11-14
STATIC MEMBER FUNCTIONS

static member function


Is a service of the class, not of a specific object of the class

Declared with static before return type:


class IntVal
{ public:
static int getValCount()
{ return valCount; }
private:
int value;
static int valCount;
}; 11-16
STATIC MEMBER FUNCTIONS
Can be called independently of class
objects, through the class name:
cout << IntVal::getValCount();

Can be called before any objects of the


class have been created

Used mostly to manipulate static


member variables of the class

11-17

Вам также может понравиться