LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Inheritance question (https://www.linuxquestions.org/questions/programming-9/inheritance-question-476542/)

kornerr 08-23-2006 07:11 AM

Inheritance question
 
I have:
Code:

class A {
};

Code:

class B: public A {
};

Code:

class C {
    public:
        void Update ();
    protected:
        A *a;
};

Code:

void C::Update () {
    a->Check ();
}

And:
Code:

class D: public C {
    protected:
        B *a;
};

I want to be able to call D::Update () so that B *a is updated, not A *a.
Is there any way to accomplish this?
And I don't want to use C::Update (A* a) which will defenitely work if I pass B* a to it.
Thanks.

cupubboy 08-23-2006 07:33 AM

I'm not sure I understand
why don't you override it??? .. Declare and define a D::Update .. not that's a very good desing .. but ..

xhi 08-23-2006 07:41 AM

yeh, override the update() function in D..

in C, make Update() virtual

then in D, define an Update()

kornerr 08-23-2006 08:36 AM

Ok, this is kinda ugly, but it does what I want. Overriding is what I escape since I want to have one base class that contains common functions so that I can use them from derived classes.
Code:

#include <iostream>
using namespace std;

class A {
        public:
                void AFunc () {
                        cout << "AFunc\n";
                }
};

class B: public A {
        public:
                void BFunc () {
                        cout << "BFunc\n";
                }
};

class C {
        public:
                C () {
                        a = new A ();
                }
                void Update () {
                        a->AFunc ();
                }
        protected:
                A *a;
};

class D: public C {
        public:
                D () {
                        b = new B ();
                        a = b;
                }
        protected:
                B *b;
};

int main () {
        D d;
        d.Update ();

        return 0;
}


dmail 08-23-2006 09:55 AM

Hmm thats nasty code, heres a few points:
Any class which is a base for another class should have a virtual destructor.
You create a D which calls the constructor C which creates a pointer. D then creates a new pointer and assigns it to the pointer created by C; Memory leak. There are two copies of the pointer in two classes which is responsible for the destruction? Neither clean up memory leak.

sarutobi 08-23-2006 11:25 PM

Hmm... I wrote some Java code that solve problem. But I didn't understand why upcast isn't work in C++.
Code:

public class A {

        public A() {
                super();
                msg = "A";
        }
       
        protected String msg;
       
        public String get(){
                return msg;
        }

}
public class B extends A{
       
        public B() {
                super();
                msg = "B";
        }
       
        public String test(){
                return "Test";
        }

}

public class C {
        protected A a;
        public C() {
                super();
                a = new A();
        }
       
        public void Update(){
                System.out.println(a.get());
        }

}

public class D extends C{

        public D() {
                super();
                a = new B();
       
        }

        public void Update(){
                super.Update();
                System.out.println(((B)a).test()); //<- This is upcast a to B.
        }
}
        public static void main(String[] args){
                C c = new C();
                c.Update();
                D d = new D();
                d.Update();
        }
}

Can anybody write C++ code from my Java template? :)

xhi 08-24-2006 07:38 AM

> But I didn't understand why upcast isn't work in C++.

you cant cast from an A to a B, a B to an A sure, but not an A to a B.. :)

edit> oops. sorry i didnt see in D where you say a = new B(), i take back my previous statement, your cast should work..


All times are GMT -5. The time now is 07:37 AM.