Pointer to Members

It is possible to take the address of a member of a class and assign it to a pointer. The address of a member can be obtain by applying the operator & to a "fully qualified" class member name. A class member pointer can be declared using the operator ::* with the class name.For example, given the class

class A
{
private:
int m;
public:
void show();
};
We can define a pointer to the member m as follows:
int A::* ip= &A :: m;
The ip pointer created thus acts like class member in that it must be invoked with a class object. The phrase A::* means "pointer to member of a class". The phrase &A::m means the "address of the m member of A class".

Remember, the following statement is not valid:

int *ip = &m;

const Member Function

If a member function does not alter any data in the class, then we may declare it as a const member function as fallows:

void mul(int ,int) const;
double get_balance() const;

The qualifier const is appended to the function prototype. The compiler will generate an error message if such functions try to alter the data values.

A friend function possesses certain special characteristics:

  • It is not in the scope of the class to which it has been declared as friend.
  • Since it is not in the scope of the class, it cannot be called using the object of that class.
  • It can be invoked like a normal function without the help of any object.
  • member function, it cannot access the member names directly and has to use an object name and dot membership operator with each member name.
  • It can be declared either in the public or the private part of a class without affecting its meaning.
  • Usually, it has the objects as arguments.

Friendly Functions

A non member function cannot have an access to the private data of a class. However, there could be a situation where we would like two classes to share a particular function. For example, consider a case where two classes, manager and scientist, have been defined. We would like to use a function income_tax() to operate on the object of both these classes. In such situations, C++ allows the common function to be made friendly with both the classes, thereby allowing the function to have access to the private data of these classes. Such a function need not be member of any of these classes.

To make an outside function "friendly" to a class, we have to simply declare this function as a friend of the class as shown below:

class ABC
{
.....
.....
public:
.....
.....
friend void xyz(void);
};

Array of Objects

We know that an array can be of any data type including struct. Similarly, we can also have array of variables that are of the type class. Such variables are called arrays of objects. Consider the following class definition:

class employee
{
char name[30];
float age;
public:
void getdate(void);
void putdata(void);
};

Static Member Functions

Like static member variable, we can also have static member function. A member function that is declared static has the following properties:
  • A static function can have access to only static members declared in the class.
  • A static member function can be called the class name as follows:

class-name :: function-name;

The static function showcount() displays the number of objects created till that moment. A count of number of objects created is maintained by the static variable count.
The function showcode() displays the code number of each object.

Static Data Members

A data member of a class can be qualified as static. The properties of a static member variable are similar to that of a C static variable. A static member variable has certain special characteristics. These are:
  • It is initialized to zero when the first object of its class is created. No other initialization is permitted.
  • Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.
  • It is visible only within the class, but its lifetime is the entire program.

Static variables are normally used to maintain values common to the entire class. For example, a static data member can be used as a counter that record the occurrences of all the objects.Program illustrates the use of a static data member.

#include
using namespace std;

class item
{
static int count;
int number;
public:
void getdata(int a)
{
number = a;
count ++;
}
void getcount(void)
{
cout << "Count: ";
cout << count <<"\n";
}
};
int item :: count;

int main()
{
item a,b,c;
a.getcount();
b.getcount();
c.getcount();

a.getdata(100);
b.getdata(200);
c.getdata(300);

cout << "After reading data"<<"\n";
a.getcount();
b.getcount();
c.getcount();
return 0;
}

The output of the program would be:
Count: 0
Count: 0
Count: 0
After reading data
Count: 3
Count: 3
Count: 3