Static and Const: CSC-210: Object Oriented Programming
Static and Const: CSC-210: Object Oriented Programming
Static and Const: CSC-210: Object Oriented Programming
Static Data It is initialized to zero when the first object of its class is
created.
Member you cannot initialize a static member variable inside the
class declaration.
It is visible only within the class, but its lifetime is the entire
program.
Static members are generally used to maintain values
common to the entire class.
class demo Static Data members
{
static int count;
public:
void getcount()
{ count
cout<<"count="<<++count;
} 0
3
2
1
};
int demo::count;
d1 d2 d3
int main()
{
demo d1,d2,d3;
d1.getcount(); Static members are declared inside
d2.getcount(); the class and defined outside the
d3.getcount(); class.
return 0;
}
class demo
Regular Data members
{
int count;
public:
void getcount()
{
count = 0; d1 d3
cout<<"count="<< ++count;
d2
}
};
int main() 10 01 10
{
demo d1,d2,d3; count count count
d1.getcount();
d2.getcount();
d3.getcount();
return 0;
}
Static member functions can access only static members
of the class.
Static member functions can be invoked using class
name, not object.
Static Member There cannot be static and non-static version of the same
function.
Functions
They cannot be virtual.
They cannot be declared as constant or volatile.
A static member function does not have this pointer.
class item C++ Code
{
int number;
static int count;// static variable declaration
public:
void setdata(int a){
number = a;
count++; int main()
} {
static void getcount(){ item a,b,c;
cout<<“value of number: ”<<number;
cout<<“\nvalue of count: ”<<count; a.setdata(100);
} item::getcount();
};
int item :: count=10; // static variable definition b.setdata(200);
Output: item::getcount();
value of count: 11 c.setdata(300);
value of count: 12 item::getcount();
value of count: 13 return 0;
}
C++ Code
#include<iostream>
using namespace std;
class Count
{
static int counter;
public:
Count() Output:
{ 1
cout << counter++ <<endl; 2
} 3
};
int Count::counter = 1;
void main()
{
Count c, c1, c2;
}
class Life class Life
C++ Code
{ {
int i; int i;
public: public:
Life() Life()
{ {
i = 0; i = 0;
cout << "Constructor\n"; cout << "Constructor\n";
} }
~Life() ~Life()
{ {
cout << "Destructor\n"; cout << "Destructor\n";
} }
}; };
Example C++ }
cout << i;
};
int main()
{
Constant c;
c.display();
}
You can declare a method of a class to be const
A const method can be called by any type of object.
Non-const functions can be called by non-const objects
only.
int main()
{
Constant c,c1(40);
c.display();
c1.display();
}
The objects of a class can also be declared as const.
An object declared as const cannot be modified and
hence, can invoke only const member functions as
these functions ensure not to modify the object.
Const Objects A const object can be created by prefixing the const
keyword to the object declaration. Any attempt to
change the data member of const objects results in a
compile-time error.
#include<iostream>
using namespace std;
class Constant
{
const int i = 10;
int j;
public:
Constant() :i(1),j(0) {}
Constant(int a) :j(a) {}
void display() const
{
cout << i << " " << j << endl;
}
void display2() int main()
{ {
cout << i << " " << j << endl; Constant c,c1(40);
} c.display();
}; c1.display();
const Constant c2(2),c3;
c2.display();
c3.display();
}
A copy constructor is a member function that initializes
an object using another object of the same class. A copy
constructor has the following general function
Copy prototype:
Constructor ClassName (const ClassName &old_obj);
#include<iostream> C++ Code
using namespace std;
class Real
{
int i,j;
public:
Real() :i(0), j(0) { cout << "Default Constructor Called\n"; }
Real(int a, int b)
{
i = a;
j = b;
cout << "\nParameterized Constructor Called\n";
}
Real(const Real& r)
{ int main()
i = r.i; {
j = r.j; Real r1;
cout << "\nCopy Constructor Called\n"; r1.display();
} Real r2(2, 4);
void display() r2.display();
{ Real r3 = r2;
cout << i << " " << j << endl; r3.display();
}
}
};