What is Static Variable in c++?
In simple computer codding or in programming, variable that allocated statically called static variable. Static variable saved in memory till program ends . In other words its the variable you define as static and it will store at the same place or location you assign it and it will remain at same place throughout the execution of your whole program.Static Variable in C++ |
Question Asked in Interview "What is Static?"Static Variable is declare : static int totalObject;
Answer:- It is a variable that is declared static in the body of function and it maintains its data or value throughout the function.
Initializing Value, Outside the class e.g int Bank :: totalObject=0; You can check in example below.
Operation: totalObject++; inside the class
What is member Initializer list in c++ and Why need it?
Member intilializer is a constructor used in c++. Instant of using variable inside the constructor here we use it outside the constructor body. Variable are listed after the header of the constructor and it is separated by a colon (:). Every variable is in the form of data member name and parameter from the main function.Member Intitializer |
Example of Member Initializer and Static Variable
#include<iostream.h>class Bank // Class Name
{
private:
const int password; // Constant Integer
char *name; // Pointer Character For Dynamic Charracter Array
int amount; // Simple integer
public:
static int totalObject; // Static Integer
Bank(int pass):password(pass), name(NULL),amount(0) // Member Intilizer For Constant Variable
{
totalObject++; // When Constructor will call it will one to variable
name= new char [20]; // Assigning memory to name
}
// Setters.....
void setName(char *n) // setter for Name
{
name=n;
}
void setAmount(int am) // Setter For Amount
{
amount=am;
}
// Getters
char* getName()
{
return name;
}
int getAmount()
{
return amount;
}
const int getPassword()
{
return password;
}
// Function For Transaction
void Deposit(int DA) // Function To Deposit Amount
{
amount=amount+DA;
cout<<"After you Deposit amount your new balance is:"<<amount<<endl;
}
void withDraw(int wd) // Function to Withdraw and Update Amount
{
amount=amount-wd; // Amount will Here
cout<<"After Withdraw Your New Amount is:"<<amount<<endl;
}
};
// Intiliazing Static variable
int Bank::totalObject=0;
// Main Function
void main ()
{
int deposit,witdraw,password,amount;
char name[20];
// Entering Data For Account
cout<<"Enter User Name: ";
cin>>name;
cout<<"Enter Pssword For Account: ";
cin>>password;
Bank Account(password); // Creating Object and assigning constant value
Account.setName(name);
cout<<"Enter Amount For Your ACCOUNT:";
cin>>amount;
Account.setAmount(amount);
cout<<"Enter Amount To Deposit:";
cin>>deposit;
Account.Deposit(deposit);
cout<<"Enter Amount To Widraw:";
cin>>witdraw;
Account.withDraw(witdraw);
// Displaying Account Information
cout<<"Total Objects In Class::"<<Bank::totalObject<<endl;
cout<<"****Your Complete Account Information*****"<<endl;
cout<<"Account Name ::"<<Account.getName()<<endl;
cout<<"Account Password ::"<<Account.getPassword()<<endl;
cout<<"Amount In Account ::"<<Account.getAmount()<<endl;
}
Output of program
Bank Account |
0 comments: