Static and Const: CSC-210: Object Oriented Programming
Static and Const: CSC-210: Object Oriented Programming
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;
}
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.
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 getdata(int a){
number = a;
count++; int main()
} {
static void getcount(){ item a,b,c;
cout<<”value of count: “<<count;
} a.getdata(100);
}; item::getcount();
int item :: count; // static variable definition
b.getdata(200);
Output: item::getcount();
value of count: 1 c.getdata(300);
value of count: 2 item::getcount();
value of count: 3 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:
#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();
}
}
};
Friend Functions
In C++ a Friend Function that is a "friend" of a given class is allowed access
to private and protected data in that class.
A friend function is a function which is declared using friend keyword.
Friend Function
Class Class
class A class B
{ {
private: private:
int numA; Friend Function int numB;
public: void add() public:
void setA(); { void setB();
friend void add(); Access friend void add();
}; numA, numB };
}
Friend Function
Friend function can be declared either in public or private part of the class.
It is not a member of the class so it cannot be called using the object.
Usually, it has the objects as arguments.
Syntax:
class ABC
{
public:
……………………………………………
friend void xyz(argument/s); //declaration
……………………………………………
};
Program: Friend Function
class numbers {
int num1, num2;
public:
void setdata(int a, int b);
friend int add(numbers N);
};
void numbers :: setdata(int a, int b){
num1=a;
int main()
num2=b;
{
}
numbers N1;
int add(numbers N){
N1.setdata(10,20);
return (N.num1+N.num2);
} cout<<”Sum = ”<<add(N1);
return 0;
}
class Box { Program: Friend Function
double width;
public:
friend void printWidth( Box );
void setWidth( double wid );
};
void Box::setWidth( double wid ) {
width = wid;
}
void printWidth(Box b) {
cout << "Width of box : " << b.width;
}
int main( ) {
Box box;
box.setWidth(10.0);
printWidth( box );
return 0;
}
Use of friend function