-
October 2nd, 2013, 08:27 PM
#1
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.
-
October 2nd, 2013, 10:03 PM
#2
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.
-
October 3rd, 2013, 05:58 PM
#3
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.
-
October 3rd, 2013, 07:38 PM
#4
Re: Polymorphic Methods in Abstract class
Originally Posted by rockx
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
-
October 5th, 2013, 09:05 PM
#5
Re: Polymorphic Methods in Abstract class
so where do i implement this, in class Furniture or class Chair or the main?
-
October 6th, 2013, 04:16 AM
#6
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".
-
October 7th, 2013, 06:21 AM
#7
Re: Polymorphic Methods in Abstract class
Originally Posted by rockx
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|