Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. base class calling derived class' functions

base class calling derived class' functions

Scheduled Pinned Locked Moved C / C++ / MFC
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bernhard
    wrote on last edited by
    #1

    okay.. in my program i would like to have a structure like that class base { public: void function1() { .... function2(); } virtual void function2 = 0; }; class derived1 : public base { public: void function2 { cout << "Function2 from derived1"; } }; class derived2 : public base { public: void function2 { cout << "Function2 from derived2"; } }; int main() { base* pBase = new derived1; base* pBase2 = new derived2; pBase->function1(); pBase->function2(); } Any Ideas, thanks in advance, bernhard


    "I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
    U.S. Secretary of State Colin Powell at George Bush's ranch in Texas

    T 1 Reply Last reply
    0
    • B Bernhard

      okay.. in my program i would like to have a structure like that class base { public: void function1() { .... function2(); } virtual void function2 = 0; }; class derived1 : public base { public: void function2 { cout << "Function2 from derived1"; } }; class derived2 : public base { public: void function2 { cout << "Function2 from derived2"; } }; int main() { base* pBase = new derived1; base* pBase2 = new derived2; pBase->function1(); pBase->function2(); } Any Ideas, thanks in advance, bernhard


      "I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
      U.S. Secretary of State Colin Powell at George Bush's ranch in Texas

      T Offline
      T Offline
      The_Server
      wrote on last edited by
      #2

      I cant see anyt problem in your code It's ok and should work... output: Function2 from derived1 Function2 from derived1 what is your problem ???? any ideas for what ???? ------ The Server

      B 1 Reply Last reply
      0
      • T The_Server

        I cant see anyt problem in your code It's ok and should work... output: Function2 from derived1 Function2 from derived1 what is your problem ???? any ideas for what ???? ------ The Server

        B Offline
        B Offline
        Bernhard
        wrote on last edited by
        #3

        it doesn't compile (in vc++6) because it tries to call the pure virtual function function2 from the base class (and not the derived ones).. it only works if i call the function2 from the derived classes..


        "I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
        U.S. Secretary of State Colin Powell at George Bush's ranch in Texas

        J A T 3 Replies Last reply
        0
        • B Bernhard

          it doesn't compile (in vc++6) because it tries to call the pure virtual function function2 from the base class (and not the derived ones).. it only works if i call the function2 from the derived classes..


          "I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
          U.S. Secretary of State Colin Powell at George Bush's ranch in Texas

          J Offline
          J Offline
          Jawache
          wrote on last edited by
          #4

          You need to declare thye base class functions as virtual aswell as the derived class Asim Hussain e: asim@jawache.net w: www.jawache.net

          1 Reply Last reply
          0
          • B Bernhard

            it doesn't compile (in vc++6) because it tries to call the pure virtual function function2 from the base class (and not the derived ones).. it only works if i call the function2 from the derived classes..


            "I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
            U.S. Secretary of State Colin Powell at George Bush's ranch in Texas

            A Offline
            A Offline
            Andrew Walker
            wrote on last edited by
            #5

            Bernhard wrote: it doesn't compile (in vc++6) because it tries to call the pure virtual function function2 from the base class (and not the derived ones).. it only works if i call the function2 from the derived classes.. You just said it youself :-), this class is abstract, it has a pure virtual method, it can't be instantiated.

            B 1 Reply Last reply
            0
            • A Andrew Walker

              Bernhard wrote: it doesn't compile (in vc++6) because it tries to call the pure virtual function function2 from the base class (and not the derived ones).. it only works if i call the function2 from the derived classes.. You just said it youself :-), this class is abstract, it has a pure virtual method, it can't be instantiated.

              B Offline
              B Offline
              Bernhard
              wrote on last edited by
              #6

              but is there something, which will do the job.. i want a function in the base class which calls a overloaded function from derived classes?


              "I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
              U.S. Secretary of State Colin Powell at George Bush's ranch in Texas

              1 Reply Last reply
              0
              • B Bernhard

                it doesn't compile (in vc++6) because it tries to call the pure virtual function function2 from the base class (and not the derived ones).. it only works if i call the function2 from the derived classes..


                "I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
                U.S. Secretary of State Colin Powell at George Bush's ranch in Texas

                T Offline
                T Offline
                The_Server
                wrote on last edited by
                #7

                FOR CRYING OUT LOUD:mad: IT F#$%ING WORKS !!!!:mad: file.h:

                class cBase
                {
                public:
                virtual void func1(){ func2(); }
                virtual void func2()==0;
                };

                class cDer1
                {
                public:
                void func2() { printf("(2)It works!!!\n");}
                };

                class cDer2
                {
                public:
                void func2() { printf("(2)It works!!!\n");}
                };

                file.cpp

                #include "file.h"
                #include
                #include
                void main()
                {

                cBase * a= new cDer1();
                cBase * b= new cDer2();

                a->func1();
                b->func1();
                a->func2();
                b->func2();

                printf("IT F@#!@ WORKS");
                getch();
                }

                ==================== === THE SERVER === ====================

                B 1 Reply Last reply
                0
                • T The_Server

                  FOR CRYING OUT LOUD:mad: IT F#$%ING WORKS !!!!:mad: file.h:

                  class cBase
                  {
                  public:
                  virtual void func1(){ func2(); }
                  virtual void func2()==0;
                  };

                  class cDer1
                  {
                  public:
                  void func2() { printf("(2)It works!!!\n");}
                  };

                  class cDer2
                  {
                  public:
                  void func2() { printf("(2)It works!!!\n");}
                  };

                  file.cpp

                  #include "file.h"
                  #include
                  #include
                  void main()
                  {

                  cBase * a= new cDer1();
                  cBase * b= new cDer2();

                  a->func1();
                  b->func1();
                  a->func2();
                  b->func2();

                  printf("IT F@#!@ WORKS");
                  getch();
                  }

                  ==================== === THE SERVER === ====================

                  B Offline
                  B Offline
                  Bernhard
                  wrote on last edited by
                  #8

                  it really works.. my problem was that i didn't declare both base class members as virtual, because in my real project the calling function was the constructor.. and the constructor can't be virtual (at least i think the constructor can't be virtual) thanks for your help (i found another workaround) bernhard (hope you didn't get too angry)


                  "I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
                  U.S. Secretary of State Colin Powell at George Bush's ranch in Texas

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups