I have a kind of confusion here. I have a bunch of classes under mainwindow. One is the base class: DocumentViewer. Under this we have multiple subclasses, like PDFViewer, RichTextViewer, DocumentViewer, ImageViewer etc.
The property Borders which is part of base class i.e. DocumentViewer. And ImageViewer has the property AspectRatio. But as I am inheriting the base class, can I access that Borders in derived class and use accordingly for my ImageViewer class?
Or I need to create same methods for the ImageViewer class too?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class DocumentViewer : public MainWindow
{
private: bool Borders;
public: bool GetBorders();
void PutBorders(...);
}
....
....
class ImageViewer : public DocumentViewer
{
private: bool AspectRatio;
public: bool GetAspectRatio();
void PutAspectRatio(...);
}
Do you want the object of a derived class to be able to call the void PutBorders(...) function, or do you want to access the function in your ImageViewer class definition?
The property Borders which is part of base class i.e. DocumentViewer. And ImageViewer has the property AspectRatio. But as I am inheriting the base class, can I access that Borders in derived class and use accordingly for my ImageViewer class?
ImageViewer is a DocumentViewer. A DocumentViewer has a property "Borders". So does an ImageViewer have that property? Well, an ImageViewer is a DocumentViewer. So ImageViewer has all the properties of DocumentViewer, because ImageViewer is a DocumentViewer.
Assuming that you want to call your void DocumentViewer::PutBorders() class in the function void ImageViewer::borderFunction() you simply need to call it using the class name DocumentViewer:: in front.
Assuming that you want to call your void DocumentViewer::PutBorders() class in the function void ImageViewer::borderFunction() you simply need to call it using the class name ImageViewer:: in front
Works fine without it. That's only needed when there are multiple implementations of the same function and you need to specify which one (which, as an aside, is generally the sign of a bad design).
ImageViewer has a function PutBorders. It is called like any other class function it has.
@hoogo, I tried it and getting errors, as @Repeater mentioned some simpler method is required to access the bool value in the DocumentViewer for the ImageViewer and make it applicable for the Image such that it uses AspectRatio and Borders properties while loading.
You must be getting errors from your main() function, I just recompiled the code
I sent you and I'm getting no errors. Would you mind posting your code as well as what errors you get?
Also watch out for the semi-colons at the end of your class definition, you forgot them in the first code you submitted :)