CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2010
    Posts
    3

    what is the advantage of using base class pointer to point to derived class's object

    what is the advantage of using base class pointer to point to derived class's object


    like

    class base
    {

    }

    class derived : public base
    {

    }

    void main()
    {
    base* b=new derived();
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: what is the advantage of using base class pointer to point to derived class's ob

    Quote Originally Posted by lokesh_kumar_s
    what is the advantage of using base class pointer to point to derived class's object
    The answer to this is related to the answer to the question of What is "OOP" and what's so great about it?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: what is the advantage of using base class pointer to point to derived class's ob

    Quote Originally Posted by lokesh_kumar_s View Post
    what is the advantage of using base class pointer to point to derived class's object


    like

    class base
    {

    }

    class derived : public base
    {

    }

    void main()
    {
    base* b=new derived();
    }
    You can assign instances of any derived class to the base.

    For example, if you have class derived1 and derived2, it is very common to have a vector<base*> that can hold pointers of objects derived1 or derived2.

    In your example, maybe the code cotinues like this?

    Quote Originally Posted by lokesh_kumar_s View Post
    void main()
    {
    base* a=new derived1();
    base* b=new derived2();

    ...
    if(...)
    {
    std::swap(a, b);
    }
    }
    This would not have been possible if a and b were not defined as b*.

  4. #4
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: what is the advantage of using base class pointer to point to derived class's ob

    Quite a while ago, before I even knew anything about objects, polomorphism and inheritance I was struggling with the problem that it was not possible to put objects or structures of different types into containers.
    I soon found out that the only way ( at that time, with the tools I knew ) was to store anonymous pointers to that objects. I just had to make shure that I had a way to identify the type when I wanted to use that stored objects.
    I solved that problem by storing an Id field at the start of that structures.
    Using all that different types was as little difficult. The code contained lots of casts and if's and else if's. But it worked fine.
    Object oriented languages like C++ helped a lot. No more branching because I could use virtual member functions. The compiler does all that stuff for me.
    Since C++ supports polimorphism only for pointers and references, but it's not possible to store references in containers, the main reason for me to use pointers to base is to store them in containers.
    Kurt

  5. #5
    Join Date
    Apr 2010
    Posts
    3

    Re: what is the advantage of using base class pointer to point to derived class's ob

    Quote Originally Posted by monarch_dodra View Post
    You can assign instances of any derived class to the base.

    For example, if you have class derived1 and derived2, it is very common to have a vector<base*> that can hold pointers of objects derived1 or derived2.

    In your example, maybe the code cotinues like this?



    This would not have been possible if a and b were not defined as b*.
    thanks monarch_dodra,
    for your answer. but see
    Code:
    #include <iostream>
    
    struct Shape
    {
      virtual void print()
      {
        std::cout << "SHAPE" << std::endl;
      }
    
      virtual ~Shape() {}
    };
    
    struct Box : public Shape
    {
      virtual void print(int i)
      {
        std::cout << "BOX" << std::endl;
      }
    };
    
    int main(int argc, char** argv) 
    { 
      Shape* s = new Box;
    
      s->print();
    
      delete s;
    
      return 0; 
    }
    but tell me in the above code they have used shape pointer to hold Box' object.
    for only above purpose holding box's object in base class pointer is useless right. in other words is there any advantage is there we can also use like Box s = new Box; right. it is simple and more easily understandable for above kind of situation.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: what is the advantage of using base class pointer to point to derived class's ob

    Quote Originally Posted by lokesh_kumar_s
    but tell me in the above code they have used shape pointer to hold Box' object.
    for only above purpose holding box's object in base class pointer is useless right. in other words is there any advantage is there we can also use like Box s = new Box; right. it is simple and more easily understandable for above kind of situation.
    Yes, but if you want to criticise such a trivial example and simplify it without seeing its wider application, then clearly in the above code, even the use of the Box class is useless. You might as well write:
    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << "BOX" << std::endl;
    }
    So, the point is not that the use of a Shape pointer to point to a Box object is useless; the point is to illustrate with a trivial example the use of polymorphism, so one can observe that "BOX" instead of "SHAPE" is printed, because print is a virtual member function.

    In a less trivial situation, it may be the case that the choice between Box and some other Shape derived class is made at run time, hence the use of this run time polymorphism actually becomes useful: one can program to the Shape interface instead of having to consider the different cases at every step.

    You might want to read these articles (in PDF format):
    The Open-Closed Principle
    The Liskov Substitution Principle
    The Dependency Inversion Principle
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Oct 2007
    Posts
    132

    Re: what is the advantage of using base class pointer to point to derived class's ob

    I think one simple answer to your question (of which there are many) might be to add about 100 more derivations of Shape. (Most of the time you can't get the real value from C++ until you get into real-world situations where programs aren't trivial and require a lot of design consideration). So how do I call print on every single instance of every single derivied "Shape" type without cluttering my code with box->print everywhere 100 times over? The answer as was already alluded to is to make print() a pure virtual function of Shape() and have a container of Shapes i.e. vector<Shape*> and just loop through the vector calling shape->print() 100 times...it doesn't matter any more what the derived type is...

    I think in your sample code this is not quite clear because the print() function is not pure virtual -- so it is confusing to see how this method is used in that case. It is more often the case that the base class has pure virtual functions in order to be useful. I believe this is related to the Template Method pattern, however, the Shape code does not follow that pattern since print is not pure virtual, so that is where the confusion is I think. The Shape example you gave is only there to demonstrate the rules of virtual functions (i.e. which print gets called depending on the type of s), not really provide an example of real-world OOP or the Template Method pattern.

  8. #8
    Join Date
    Aug 2009
    Posts
    81

    Re: what is the advantage of using base class pointer to point to derived class's ob

    Quote Originally Posted by andersod2 View Post
    I think one simple answer to your question (of which there are many) might be to add about 100 more derivations of Shape. (Most of the time you can't get the real value from C++ until you get into real-world situations where programs aren't trivial and require a lot of design consideration). So how do I call print on every single instance of every single derivied "Shape" type without cluttering my code with box->print everywhere 100 times over? The answer as was already alluded to is to make print() a pure virtual function of Shape() and have a container of Shapes i.e. vector<Shape*> and just loop through the vector calling shape->print() 100 times...it doesn't matter any more what the derived type is...
    Yes, that's one of the reasons Bjarne Stroustrup first resisted to put RTTI into the language, because of the ways people can take it into extreme and ask a lot of capability queries about the object. that's the basic rule of thumb of every good design no matter what language it is written - "Tell, don't ask", when you have an object you simply tell it to do something without asking any question about the state of itself.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured