6. Static Class Data and Functions
Data and member functions in a class can be made
static.
For a statically declared data item, the compiler
creates only one instance of that item no matter
how many objects of the same class are created.
Precisely, all the objects of the class see the same
data; application e.g., road-racing game; e.g, count.
e.g.,
private:
static int data_item; //data_item declared statically
7. Automatic Class Data
class foo int main()
{ {
private: foo f1, f2, f3;
int count;
cout<< “count is ” <<
public: f1.get_count() <<endl;
foo() : count(0)
{ count ++ ; }
cout<< “count is ” <<
int get_count() f2.get_count() <<endl;
{ return count ; }
}; cout<< “count is ” <<
f3.get_count() <<endl;
}
8. Static Class Data
class foo int main()
{ {
private: foo f1, f2, f3;
static int count;
cout<< “count is ” <<
public: f1.get_count() <<endl;
foo()
{ count ++ ; }
cout<< “count is ” <<
int get_count() f2.get_count() <<endl;
{ return count ; }
}; cout<< “count is ” <<
int foo : : count = 0; f3.get_count() <<endl;
}
10. Static Functions
Like data in a class, member functions can also be
made static.
A static member function follows the keyword
static in its declarator.
e.g.,
void mem_func() //normal function declarator
{ }
static void mem_func()//static function declarator
{ }
11. class gamma int main()
{ private: {
static int total;
public: gamma g1;
gamma() //constructor gamma :: show_total();
{ total++; }
static show_total() gamma g2, g3;
{ cout << “Total is ” << gamma :: show_total();
<< total <<endl;
}
}; }
int gamma : : total = 0; //definition
12. class gamma int main()
{ private: {
static int total;
int id; gamma g1;
public: gamma :: show_total();
gamma() //constructor
{ total++;
id = total; } gamma g2, g3;
static show_total() gamma :: show_total();
{ cout << “Total is ” <<
<< total <<endl;
} g1.show_id();
void show_id()
{ cout << “Id nr. is ” << g2.show_id();
<< id <<endl; g3.show_id();
}
}; }
int gamma : : total = 0; //definition
14. Assignment Operator
If a1 and a2 are objects then the statement a1 = a2
will cause the compiler to copy data from a1,
member by member, into a2. This is the default
action of the assignment operator.
15. class alpha int main()
{
{
private: alpha a1(5);
int data;
alpha a2;
public:
alpha() { }
a2 = a1; //invoke overloaded =
alpha(int d) { data = d; }
cout<<"n a2= ";
void display()
{ cout<<data; } a2.display();
}
apha operator = (alpha& a)
{ data = a.data;
cout << "nAssignment operator invoked";
return alpha(data);
}
};
16. class alpha int main()
{
{
private: alpha a1(5);
int data;
alpha a2;
public:
alpha() { }
a2 = a1; //invoke overloaded =
alpha(int d) { data = d; }
cout<<"n a2= ";
void display()
{ cout<<data; } a2.display();
}
alpha& operator = (alpha& a)
{ data = a.data;
cout << "nAssignment operator invoked";
return *this;
}
};
17. This Pointer
The member functions of every object have access to a
sort of magic pointer named this, which points to the
object itself.
Using this pointer any member function can find out
the address of the object of which it is a member.
18. class where int main()
{ {
private:
where w1, w2, w3;
char charray[10]; //occupies 10 bytes
public: w1.reveal();
void reveal() w2.reveal();
{ w3.reveal();
cout << “nObject’s address is “ <<
this; } }
};
19. Copy Initialization/Constructor
For the statement alpha a2(a1); the compiler
creates a new object, a2, of class alpha, and copies
the data from a1, member by member, into a2. The
is the default action of the copy constructor.
20. class alpha int main()
{ private:
int data; {
public: alpha a1(5);
alpha() { }
alpha a2;
alpha(int d) { data = d; }
void display()
a2 = a1; //invoke overloaded =
{ cout<<data; }
alpha(alpha& a) //copy constructor cout<<"n a2= ";
{ data = a.data;
a2.display();
cout << “nCopy constructor invoked”;
}
alpha a3(a1); //invoke copy constructor
void operator = (alpha& a) // alpha a3 = a1;//equivalent definition of a3
{ data = a.data;
cout << "nAssignment operator invoked"; cout << “na3=”;
} a3.display();
};
}