472,809 Members | 4,947 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,809 software developers and data experts.

Base class calling derived class methods

Suppose I have a base class "foo". Another class, "bar" derives from it.
Base class "foo" has a method called "rob_the_liquor_store()", and the
inherited class "bar" overrides this method with one of its own, maybe
specifying the liquor store over on 44th Street and 5th Avenue or something.

Anyway this is what we have so far:

base class: "foo"
|------------method: "rob_the_liquor_store()"
|
|
derived class: "bar" inherits from "foo"
|------------method: "rob_the_liquor_store()"

Now the question: If I add another method to "foo" (in keeping with
character, let's call it "commit_a_felony()"), can I specify that it should
call the inherited class's ("bar's") "rob_the_liquor_store()" method instead
of the base class's? If so, how?

Thanks
Sep 13 '06 #1
8 1960
Hi Mike,

"Mike C#" <xy*@xyz.comwrote in message
news:Oq**************@TK2MSFTNGP02.phx.gbl...
Suppose I have a base class "foo". Another class, "bar" derives from it.
Base class "foo" has a method called "rob_the_liquor_store()", and the
inherited class "bar" overrides this method with one of its own, maybe
specifying the liquor store over on 44th Street and 5th Avenue or
something.

Anyway this is what we have so far:

base class: "foo"
|------------method: "rob_the_liquor_store()"
|
|
derived class: "bar" inherits from "foo"
|------------method: "rob_the_liquor_store()"

Now the question: If I add another method to "foo" (in keeping with
character, let's call it "commit_a_felony()"), can I specify that it
should call the inherited class's ("bar's") "rob_the_liquor_store()"
method instead of the base class's? If so, how?
Technically yes, but if you are in such a situation to need this is looks
like you should reconsider your class design.

First: you have to be sure that while you are in a method of foo your
instance is at least a bar instance or further derived otherwise your bar
method can "get upset" as it executes without the proper class layout it
expects. E.g. if bar has an instance member this will only be valid if you
have a bar instance otherwise it will be random memory.

So you can but should not do this in a foo method:
((bar*)this)->commit_a_felony()

--
SvenC
Thanks

Sep 14 '06 #2
OK, thanks. The reason I ask is because I have classes that actually look
more like this:

base class: "foo"
|------------method: connect_to_db()
|
|>>>>derived class: "bar_1"
| |------------method: init_columns()
|
|>>>>derived class: "bar_2"
| |------------method: init_columns()

The connect_to_db() method of foo is inherited by the bar_n classes. This
method just opens a database connection, and each derived class can use the
common inherited connect_to_db() method without change. The init_columns()
method of each derived class contains some code specific to the bar_n()
object, and this method can't be implemented (afaik) in "foo". I'd like to
cut down on some code and call init_columns() and some other functions that
had to be implemented in foo directly from connect_to_db(), but it's
implemented only in foo. The reason I'd like to do this is that I'm
noticing a lot of repetitive code in some of these methods, which I'd like
to eliminate.

Thanks

"SvenC" <Sv***@community.nospamwrote in message
news:OE**************@TK2MSFTNGP02.phx.gbl...
Hi Mike,

"Mike C#" <xy*@xyz.comwrote in message
news:Oq**************@TK2MSFTNGP02.phx.gbl...
>Suppose I have a base class "foo". Another class, "bar" derives from it.
Base class "foo" has a method called "rob_the_liquor_store()", and the
inherited class "bar" overrides this method with one of its own, maybe
specifying the liquor store over on 44th Street and 5th Avenue or
something.

Anyway this is what we have so far:

base class: "foo"
|------------method: "rob_the_liquor_store()"
|
|
derived class: "bar" inherits from "foo"
|------------method: "rob_the_liquor_store()"

Now the question: If I add another method to "foo" (in keeping with
character, let's call it "commit_a_felony()"), can I specify that it
should call the inherited class's ("bar's") "rob_the_liquor_store()"
method instead of the base class's? If so, how?

Technically yes, but if you are in such a situation to need this is looks
like you should reconsider your class design.

First: you have to be sure that while you are in a method of foo your
instance is at least a bar instance or further derived otherwise your bar
method can "get upset" as it executes without the proper class layout it
expects. E.g. if bar has an instance member this will only be valid if you
have a bar instance otherwise it will be random memory.

So you can but should not do this in a foo method:
((bar*)this)->commit_a_felony()

--
SvenC
>Thanks


Sep 14 '06 #3
Hi Mike,

"Mike C#" <xy*@xyz.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
OK, thanks. The reason I ask is because I have classes that actually look
more like this:

base class: "foo"
|------------method: connect_to_db()
|
|>>>>derived class: "bar_1"
| |------------method: init_columns()
|
|>>>>derived class: "bar_2"
| |------------method: init_columns()

The connect_to_db() method of foo is inherited by the bar_n classes. This
method just opens a database connection, and each derived class can use
the common inherited connect_to_db() method without change. The
init_columns() method of each derived class contains some code specific to
the bar_n() object, and this method can't be implemented (afaik) in "foo".
I'd like to cut down on some code and call init_columns() and some other
functions that had to be implemented in foo directly from connect_to_db(),
but it's implemented only in foo. The reason I'd like to do this is that
I'm noticing a lot of repetitive code in some of these methods, which I'd
like to eliminate.
This is how you solve it in a correct way

class foo
{
public:
virtual void init_columns() = 0; // = 0 means pure virtual so derivers
must implement this
void connect_to_db();
};

class bar_1 : public foo
{
public:
void init_columns()
{
// implement the bar_1 version
}
};

void main()
{
foo *f = new bar_1();
f->connect_to_db(); // calls foo's method
f->init_columns(); // calls bar_1's method
}

So that is a classic use case for pure virtual functions.

--
SvenC
Thanks

"SvenC" <Sv***@community.nospamwrote in message
news:OE**************@TK2MSFTNGP02.phx.gbl...
>Hi Mike,

"Mike C#" <xy*@xyz.comwrote in message
news:Oq**************@TK2MSFTNGP02.phx.gbl...
>>Suppose I have a base class "foo". Another class, "bar" derives from
it. Base class "foo" has a method called "rob_the_liquor_store()", and
the inherited class "bar" overrides this method with one of its own,
maybe specifying the liquor store over on 44th Street and 5th Avenue or
something.

Anyway this is what we have so far:

base class: "foo"
|------------method: "rob_the_liquor_store()"
|
|
derived class: "bar" inherits from "foo"
|------------method: "rob_the_liquor_store()"

Now the question: If I add another method to "foo" (in keeping with
character, let's call it "commit_a_felony()"), can I specify that it
should call the inherited class's ("bar's") "rob_the_liquor_store()"
method instead of the base class's? If so, how?

Technically yes, but if you are in such a situation to need this is looks
like you should reconsider your class design.

First: you have to be sure that while you are in a method of foo your
instance is at least a bar instance or further derived otherwise your bar
method can "get upset" as it executes without the proper class layout it
expects. E.g. if bar has an instance member this will only be valid if
you have a bar instance otherwise it will be random memory.

So you can but should not do this in a foo method:
((bar*)this)->commit_a_felony()

--
SvenC
>>Thanks



Sep 14 '06 #4
Thanks SvenC, tell me though - will this pattern work? This is a little
closer to what I'm trying to do:

class foo
{
public:
void connect_to_db();
protected:
virtual void init_columns() = 0;
};

void foo::connect_to_db()
{
this->init_columns();
}

class bar_1 : public foo
{
public:
void init_columns()
{
// implement the bar_1 version
}
};

class bar_1 : public foo
{
public:
void init_columns()
{
// implement the bar_2 version
}
};

void main()
{
foo *f1 = new bar_1();
f1->connect_to_db();
foo *f2 = new bar_2();
f2->connect_to_db();
}

I noticed in main() you declare a pointer to foo and instantiate it as a
bar_n type. I just want to make sure I'm reading that correctly :) Thanks
again!

"SvenC" <Sv***@community.nospamwrote in message
news:up**************@TK2MSFTNGP05.phx.gbl...
Hi Mike,

"Mike C#" <xy*@xyz.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>OK, thanks. The reason I ask is because I have classes that actually
look more like this:

base class: "foo"
|------------method: connect_to_db()
|
|>>>>derived class: "bar_1"
| |------------method: init_columns()
|
|>>>>derived class: "bar_2"
| |------------method: init_columns()

The connect_to_db() method of foo is inherited by the bar_n classes.
This method just opens a database connection, and each derived class can
use the common inherited connect_to_db() method without change. The
init_columns() method of each derived class contains some code specific
to the bar_n() object, and this method can't be implemented (afaik) in
"foo". I'd like to cut down on some code and call init_columns() and some
other functions that had to be implemented in foo directly from
connect_to_db(), but it's implemented only in foo. The reason I'd like
to do this is that I'm noticing a lot of repetitive code in some of these
methods, which I'd like to eliminate.

This is how you solve it in a correct way

class foo
{
public:
virtual void init_columns() = 0; // = 0 means pure virtual so derivers
must implement this
void connect_to_db();
};

class bar_1 : public foo
{
public:
void init_columns()
{
// implement the bar_1 version
}
};

void main()
{
foo *f = new bar_1();
f->connect_to_db(); // calls foo's method
f->init_columns(); // calls bar_1's method
}

So that is a classic use case for pure virtual functions.

--
SvenC
>Thanks

"SvenC" <Sv***@community.nospamwrote in message
news:OE**************@TK2MSFTNGP02.phx.gbl...
>>Hi Mike,

"Mike C#" <xy*@xyz.comwrote in message
news:Oq**************@TK2MSFTNGP02.phx.gbl...
Suppose I have a base class "foo". Another class, "bar" derives from
it. Base class "foo" has a method called "rob_the_liquor_store()", and
the inherited class "bar" overrides this method with one of its own,
maybe specifying the liquor store over on 44th Street and 5th Avenue or
something.

Anyway this is what we have so far:

base class: "foo"
|------------method: "rob_the_liquor_store()"
|
|
derived class: "bar" inherits from "foo"
|------------method: "rob_the_liquor_store()"

Now the question: If I add another method to "foo" (in keeping with
character, let's call it "commit_a_felony()"), can I specify that it
should call the inherited class's ("bar's") "rob_the_liquor_store()"
method instead of the base class's? If so, how?

Technically yes, but if you are in such a situation to need this is
looks like you should reconsider your class design.

First: you have to be sure that while you are in a method of foo your
instance is at least a bar instance or further derived otherwise your
bar method can "get upset" as it executes without the proper class
layout it expects. E.g. if bar has an instance member this will only be
valid if you have a bar instance otherwise it will be random memory.

So you can but should not do this in a foo method:
((bar*)this)->commit_a_felony()

--
SvenC

Thanks



Sep 14 '06 #5
Mike C# wrote:
Thanks SvenC, tell me though - will this pattern work? This is a little
closer to what I'm trying to do:

class foo
{
public:
void connect_to_db();
protected:
virtual void init_columns() = 0;
};

void foo::connect_to_db()
{
this->init_columns();
}

class bar_1 : public foo
{
public:
void init_columns()
{
// implement the bar_1 version
}
};

class bar_1 : public foo
{
public:
void init_columns()
{
// implement the bar_2 version
}
};

void main()
{
foo *f1 = new bar_1();
f1->connect_to_db();
foo *f2 = new bar_2();
f2->connect_to_db();
}

I noticed in main() you declare a pointer to foo and instantiate it as a
bar_n type. I just want to make sure I'm reading that correctly :) Thanks
again!
Your code looks fine (apart from the memory leaks, and lack of a virtual
destructor on foo - I'm sure you'll fix both problems in the real
code!). This idea is the basis of a design pattern, "Template Method":
http://en.wikipedia.org/wiki/Template_method_pattern

Tom
Sep 14 '06 #6
Cool, thanks for the heads up. Also do I have to declare them like this:

foo *f1 = new bar_1();

Or can I use it like this:

bar_1 b1;
b1.connect_to_db();

Thanks

"Tom Widmer [VC++ MVP]" <to********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Mike C# wrote:
>Thanks SvenC, tell me though - will this pattern work? This is a little
closer to what I'm trying to do:

class foo
{
public:
void connect_to_db();
protected:
virtual void init_columns() = 0;
};

void foo::connect_to_db()
{
this->init_columns();
}

class bar_1 : public foo
{
public:
void init_columns()
{
// implement the bar_1 version
}
};

class bar_1 : public foo
{
public:
void init_columns()
{
// implement the bar_2 version
}
};

void main()
{
foo *f1 = new bar_1();
f1->connect_to_db();
foo *f2 = new bar_2();
f2->connect_to_db();
}

I noticed in main() you declare a pointer to foo and instantiate it as a
bar_n type. I just want to make sure I'm reading that correctly :)
Thanks again!

Your code looks fine (apart from the memory leaks, and lack of a virtual
destructor on foo - I'm sure you'll fix both problems in the real code!).
This idea is the basis of a design pattern, "Template Method":
http://en.wikipedia.org/wiki/Template_method_pattern

Tom

Sep 14 '06 #7
"Mike C#" <xy*@xyz.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Cool, thanks for the heads up. Also do I have to declare them like this:

foo *f1 = new bar_1();

Or can I use it like this:

bar_1 b1;
b1.connect_to_db();
either will work and they'll do exactly the same thing.

-cd
Sep 14 '06 #8
Thanks!

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:%2***************@TK2MSFTNGP06.phx.gbl...
"Mike C#" <xy*@xyz.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>Cool, thanks for the heads up. Also do I have to declare them like this:

foo *f1 = new bar_1();

Or can I use it like this:

bar_1 b1;
b1.connect_to_db();

either will work and they'll do exactly the same thing.

-cd


Sep 14 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A {
5
by: Bob Hairgrove | last post by:
Consider the following: #include <string> class A { public: A( const std::string & full_name , const std::string & display_name) : m_full_name(full_name)
5
by: Dave Veeneman | last post by:
I'm using inheritance more than I used to, and I find myself calling a lot of base class methods. I generally call a base method from a dreived class like this: this.MyMethod(); I'm finding...
1
by: Mark McDonald | last post by:
This question kind of follows on from Mike Spass’ posting 10/11/2004; I don’t understand why you can’t declare an implicit operator to convert a base class to a derived class. The text...
12
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
5
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
3
by: Goran Djuranovic | last post by:
Hi all, Is there a way to retrieve a derived class name inside a subroutine or a function of the base class? I am trying to get some data from the database, based on which derived class is calling...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.