0% found this document useful (0 votes)
6 views

Static and Const: CSC-210: Object Oriented Programming

This document discusses static and const members in C++ classes. Static members are shared across all objects of a class while const members cannot be modified. The document provides examples of declaring and using static, const, and copy constructors.

Uploaded by

Bilal Asad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Static and Const: CSC-210: Object Oriented Programming

This document discusses static and const members in C++ classes. Static members are shared across all objects of a class while const members cannot be modified. The document provides examples of declaring and using static, const, and copy constructors.

Uploaded by

Bilal Asad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CSC-210: Object Oriented Programming

Static and Const


Lecture 05

By: Ms. Zupash Awais


Bahria University Lahore Campus
 A static data member is useful, when all objects of
the same class must share a common
information.
 Just write static keyword prefix to regular variable
Static Data  It is initialized to zero when first object of class
Members created
 Only one copy is created for each object
 Its lifetime is entire program
 Data members of the class which are shared by all objects
are known as static data members.
 Only one copy of a static variable is maintained by the
class, and it is common for all objects.
 Static members are declared inside the class and
defined outside the class.

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";
} }
}; };

int main() int main()


{ {
int x = 0; int x = 0;
if (x == 0) if (x == 0)
{ {
Life obj; static Life obj;
} }
cout << "End of main\n"; cout << "End of main\n";
} }
 The const member functions are the functions which are
declared as constant in the program.
 The object called by these functions cannot be modified.
 It is recommended to use const keyword so that accidental
changes to object are avoided.

Const Members  Const data members must be initialized by the


constructor using an initialization list.
 Once initialized, a const data member may never be
modified, not even in the constructor or destructor.

 In Java you make constant variable using static final


keyword
class Constant
{
const int i = 10;
public:
Constant() :i(1) {}
void display()
{

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.

Const Methods  A const method can be overloaded with a non-


const version.
 Constructors and destructors can never be declared
as const. They are always allowed to modify an object
even if the object is const.
#include<iostream>
using namespace std;
class Constant
{
const int i = 10;
int j;
public:
Constant() :i(1),j(0) {}
void display() const
{
Example cout << i<<“ “<<j<<endl;
}
};

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();
}
}
};

You might also like