#include <iostream>
usingnamespace std;
class Dealer
{
private:
char mobileno[11];
char dealername[25];
char dealeraddress[51];
char dealercity[25];
char phoneno[11];
public:
staticvoid showID()
{
int companyID=6519;
cout<<"the company id is\t"<<companyID<<endl;
}
void get()
{
cout<<"enter mobile no:"<<endl;
cin>>mobileno;
cout<<"enter dealer name:"<<endl;
cin>>dealername;
cout<<"enter dealer address:"<<endl;
cin>>dealeraddress;
cout<<"enter dealer city:"<<endl;
cin>>dealercity;
cout<<"enter phone no:"<<endl;
cin>>phoneno;
}
void print()
{
cout<<"the mobile no is"<<mobileno<<endl;
cout<<"the dealer name is"<<dealername<<endl;
cout<<"the dealer address is"<<dealeraddress<<endl;
cout<<"the dealer city is"<<dealercity<<endl;
cout<<"the phone no is"<<phoneno<<endl;
}
};
//int Dealer::companyID=6519;
int main()
{
Dealer D;
Dealer::showID();
D.get();
D.print();
return 0;
}
so i wrote this program, i am not able to get how can static void showID a static function access int companyID a normal variable, can someone please explain.
Ok, so you seem to need a more explicit statement: Static methods inside a class can only access static class variables or variables declared inside the static method. The variable companyID is declared inside the static method and is therefore available for use inside this static method.
ok thx then what is the use of static functions ? is the use of static function only being able to access the function without declaring an object like Dealer::showID();
I thinks that their use also has to do with the fact that they are static: they are shared between different instances of this class instead of having each object its own function.
Imagine them as being class specific instead of object specific like most functions