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. How to postpone implementation of abstract class in c++

How to postpone implementation of abstract class in c++

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorial
7 Posts 4 Posters 2 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.
  • T Offline
    T Offline
    tasumisra
    wrote on last edited by
    #1

    can anybody let me how to postpone implementation of abstract class methods

    #include<iostream>
    using namespace std;
    class abstrs
    {
    public:
    virtual int add(int i,int j)=0;
    virtual int Substract(int i,int j)=0;

    };
    class derieved : public abstrs
    {
    public :
    virtual int add(int i,int c)
    {
    return 100;

    }//i dont want to implement this piece of code as of now.
    /\*virtual	int Substract(int i,int c)
    {
    return 100;
    }\*/
    

    };
    int main()
    {
    derieved dr;
    int count=dr.add(12,34);
    cout<<g;

    }

    vikas da

    T A N 3 Replies Last reply
    0
    • T tasumisra

      can anybody let me how to postpone implementation of abstract class methods

      #include<iostream>
      using namespace std;
      class abstrs
      {
      public:
      virtual int add(int i,int j)=0;
      virtual int Substract(int i,int j)=0;

      };
      class derieved : public abstrs
      {
      public :
      virtual int add(int i,int c)
      {
      return 100;

      }//i dont want to implement this piece of code as of now.
      /\*virtual	int Substract(int i,int c)
      {
      return 100;
      }\*/
      

      };
      int main()
      {
      derieved dr;
      int count=dr.add(12,34);
      cout<<g;

      }

      vikas da

      T Offline
      T Offline
      Tim Craig
      wrote on last edited by
      #2

      If you don't, you won't be able to create an instance of derived. Derived will still be abstract. You'll have to derive another layer and define Subtract there.

      Once you agree to clans, tribes, governments...you've opted for socialism. The rest is just details.

      T 1 Reply Last reply
      0
      • T Tim Craig

        If you don't, you won't be able to create an instance of derived. Derived will still be abstract. You'll have to derive another layer and define Subtract there.

        Once you agree to clans, tribes, governments...you've opted for socialism. The rest is just details.

        T Offline
        T Offline
        tasumisra
        wrote on last edited by
        #3

        Thanks Tim, so we need to have implementation of all methods defined in abstract class to the single derived class to instantiate the class. am i right..?

        vikas da

        A 1 Reply Last reply
        0
        • T tasumisra

          can anybody let me how to postpone implementation of abstract class methods

          #include<iostream>
          using namespace std;
          class abstrs
          {
          public:
          virtual int add(int i,int j)=0;
          virtual int Substract(int i,int j)=0;

          };
          class derieved : public abstrs
          {
          public :
          virtual int add(int i,int c)
          {
          return 100;

          }//i dont want to implement this piece of code as of now.
          /\*virtual	int Substract(int i,int c)
          {
          return 100;
          }\*/
          

          };
          int main()
          {
          derieved dr;
          int count=dr.add(12,34);
          cout<<g;

          }

          vikas da

          A Offline
          A Offline
          Aescleal
          wrote on last edited by
          #4

          The best way of avoiding the need to implement member functions (C++ doesn't have methods) is to avoid deriving a class from an abstract class. So the first thing to ask is... why are you writing code like you have in your main? Derived doesn't actually need to implement any abstract classes so just implement what you need to get your code compiling:

          class adder
          {
          public:
          int add( int i, int j ) const { return i + j; }
          };

          int main()
          {
          std::cout << adder().add( 12, 34 ) << std::endl;
          }

          You only need abstract classes as your code gets bigger and you want to start cutting down on the dependencies between lumps of code. You use abstract classes as a design tool to avoid most of your code needing to know what concrete types they're dealing with. If on the other hand you've got an interface you need to implement to use a particular lump of code then you haven't got a lot of choice but to implement the member functions. You could take the Java route and implement a minimal class and then derive from that:

          class adder
          {
          public:
          virtual int add( int i, int j ) = 0;
          };

          class minimal_adder
          {
          public:
          virtual int add( int, int ) { return 0; }
          };

          But that can turn into a maintenance nightmare (you have to look at two implementations to find out where a member function is implemented, not just one. It gets worse when some wit adds another level, then another...). So the points here are: - Don't use an abstract class until you need to, never do it "just in case" [1] - When you create abstract classes try and create them from the existing interface of a concrete class. You'll at least know there's client code (and unit tests) ready to use your new abstract class against - Only use Java style stub implementations as a last resort (some chunk of client code is expecting an interface with 30 member functions, half of which you have no idea what the contracts are) Cheers, Ash [1] Needing to includes using one as a firewall between code you're writing and code your colleagues are writing. If you sit down with a co-worker that needs a service from code you're writing hack out a quick interface together, stub implement it for them and then take your time implementing it properly.

          1 Reply Last reply
          0
          • T tasumisra

            Thanks Tim, so we need to have implementation of all methods defined in abstract class to the single derived class to instantiate the class. am i right..?

            vikas da

            A Offline
            A Offline
            Aescleal
            wrote on last edited by
            #5

            Yep - every pure virtual function in an abstract class needs to be implemented in a derived class or you won't be able to instantiate the derived class. Cheers, Ash

            1 Reply Last reply
            0
            • T tasumisra

              can anybody let me how to postpone implementation of abstract class methods

              #include<iostream>
              using namespace std;
              class abstrs
              {
              public:
              virtual int add(int i,int j)=0;
              virtual int Substract(int i,int j)=0;

              };
              class derieved : public abstrs
              {
              public :
              virtual int add(int i,int c)
              {
              return 100;

              }//i dont want to implement this piece of code as of now.
              /\*virtual	int Substract(int i,int c)
              {
              return 100;
              }\*/
              

              };
              int main()
              {
              derieved dr;
              int count=dr.add(12,34);
              cout<<g;

              }

              vikas da

              N Offline
              N Offline
              Niklas L
              wrote on last edited by
              #6

              In addition to the replies here, even though you have to implement all abstract functions, there is still a chance you can get a 'Pure virtual function call' runtime error. You can find an example here[^] and some additional info.

              home

              T 1 Reply Last reply
              0
              • N Niklas L

                In addition to the replies here, even though you have to implement all abstract functions, there is still a chance you can get a 'Pure virtual function call' runtime error. You can find an example here[^] and some additional info.

                home

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

                Thanks for all your replies i got my answer...:) have a nice time ...

                vikas da

                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