Search code examples
c++oopinheritancefunction-prototypes

How to prototyply declare an inherited class in C++?


I am making a project, which requires multiple classes, some of them are inherited!! So i am using one inherited class in a main class before it's definition! Kindly help me to how to use prototype declaration for inherited class.thanks!!

Example:

  class A
{
  /*data*/
};
  class B:public A 
 {public:
   void func()
   {
       C obj;
   }
   };
class C:public B
{
};`

Solution

  • Only functions have prototypes. What you can do with class is to forward declare it.

    class MyFutureClass;
    struct MyFutureStruct;
    

    These is an incomplete classes. No, that structs in C++ are same as classes, just they have public access by default, classes have private. To make them complete you should provide definition anywhere further in code.

    Incomplete class is just one possible incomplete type. The following types are incomplete types:

    • type void;
    • class type that has been declared but not defined;
    • array of unknown size;
    • array of elements of incomplete type;
    • enumeration type until its underlying type is determined.

    Now what you can use incomplete class for?

    1. declare a pointer variable or class field
    2. Declare a function or method that accepts argument or returns of incomplete type, without defining it.

    what you can't do with it?

    1. Inherit incomplete class by other class
    2. Define objects fields of incomplete type
    3. Declare non-static class data member of incomplete type
    4. Use incomplete class type as parameter for template
    5. Define functions that use argument of incomplete type or
    6. Perform implicit or explicit conversion to incomplete class, implicit or explicit, lvalue-to-rvalue conversions, standard conversion, dynamic_cast, or static_cast to pointer or reference of incomplete type, except when converting from the null pointer constant or from a pointer to void (void* always can be converted and void never is defined)
    7. Access incomplete class' members or methods
    8. Use new expressions that create object of incomplete type
    9. Use it in catch-clause
    10. Use typeid, sizeof, or alignof operator
    11. Use arithmetic operator on pointer to incomplete class (because sizof isn't known , of course)

    What it is needed for?

    1. To avoid cyclic dependency of headers, when one class depends on existence and functionality of other
    2. To reduce amount of headers included in single file. You can define class that got pointers and formal parameters of declared class without defining it

    let's see, this is an absolutely useless example of avoid cyclic use of headers.

    [class_a.h]

    class B;
    
    class A
    {
        B* p;
    public:
        A ( B& value );
    };
    

    [class_b.h]

    #include "class_a.h"
    
    class B : public A   // we need complete A to inherit it
    {
        int a;
    public:
        B ( const B& );
    };
    

    [class_a.cpp]

    #include "class_b.h"
    
    // to define this constructor we need complete A, but header with complete 
    // definition is already included within class_b.h header
    
    A::A ( B& val) : p( new B(val))   // we created copy of B
    {
    }
    

    So, to define class A we need class B, but class B is child of class A, thus it needs complete definition of A. Conundrum? No. Thanks to forward declaration, we can define class A without defining B.

    (PS. If someone reviewed code and found something impossible there, my explicit allowance to fix it is due... I'm falling asleep)