المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Inheritance- how to access base class member from derived class member?



C++ Programming
07-08-2009, 06:22 PM
I'm trying to take an existing class and add a member function to it. Something like this:class baseclass {
public:
baseclass();
baseclass(int i);
~baseclass();
public:
int a() { //Impl...};
int b() { //Impl...};
}
class derived : public baseclass {
public:
derived() : base();
derived(int i) : base(i);
public:
int sum() { return baseclass->a() + baseclass->b(); }; // I DO know it's not done like this...
}I know I have to explicitly declare the constructors, but I'm under the impression that a() and b() are inherited without further effort. Which means if I create an object derived d, then int i = d.a(); is a valid expression.

Do I have to "explicitly inherit" a() and b() in order to use them in sum() ?

I've been all over the documentation and the articles here, but can't seem to track this down.

More nooB questions, I know - any help is appreciated.

MZR