1. Do we have static classes in C++?
2. If so what are they useful for?
3. Is this the name given to classes that should not be allowed instantiation?
4. If so, how is it different from abstract classes?

5. Is this homework? ;)

Why don't you write out what you think the answers are and someone will check them over for you.

just give your ideas whatever comes to your mind about your questions and if it is not correct we will resolve it

OK :) This isn't homework. But someone from Java background asked me the first question and I was stumped. Question 3 is my guess regarding the same. Question 2,4 are subsequent doubts that came to my mind...

Okay. The short answer to all of them is no. There are some different ways to get behavior that is similar. This thread on stack overflow talks about some of those things. http://stackoverflow.com/questions/9321/how-do-you-create-a-static-class-in-c.

To get a class that is not allowed instantiation, you need a pure virtual function to make that class abstract (see http://www.learncpp.com/cpp-tutorial/126-pure-virtual-functions-abstract-base-classes-and-interface-classes/ for a great explanation).

1. Do we have static classes in C++?

Since this question was given to you by a Java programmer, you have to consider what a static class is in Java and then see if C++ supports the same behavior. In Java, a static modifier is only allowed on nested classes, and the resulting behavior is that the nested class can be used without an object of the nesting class:

class Outer {
    public static class StaticClass {}
    public class InnerClass {}
}

class Test {
    // Works
    private Outer.StaticClass _staticObj = new Outer.StaticClass();

    // Error
    private Outer.InnerClass _innerObj = new Outer.InnerClass();

    // Outer object required to use InnerClass
    private Outer dummy = new Outer();
    private Outer.InnerClass _innerObj2 = dummy.new InnerClass();
}

Java's static behavior is what C++ uses by default, so static classes in this context aren't necessary:

class Outer {
public:
    class StaticClass {};
};

class Test {
    Outer::StaticClass _staticObj;
public:
    Test(): _staticObj() {}
};

It's actually the inner class behavior from Java that isn't available in C++. ;)

3. Is this the name given to classes that should not be allowed instantiation?

Not in Java. Other languages apply different meanings to static when applied to a class. C#, for example, uses your interpretation. A static class in C# cannot be instantiated and contains only static members. In C++ that would be accomplished by making all members static and declaring a private constructor.

4. If so, how is it different from abstract classes?

An abstract class can be inherited from (that's the point, really). A static class, at least where static means a class with only static members that can't be instantiated, cannot be used as a base class.

commented: Nicely tied together :) +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.