Static Data Member
• a class member that is shared by all objects of the class
• only a single copy exists for the entire class
• typically used for data common to all objects, such as a counter
int item :: count = 0; //count defined
class Student {
public:
// Declaration of the static data
// Definition and initialization of the static data member outside the class member
int Student::total_students = 0; static int total_students;
int main() { // Constructor increments the counter
// Access static member using class name before creating objects each time a new object is created
cout << "Initial number of students: " << Student::total_students << endl; Student()
// Create objects {
Student s1; total_students++;
Student s2; }
Student s3;
};
// Access the shared static member using the class name or an object
cout << "Number of students after creating s1, s2, s3: " << Student::total_students << endl;
cout << "Also accessible via an object (s1.total_students): " << s1.total_students << endl;
return 0; Initial number of students: 0
} Number of students after creating s1, s2, s3: 3
Also accessible via an object (s1.total_students): 3
OBJECTS AS FUNCTION ARGUMENTS
• Like any other data type, an object may be used as A function
argument
• two ways
• A copy of the entire object is passed to the function. (pass-by-value)
• Only the address of the object is transferred to the function(pass-by-
reference)
pass-by-value.
• Since a copy of the object is passed to the function, any change made
to the object inside the function do not effect the object used to call
the function.
pass-by-reference
• When an address of the object is passed, the called function works
directly on the actual object used in the call.
• This means that any changes made to the object inside the functions
will reflect in the actual object.
• The pass by reference method is more efficient since it requires to
pass only the address of the object and not the entire object.
int main()
{
// Create objects
Example E1, E2; class Example {
// Values are initialized for both objects public:
E1.a = 50; int a;
E2.a = 100;
cout << "Initial Values \n"; // This function will take
cout << "Value of object 1: " << E1.a // an object as an argument
<< "\n& object 2: " << E2.a void add(Example E)
<< "\n\n"; {
// Passing object as an argument a = a + E.a;
// to function add() }
[Link](E1); };
[Link](E2);
// Changed values after passing
// object as argument E1 E2
cout << "New values \n";
cout << "Value of object 1: " << E1.a A=200 A=150
<< "\n& object 2: " << E2.a
<< "\n\n"; add add
return 0;
}
int main()
{
// Create objects
Example E1, E2; class Example {
// Values are initialized for both objects public:
E1.a = 50; int a;
E2.a = 100;
cout << "Initial Values \n"; // This function will take
cout << "Value of object 1: " << E1.a // an object as an argument
<< "\n& object 2: " << E2.a void add(Example E)
<< "\n\n"; {
// Passing object as an argument a = a + E.a;
// to function add() }
[Link](E1); };
// Changed values after passing
// object as argument
cout << "New values \n"; Initial Values
cout << "Value of object 1: " << E1.a Value of object 1: 50
<< "\n& object 2: " << E2.a & object 2: 100
<< "\n\n";
return 0; New values
} Value of object 1: 50
& object 2: 150
class time
int main() {
{ int hours;
time T1,T2,T3; int minutes;
[Link](2,45); public:
[Link](3,30); void gettime(int h, int m)
[Link](T1,T2); {
cout<< ”T1=”; hours=h;
[Link]( ); minutes=m;
cout<<”T2=”; }
[Link]( ); void puttime(void)
cout<<”T3=”; {
[Link]( ); cout<< hours<<<minutes<<”minutes:”<< <<end;
return(0); }
} void sum (time t1,time t2)
{
minutes=[Link] + [Link];
hours=hours+[Link]+[Link];
}