new classname() showing error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • diwakar09
    New Member
    • Sep 2007
    • 39

    new classname() showing error

    Code:
      
        class derived;   //forward declaration
    
         class base {
    
              public :
                    base *getobject()
                      {
                              if(/*condition*/)
                                  return new derived();
                           else
                              /* do nothing */
                        }
                   virtual void paint() {} =0;
                };
    
        class derived  :  public base
            {
                 public:
                     derived(){  }
                       void paint()
                        {
                           cout<<"in derived";
                         }
                  };
    
       int main()
        {
                base  *b,j,*k;
               b=&j;
               k = b->getobject();
               k->paint();
    }

    please tell me whats the cause of error
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by diwakar09
    Code:
      
     virtual void paint() {} =0;

    the cause of error
    Here virtual function declared is a pure virtual function. So it cannot be defined in derived class.

    Comment

    • pntkiran
      New Member
      • Jun 2007
      • 16

      #3
      Hi Diwakar,

      you are using forward declaration, So can't access derived class function through base class object(remove getobject function), modify virtual void paint() function ..(remove =0) and assign k = new derived ();.

      You will get the result.
      Cheers.

      Originally posted by diwakar09
      Code:
        
          class derived;   //forward declaration
      
           class base {
      
                public :
                      base *getobject()
                        {
                                if(/*condition*/)
                                    return new derived();
                             else
                                /* do nothing */
                          }
                     virtual void paint() {} =0;
                  };
      
          class derived  :  public base
              {
                   public:
                       derived(){  }
                         void paint()
                          {
                             cout<<"in derived";
                           }
                    };
      
         int main()
          {
                  base  *b,j,*k;
                 b=&j;
                 k = b->getobject();
                 k->paint();
      }

      please tell me whats the cause of error

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        The actual problem is failing to substitute a derived object for a base object.

        Your main shoud be:
        [code=cpp]
        base* ptr = new derived;
        ptr->paint(); //calls derived::paint( )
        [/code]

        That is, when you have virtual functions you are designing the interface to be inthe base class but you will actually create derived objects and use them as base objects. This is fundamental polymorphism.

        The pure virtual function only means that you can't call the method using an object of the class that declares the pure virtual function.

        Since the base class defines the interface, the methods the user is call should be public and none of these should eb private. Instead, the base public methods call base private virtual methods and it is these that are overriden in the derived class.

        Most likely, the overiding method can be private in the derived class.

        Your base::getobject () that returns a base* is bad design. Methods like this belong in a Factory class and not your base class. It is the Factory that creates the derived object and returns its address as a base*.

        Comment

        Working...