Inheritance- how to access base class member from derived class member?
-
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()
andb()
are inherited without further effort. Which means if I create an objectderived d
, thenint i = d.a();
is a valid expression. Do I have to "explicitly inherit"a()
andb()
in order to use them insum()
? 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 -
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()
andb()
are inherited without further effort. Which means if I create an objectderived d
, thenint i = d.a();
is a valid expression. Do I have to "explicitly inherit"a()
andb()
in order to use them insum()
? 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. MZRno need to any further steps. as you've publically inherited from base class, the public and protected attributes and functions are part of derived class. <blockquote class="FQ"><div class="FQA">Mike the Red wrote:</div>int sum() { return baseclass->a() + baseclass->b(); }; // I DO know it's not done like this...</blockquote> It should be
int sum() { return baseclass::a() + baseclass::b(); };
is it really not possible for you to call d.a()? what's the problem you're facing?-Sarath. "Great hopes make everything great possible" - Benjamin Franklin
My blog - Sharing My Thoughts
-
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()
andb()
are inherited without further effort. Which means if I create an objectderived d
, thenint i = d.a();
is a valid expression. Do I have to "explicitly inherit"a()
andb()
in order to use them insum()
? 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. MZRMike the Red wrote:
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.
What's wrong with:
class baseclass
{
public:
baseclass(){}
baseclass(int i){}
~baseclass(){}
public:
int a() {return 1;}
int b() {return 2;}
};class derived : public baseclass
{
public:
derived(){}
derived(int i):baseclass(i){}
public:
int sum()
{
return a() + b();
}
};"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
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()
andb()
are inherited without further effort. Which means if I create an objectderived d
, thenint i = d.a();
is a valid expression. Do I have to "explicitly inherit"a()
andb()
in order to use them insum()
? 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...Error Between Keyboard and Chair... I get a lot of these... :doh: Thanks, guys!