Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > accessing parent class method

Reply
Thread Tools

accessing parent class method

 
 
ArbolOne
Guest
Posts: n/a
 
      06-01-2008
In my program I am using two classes foo1, the parent class, and foo2
the child class!
foo2 has overloaded inserter and extractor operator, but so does foo1!
How can I access foo1 inserter or extractor?


Thanks
 
Reply With Quote
 
 
 
 
Erik Wikström
Guest
Posts: n/a
 
      06-01-2008
On 2008-06-01 03:07, Victor Bazarov wrote:
> ArbolOne wrote:
>> In my program I am using two classes foo1, the parent class, and foo2
>> the child class!
>> foo2 has overloaded inserter and extractor operator, but so does foo1!
>> How can I access foo1 inserter or extractor?

>
> Post your code. It is easier when there is substance to the discussion.
>
> Meanwhile, here is the example:
>
> struct foo {
> void foobar();
> };
>
> struct bar : foo {
> void foobar();
> };
>
> int main() {
> bar b;
> b.foobar(); // calls bar::foobar
> b.foo::foobar(); // call foo::foobar
> }


I would call it bad style to explicitly call a parent's implemenetation
of a function (and bad design if you have to), unless it is done in the
child's implementation of the same function:

#include<iostream>

struct foo1 {
virtual void foobar() {
std::cout << "foo1\n";
}
};

struct foo2 : public foo1 {
virtual void foobar() {
std::cout << "foo2\n";
foo1::foobar(); // Call foo1's foobar()
}
};

int main() {
foo1* f = new foo1();
f->foobar();
delete f;
f = new foo2();
f->foobar();
}

--
Erik Wikström
 
Reply With Quote
 
 
 
 
ArbolOne
Guest
Posts: n/a
 
      06-01-2008
On Jun 1, 3:57 am, Erik Wikström <Erik-wikst...@telia.com> wrote:
> On 2008-06-01 03:07, Victor Bazarov wrote:
>
>
>
> > ArbolOne wrote:
> >> In my program I am using two classes foo1, the parent class, and foo2
> >> the child class!
> >> foo2 has overloaded inserter and extractor operator, but so does foo1!
> >> How can I access foo1 inserter or extractor?

>
> > Post your code. It is easier when there is substance to the discussion.

>
> > Meanwhile, here is the example:

>
> > struct foo {
> > void foobar();
> > };

>
> > struct bar : foo {
> > void foobar();
> > };

>
> > int main() {
> > bar b;
> > b.foobar(); // calls bar::foobar
> > b.foo::foobar(); // call foo::foobar
> > }

>
> I would call it bad style to explicitly call a parent's implemenetation
> of a function (and bad design if you have to), unless it is done in the
> child's implementation of the same function:
>
> #include<iostream>
>
> struct foo1 {
> virtual void foobar() {
> std::cout << "foo1\n";
> }
>
> };
>
> struct foo2 : public foo1 {
> virtual void foobar() {
> std::cout << "foo2\n";
> foo1::foobar(); // Call foo1's foobar()
> }
>
> };
>
> int main() {
> foo1* f = new foo1();
> f->foobar();
> delete f;
> f = new foo2();
> f->foobar();
>
> }
>
> --
> Erik Wikström

I got it now!
Thanks.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
Query regarding calling parent's class method which is overridden in child class??? parkarumesh@gmail.com Java 2 04-26-2007 09:34 PM
Query regarding calling parent's class method which is overridden in child class??? parkarumesh@gmail.com Java 0 04-26-2007 09:24 AM
Tricky subclassing problem: Parent class method uses static value from child class don.hosek@gmail.com Perl Misc 0 09-26-2006 08:45 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57