Nested Classes and Inheritance within Nested Classes C++

149 views Asked by At

Currently, I am trying to experiment with the uses of nested classes and inheritance within in these nested classes. The experiment I am running is shown below. In the code below, I have a class Outside and within Outside there is a class called Father. Additionally, I created two classes, Child A and Child B where they are derived from Father. I numbered the issues and concerns that I am facing below:

  1. In the class Outside, I notice that I get an error when I try to create the member variables dad, son, and daughter with Father dad, Child A and Child B, respectively, within Outside. Namely, I get an error saying incomplete datatype is not allowed. Judging from this, it looks the program was not able to see the constructor or the instructions of those objects. In some examples, namely this video https://www.youtube.com/watch?v=C5X7h6h_tks at 2:06, the nested object can be a member variable within Outside. Why is this method legal and why is my method not? If I simply write:

    class Father;
    class ClassA;
    class ClassB;
    

    Wouldn't that indicate to the compiler that Father, ChildA, ChildB exists inside Outside and that I should be able to create a member variable of these classes within Outside?

  2. Also, within the member function, awesome, in class Outside, I am able to declare a variable Father, but not ChildA and ChildB. Why is that? Is there even a way to declare ChildA and ChildB variables within awesome or in the declaration of Outside.

  3. Talking about the member function awesome again, I notice that I cannot do

      Father* ptr; 
      ptr = &dummyB; 
      //Error: A value of type "Outside::ChildA *" cannot be assigned an entity of type
    

    However, if I do it in the main function, I do no get an error. Why is that? Additionally, I get a similar error when I try

    dad_ptr = a_ptr;
    

    where dad_ptr and a_ptr are member variables of Outside. Is there a way to change the assignment of a pointer to an object to its child. My main objective in this experiment is Change the assignment of a pointer of nested object within the object that it is enclosed.

  4. Within the member function something_crazy in the class ChildB, the following is illegal:

    dad_ptr = a_ptr;
    

    Since this class is derived from Father, which is within Outside, shouldn't it have access to the member function and variables of Outside as well? How can I edit my code so that these classes can have access to Outside. Even if they do have access to the member functions, is this even possible to do. If not why?

    #include <iostream>
    #include <string>
    using namespace std;
    class Outside{
    
        public:
            int god;
        class Father;
        class ChildA;
        class ChildB;
        Outside(){
            //dad_ptr = new Father;
            cout << "Outside is envoked";
        }
        void run(){
            //ChildA lol;
        }
        void awesome();
    
    public:
    
        Father* dad_ptr;
        ChildA* a_ptr;
        ChildB* b_ptr;
    
        Father dad;// Error: incomplete datatype not allowed;
        ChildA son;//Error:incomplete datatype not allowed;
        ChildB daughter; //Error:incomplete datatype not allowed;
    
    
    public:
    
    };
    
    
    class Outside::Father{
    public:
        Father(){
            data = "Dad";
    
        }
        string data;
        virtual void something(){
            cout << "Father said something stupid";
        }
    
    
    };
    
    
    
    class ChildA : public Outside::Father{
    public:
        ChildA() {
            data = "Child A";
        }
        void something(){
            cout << "Child A said something stupid";
        }
    
    
    };
    
    class ChildB : public Outside::Father{
    public:
        ChildB() {
            data = "Child B";
        }
        void something(){
            cout << "Child B said something stupid";
        }
    
        void something_crazy(){
            dad_ptr = a_ptr; //Since Father is a nested of Outside, shouldn't a derive class have access to the objects of Outside.
        }
    
    };
    
    
    void Outside::awesome(){
        Father something; //Why can I create a variable father inside a member function, but cannot create it in the declaration of the class? Why can I create father, but not son or daughter?
        /*This cannot be done at all*/
        ChildA dummyA;
        ChildB dummyB;
        Father* ptr;
        ptr = &dummyB;
    
    
        //What I really want.
        dad_ptr = a_ptr; //Error: A value of type "Outside::ChildA *" cannot be assigned an entity of type "Outside::Father*";
    
    }
    
    int main(){
        ChildA dummyA;
        ChildB dummyB;
        Outside::Father* dad_pointer;
        ChildA* another_ptr;
        another_ptr = &dummyA;
        ptr = another_ptr;
        //ptrB = &dummyA;
        ptr = &dummyA;
        ptr->something_stupid();
        cout << endl;
        ptr = &dummyB;
        ptr->something_stupid();
        return 0;
    }
    
0

There are 0 answers