Inheritance Fun

This is a discussion on Inheritance Fun within the C++ Programming forums, part of the General Programming Boards category; Say I have class A and class B as follows: Code: class A { public: void dostuff(void) { cout<<"A is ...

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    Inheritance Fun

    Say I have class A and class B as follows:
    Code:
    class A {
      public:
        void dostuff(void) { cout<<"A is doing stuff"<<endl; }
    };
    
    class B : public A{
      public:
        void dostuff(void) { cout<<"B is doing stuff"<<endl; }
    };
    Then if I execute the following code:
    Code:
    vector<A*> alist;
    alist.push_back(new A());
    alist.push_back(new B());
    
    for(int i=0;i<2;++i) {
      (alist.at(i))->dostuff();
    }
    The output will be:
    Code:
    A is doing stuff
    A is doing stuff
    My problem is that I want B's dostuff() function to get called the second time instead of A's... any way to do that?


    Edit: On an unrelated note, how do some of you manage to get the syntax highlighting in your code tags?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    C has inheritance?
    Woop?

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Crap, wrong forum. Any mods mind moving this to the C++ board?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    As for the syntax highlighter I know most just use a program to do it for you. I used this http://www.codeguru.com/Cpp/misc/sam...icle.php/c4693 (Its for windows).
    Woop?

  5. #5
    Administrator webmaster's Avatar
    Join Date
    Aug 2001
    Posts
    977
    Too much Java? You forgot to declare the function virtual.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Thanks.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I use a syntax highliter like prog-bman said. I coded mine in Java, so if you need something multi-platform, I'll send it along to you... I'll warn you ahead of time that it's somewhat buggy though.

    the most common problems: too much highlighting (ifstream)
    escape characters in strings:
    Code:
    int main()
    {
    	std::cout<<"qoth the raven, \"nevermore\"";
    	return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Banned
    Join Date
    Jun 2005
    Posts
    594
    ah major looks like its time for you to rewrite it in c++ and
    compete in the new contest

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I thought about it... ATM I'm too busy, and if I had the time, I'd work on fixing that version up... and when I'm done with that, I have other plans for it...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 04:03 PM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 01:46 PM
  4. Back with some inheritance fun
    By hpy_gilmore8 in forum C++ Programming
    Replies: 11
    Last Post: 01-17-2004, 12:15 AM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21