What is Static Keyword? Definition in C++ Tamil - linto.in
static என்பது ஒரு keyword அல்லது modifier. இது variable, method(function), constructor, class, properties, operator and event போன்றவைகளில் பயன்படுத்தலாம். அவ்வாறு பயன்படுத்தும்போது அதை access செய்ய object தேவை இல்லை. இதுவே இதன் சிறப்பம்சம்.
static Keyword in C++
static என்பது ஒரு keyword அல்லது modifier. இது variable, method(function), constructor, class, properties, operator and event போன்றவைகளில் பயன்படுத்தலாம். அவ்வாறு பயன்படுத்தும்போது அதை access செய்ய object தேவை இல்லை. இதுவே இதன் சிறப்பம்சம்.
Advantage of C++ static keyword
static keyword-ஐ ஒரு variable அல்லது function-க்கு பயன்படுத்தினால் அது static member ஆக மாறிவிடும். இந்த static member-ஐ access செய்ய object தேவை இல்லை.ஒவ்வொரு முறையும் object create ஆகும்போது memory-ஐ create செய்யாது, ஆகையால் இது memory-யை save செய்கிறது
static variable in C++
ஒரு variable-ஐ static ஆக declare செய்தால் அது static variable என்று அழைக்கபடுகிறது. ஒவ்வொரு object-ஐ create செய்யும்போதும் variable-க்கு memory எடுத்துகொள்ளும். ஆனால் எத்தனை static variable பயன்படுத்தினாலும் அவை அனைத்திருக்கும் ஒரே ஒருமுறை தான் memory எடுத்துகொள்ளும். இந்த memory அனைத்து variable-க்கும் share செய்துகொள்ளும். ஆகையால் இது memory efficient செயல்படுகிறது.
Program for static variable
#include <iostream>
using namespace std;
class Account {
public:
int accno; //data member (also instance variable)
string name; //data member(also instance variable)
static float rateOfInterest;
Account(int accno, string name){
this->accno = accno;
this->name = name;
}
void display(){
cout<<accno<<" "<<name <<" "<<rateOfInterest<<endl;
}
};
float Account::rateOfInterest=12.5;
int main() {
Account a1 =Account(201, "Sanjana"); //creating an object of Employee
Account a2=Account(202, "Kasthu"); //creating an object of Employee
a1.display();
a2.display();
return 0;
}
Output:
201 Sanjana 12.5202 Kasthu 12.5
C++ static variable example for Counting Objects
#include <iostream>
using namespace std;
class Account {
public:
int accno; //data member (also instance variable)
string name;
static int count;
Account(int accno, string name){
this->accno = accno;
this->name = name;
count++;
}
void display(){
cout<<accno<<" "<<name<<endl;
}
};
int Account::count=0;
int main(void) {
Account a1 =Account(201, "Sanjay"); //creating an object of Account
Account a2=Account(202, "Nakul");
Account a3=Account(203, "Ranjana");
a1.display();
a2.display();
a3.display();
cout<<"Total Objects are: "<<Account::count;
return 0;
}
Output:
201 Sanjay202 Nakul
203 Ranjana
Total Objects are: 3