when writing programs in c++ Why we need to create a class pointer in the header file, And then create the actual class by referring the pointer to the source file using the ‘Create Default Subobject’ function. why can’t we directedly create the class object in the header file ?
Could you write out an example for what you mean by “directly create the class object in the header file?”
Do you mean using an initializer value in the header? That didn’t used to be valid C++ until fairly recently, and even now, there are limitations on what can go in the header initializer.
One reason to not do this, is that in the header, you can make do with a forward class declaration, whereas if you were to try to initialize/allocate the value in the header, you would have to include the full header of that other class. This not only leads to larger include sets for everyone who uses your class, but it can also pretty easily lead to cyclic includes, which are a really bad thing.