-1

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?

    class DocumentViewer : public MainWindow
        {
        private: bool Borders;
        public: bool GetBorders();
                void PutBorders(...);
        }
....
....
        class ImageViewer : public DocumentViewer
        {
        private: bool AspectRatio;
        public: bool GetAspectRatio();
                void PutAspectRatio(...);
        }
highlander141
  • 1,683
  • 3
  • 23
  • 48
  • 1
    If the member variable `Borders` is in the `protected` section of the base class, then yes it can be accessed from child classes. That's one of the main points of inheritance, that member variables also gets inherited. – Some programmer dude Dec 20 '17 at 09:22
  • 1
    Please provide a [mcve]. The title asks for the opposite: accesing derived class member from base, while in the question you want to access a base class member in derived – 463035818_is_not_an_ai Dec 20 '17 at 09:24
  • @tobi303 Yes I corrected the heading. Will try to post sample code.. – highlander141 Dec 20 '17 at 09:26
  • @HansPassant Well it was defined like that, now the property is not accessible for images which should have done like you said! Now is there any way or I need to duplicate the properties? – highlander141 Dec 20 '17 at 09:37
  • @Someprogrammerdude Actually its in private section, and I have posted the sample code, pls have a look – highlander141 Dec 21 '17 at 06:56
  • Then the member variable itself is still inherited, but it can't be accessed directly from the child classes. Also, having a private member variable and public getter/setter functions means that in all practicality the variable is still "public". Why hide the variable if anyone can get or set its value anyway? – Some programmer dude Dec 21 '17 at 07:01
  • @Someprogrammerdude So how can I have the access in Image class, pls provide a sample code to access the Borders variable and set it accordingly for Image class? – highlander141 Dec 21 '17 at 08:02
  • Make the variable `protected` or `public`, or use the getter and setter function you already have. – Some programmer dude Dec 21 '17 at 08:04
  • @Someprogrammerdude I cannot change the variable access specifiers. How to use getters and setters, some sample code will be appreciated.. – highlander141 Dec 21 '17 at 08:06
  • You have the functions `GetBorders` and `PutBorders`. How do you otherwise use them? Perhaps you should take a few steps back, [get a couple of good beginners books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and more or less start over? – Some programmer dude Dec 21 '17 at 08:13
  • @Someprogrammerdude Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/161686/discussion-between-highlander141-and-some-programmer-dude). – highlander141 Dec 21 '17 at 08:18
  • @Someprogrammerdude Can you join me for few minutes in the chat room? – highlander141 Dec 21 '17 at 08:47

2 Answers2

0

Although Borders are no longer directly accessible by the class inheriting DocumentViewer, you are still able to modify it using getter and setter that you have declared as in getBorders and PutBorders. If ImageViewer need Borders, just access it using getter and setter.

0

In addition to Some programmer dude's comment, this post provides a helpful answer to the question:

Inheriting private members in C++

Autex
  • 307
  • 2
  • 12