Thread: inherits what?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    inherits what?

    if i have a parent class ( class PARENT)
    that is full of pure virtual functions
    and then another class (class TEEN) that inherits from that class which also contains a few virtual functions and a few normal functions, and then finally have a class (class CHILD) that inherits from TEEN. does class CHILD have to implement the virtual functions that were listed in both PARENT and TEEN?
    and does TEEN have to implement the functions from PARENT or not?

    Code:
    //an example of what i mean
    
    class PARENT {
    public:
      virtual void func1() {};
      virtual void func2() {};
      virtual void func3() {};
    };
    
    class TEEN : public PARENT{
    public:
      //do i add all the virtual functions from PARENT here?
      // void func1();   or virtual void func1();
      // void func2();   or virtual void func2();
      // void func3();   or virtual void func3(); 
      void func4();
      void func5();
      virtual void func6();
      virtual void func7();
    
    };
    
    class CHILD : public TEEN {
    public:
       //and so in here which functions need to be implemented?
    };

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    mmm if it's declared, it means that it provides an implementation for the other classes but that it can be changed through the other class ( wich couldn't be done otherwise, you can't overload a function with the exact same parameters )... besides I'm wondering why you made PARENT pure virtual, it makes it impossible to create a PARENT...

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Teens these days with their void functions...

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    Code:
    class CHILD : public TEEN {
    public:
       //and so in here which functions need to be implemented?
    };
    func8( ), func9( ) and func0( ) lol...
    As for what needs to be implemented, it really depends on your functions... for exemple, in a game I'm making, the function Update( ) is implemented in every single derivated class

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    Teens these days with their void functions...
    hehe void functions are easy to type no thinking invovled.. hahah

    hrm so since PARENT is fully virtual, then does that mean i will have to implement its functions in either TEEN or CHILD or both or some in one and some in the other depending on what i do..?

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Unless it marries an instance of the same type and has to move to Califronia before having an orphan class inherit it's properties.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    Every function that is pure virtual have to be implemented in derived classes, a function that is virtual CAN be implemented in derived classes and regular functions can only be overloaded

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    ok thanks got it

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quote Originally Posted by tilex
    Every function that is pure virtual have to be implemented in derived classes, a function that is virtual CAN be implemented in derived classes and regular functions can only be overloaded
    (Very) minor clarification: Pure virtual functions must be implemented in the derived class only if the derived class is to be instantiated. That is to say, you can have multiple levels of abstract classes in your hierarchy.

  10. #10
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by paperbox005
    if i have a parent class ( class PARENT)
    that is full of pure virtual functions
    [...]
    Code:
    //an example of what i mean
    class PARENT {
    public:
      virtual void func1() {};
      virtual void func2() {};
      virtual void func3() {};
    };
    note that these are NOT pure virtual functions, you have given each of them a function definition. Thus inheriting classes would not have to implement them. If you want pure virtual functions you need to do this...

    Code:
    class PARENT {
    public:
      virtual void func1() = 0;
      virtual void func2() = 0;
      virtual void func3() = 0;
    };

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    Quote Originally Posted by Zach L.
    (Very) minor clarification: Pure virtual functions must be implemented in the derived class only if the derived class is to be instantiated. That is to say, you can have multiple levels of abstract classes in your hierarchy.
    Thank you for correcting me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual Table
    By C-RaT in forum C++ Programming
    Replies: 21
    Last Post: 09-05-2008, 12:38 PM
  2. Multilevel Inheritance
    By duvernais28 in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2006, 05:57 PM