CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2004
    Posts
    249

    Polymorphic Methods in Abstract class

    Imagine if there is an abstract class with a method (say output or print) which would be inherited by a few other classes. Later objects are created using the inherited classes, and the user wishes to call the above method twice, for eg (i) output/print to screen and (ii) output/print to a file.

    what is the best way to achieve that.

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

    Re: Polymorphic Methods in Abstract class

    If by "print to screen" you mean "print to standard output", then one approach is:
    Code:
    class A
    {
    public:
        friend std::ostream& operator<<(std::ostream& out, const A& object);
        // ...
    private:
        virtual void print(std::ostream& out) const = 0;
        // ...
    };
    
    // Presumably in a source file or declared inline in the header:
    std::ostream& operator<<(std::ostream& out, const A& object)
    {
        object.print(out);
        return out;
    }
    This way, subclasses of A can override the print method to do the printing they require. Users of the class and its subclasses then use the overloaded operator<< whether with std::cout or some std::ofstream as the case may be.
    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
    May 2004
    Posts
    249

    Question Re: Polymorphic Methods in Abstract class

    what i have done was something similar to the below:

    Code:
    class Furniture {
    
    public:
        
       // pure virtual functions; overridden in derived classes
       //   virtual string getName() const = 0; // return furniture name
       virtual void print() const = 0;     // output furniture 
    
    }; 
    
    class Chair : public Furniture {
    public:
    
    
        virtual void print() const;  // output Chair object
    
    };
    
      //method definition
    
    #include <iostream>
    #include "chair.h"   // Chair class definition
    using namespace std;
    
    void Chair::print() const
    {
       cout << "This is a chair/n/n" ;
    
    }
    After creating objects of chair and *other furniture classes, I would like to call the print method twice, one to print the object in a standard stream, and one in a file stream, I really don;t know what to do. I mean how to best achieve that.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Polymorphic Methods in Abstract class

    Quote Originally Posted by rockx View Post
    what i have done was something similar to the below:

    Code:
    class Furniture {
    
    public:
        
       // pure virtual functions; overridden in derived classes
       //   virtual string getName() const = 0; // return furniture name
       virtual void print() const = 0;     // output furniture 
    
    }; 
    
    class Chair : public Furniture {
    public:
    
    
        virtual void print() const;  // output Chair object
    
    };
    
      //method definition
    
    #include <iostream>
    #include "chair.h"   // Chair class definition
    using namespace std;
    
    void Chair::print() const
    {
       cout << "This is a chair/n/n" ;
    
    }
    After creating objects of chair and *other furniture classes, I would like to call the print method twice, one to print the object in a standard stream, and one in a file stream, I really don;t know what to do. I mean how to best achieve that.
    Have your print() function accept a pointer or reference to a std::ostream.
    Code:
    void print(std::ostream& strm)
    {
        strm << "This is a chair";
    }
    
    //...
    print(cout);
    std::ofstream ofs("Test.txt");
    print(ofs);
    Regards,

    Paul McKenzie

  5. #5
    Join Date
    May 2004
    Posts
    249

    Re: Polymorphic Methods in Abstract class

    so where do i implement this, in class Furniture or class Chair or the main?

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

    Re: Polymorphic Methods in Abstract class

    Look at my example: I declared the function as pure virtual in the base class. What I did not show was that the virtual function was then defined in the derived classes, but that should be obvious since I wrote that "subclasses of A can override the print method to do the printing they require".
    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
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Polymorphic Methods in Abstract class

    Quote Originally Posted by rockx View Post
    Imagine if there is an abstract class with a method (say output or print) which would be inherited by a few other classes. Later objects are created using the inherited classes, and the user wishes to call the above method twice, for eg (i) output/print to screen and (ii) output/print to a file.
    Don't you see that polymorphism of outputting mechanism generally has nothing to do with the polymorphism/abstractness of your class hierarchy? Those two aspects are pretty independent of each other. Your hierarchy polymorphism basically controls what to output ('Furniture' vs 'Chair'), while outputting polymorphism controls how/where to output (screen vs file).
    Best regards,
    Igor

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