Hi,

what are the classes that cannot be instantiated in c++?

I know that one of the ans is Abstract base class.what is the other one in c++?

could you please help us?

A class that has _any_ pure virtual function cannot be instantiated directly. So not only complete Abstract classes...

class myBase
{
    virtual void pureVirtual() = 0;
    void normal() { };
};

class myClass : public myBase
{
    void pureVirtual() { }
};

int main()
{
    myBase base(); // Error, cannot be instantiated
    myClass class(); // Fine
}

@thelamb, any class that has at least one pure virtual method in its declaration or in any of its ancestors (base classes) that has not be overriden (provided an implementation for) is by definition an "Abstract Class". So I don't know what you mean by "complete abstract class" (I guess you mean what Java people call an interface class, which has no data members, no implemented methods, but only pure virtual methods).

One other example of a class that cannot be instantiated is an incomplete class which holds a data member of a type (by value) that was forward-declared but whose actual declaration is never found later. But I don't believe any compiler will actually compile this, but neither will it compile an instantiation of an abstract class either.

Another example is a class that is not POD (Plain Old Data) and that does not have any constructor declared for it (because the compiler will not be able to provide a default constructor is the data in the class it not simple plain old data).

Or a class that has a private default constructor and neither a public constructor nor a public static method to call the private constructor. (that trick btw can be quite useful sometimes)

"So I don't know what you mean by "complete abstract class" (I guess you mean what Java people call an interface class, which has no data members, no implemented methods, but only pure virtual methods)."

What else could I possibly mean?

@thelamb nothing else, you're right.. but "complete abstract class" is not a term that people use (at least not in C++), so I was merely remarking the fact that using imprecise terminology is not an effective way to communicate in this field. And also the OP wants "other" classes beside abstract classes that cannot be instantiated, so that didn't really answer the question.

I think in code it makes more sense, here are all the cases I can think of:
Notice cases B, C, E and F, they are all valid classes that cannot be instantiated.

#include <iostream>

class simplePOD {
  public:
    int valueInt;
    double valueDouble;
};

class notPOD {
  public:
    simplePOD& someRef;
};

class privCtorClass {
  private:
    int someValue;

    privCtorClass() : someValue(42) { };

};

class privCtorClassWithStatic {
  private:
    int someValue;

    privCtorClassWithStatic() : someValue(42) { };
  public:
    static privCtorClassWithStatic* Create() { 
      return new privCtorClassWithStatic(); 
    };
};

class abstractBase {
  public:
    virtual void somePureVirtualMethod() = 0;
};

class abstractDerived : public abstractBase {
  public:
    virtual void anotherMethod() { 
      std::cout << "Hello!" << std::endl; 
    };
};

/* This will not compile with error class 'useForwardDeclared' is incomplete because of member 'someObject' or incomplete type 'forwardDeclared'
class forwardDeclared;

class useForwardDeclared {
  public:
    forwardDeclared someObject;
};

//No real declaration of forwardDeclared.
*/

int main() {
  simplePOD* A = new simplePOD(); //This works fine.

  notPOD* B = new notPOD(); //This does NOT work.
  
  privCtorClass* C = new privCtorClass(); //This does NOT work.

  privCtorClassWithStatic* D = privCtorClassWithStatic::Create(); //This works fine.

  abstractBase* E = new abstractBase(); //This does NOT work.

  abstractDerived* F = new abstractDerived(); //This does NOT work.
};

i would like to know the name of the class.

Well.. it looks like you are looking for a quick answer for an assignment "short-answer" question and that you are not actually interested in understanding what makes a class not instantiatable (that's not a word but whatever).

So that's where I will stop helping you...

Thanks for your reply

hey i would like to know the class name then only i can do and try it out.
example:
"if the class is having the pure virtual function then the class will behave as a abstract base class,it cannot be instantiated also".

I gave answer for one class,like this i want .So that i can do further R&D on this.

Help me please

"If a class has only a private constructor, that constructor cannot be access and thus cannot be instantiated."

"If a non-POD class has no constructor, that class cannot be instantiated."

I though that was pretty clear from my explanation and examples.
Look up, "private constructors" and "non-POD classes" and you should find plenty of explanations on those concepts.

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.