c++ interview questions on static classes, static initializer, and static members
What is static method? What is a static initializer?
A static method is not bound to an instance of a class. It does not need a class instance to access it. But to access other (non-static) members of the class from the static method, we need to declare a class instance inside static method.
plaese explain in detail with example,how can we acess the member of class without instance????
Static Initializer Block is concept of Java. Do we really have this in C++?
Is it same as Initialization of Static variables.
Question didn’t ask about static specific to classes.
We can explain a static method in terms of a class or as a normal non-class function.
A staic method in a class’s role is to use the static data members only.
and staic method can be accessed by a static/non-static class object.
But a static method cannot have non-static access to the data-members.
And static method cnnot be constant, volatile and virtual.
Now, static non-class method: A staic keyword in front of a non-class method is used to limit the method scope to the current compilation unit. It has internal linkage and one can use this principal in C for information hiding, while class’s static method/data-member has external-linkage.
static void print_some_data()
is limited in abc.cpp/c only, no other compilation unit in an application can use it.
we use static so tht there is no need to acces tht method or data thro an object..
eg. in Java,C# we use..
public static void main(String args)
bcoz de prog is started from main, the compiler vil not create an object to access this method and from here the execution begins..
What are they?
Their reason of existence?
Challenges they have? (Link initilization)……
Static data members are members are that shared by all instances of a class.
A common usage is to keep account of the numbers of instances of a class.
Static member functions are used to access private static data members, they do not have a this pointer and they can only access static data members, this is because they are not attached to an instance of an object
why use static variables, where are the static variables stored,
it is similar to a brainbench test with 4 possible answers to each question.
1) o/p of a program containing two two static variables with the same name in a program
What is “static” mean in the c++?
Was probably looking for you to mention it’s allocation at the start of the program, one copy in memory, etc.
1. Within a function, there is one copy of the variable that retains its value even when the function returns.
2. Within a class, all objects share the static variable and the static variable is accessed using the class name.
3. In the global scope a static variable is local to the current file.
“Static variables and static functions. Local static variables within a function and their use.”
How do you access the static member of a class?