c++ interview questions on static classes, static initializer, and static members

(Last Updated On: May 12, 2010)

c++ interview questions on static classes, static initializer, and static members

Static
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.
e.g.
In abc.cpp/c
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..

Static members,
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.
Static
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?
::

HOW DO YOU START A PROFITABLE TRADING BUSINESS? Read more NOW >>>

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!
This entry was posted in Quant Development and tagged , , , , on by .

About caustic

Hi i there My name is Bryan Downing. I am part of a company called QuantLabs.Net This is specifically a company with a high profile blog about technology, trading, financial, investment, quant, etc. It posts things on how to do job interviews with large companies like Morgan Stanley, Bloomberg, Citibank, and IBM. It also posts different unique tips and tricks on Java, C++, or C programming. It posts about different techniques in learning about Matlab and building models or strategies. There is a lot here if you are into venturing into the financial world like quant or technical analysis. It also discusses the future generation of trading and programming Specialties: C++, Java, C#, Matlab, quant, models, strategies, technical analysis, linux, windows P.S. I have been known to be the worst typist. Do not be offended by it as I like to bang stuff out and put priorty of what I do over typing. Maybe one day I can get a full time copy editor to help out. Do note I prefer videos as they are much easier to produce so check out my many video at youtube.com/quantlabs