Inheritance: Base class property accessibility from Derived class

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?
I want to access the function, and at the same time set the properties in derived class. What is the difference in both cases?
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.
Thanks Repeater, a sample code in accessing the property, getting and setting it would be highly appreciated, as I started learning c++ newly..
1
2
ImageViewer someObject;
bool bordersState = someObject.getBorders();
your header should look something like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef HEADER_H
#define HEADER_H

class MainWindow
{
	
};

class DocumentViewer : public MainWindow
{
    private:
	bool Borders;
    
     public:
	DocumentViewer();
	bool GetBorders();
        void PutBorders() {}
};

class ImageViewer : public DocumentViewer
{
    private:
	bool AspectRatio;
      
    public:
	ImageViewer() : DocumentViewer() {}
	bool GetAspectRatio();
        void PutAspectRatio();
        
        void borderFunction()
            {
        	//statements
        	DocumentViewer::PutBorders(/*arguments*/);
            }
};

#endif 


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.

I hope that answered your question?


Edit: typos
Last edited on
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.
Last edited on
@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.
@hoogo, I tried it and getting errors


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 :)
Alright, can you post your complete code ?, I will try the same here. I am not sure if I can post the code :-/

Anyway I donot want to modify the Borders value, just want to access it and use it in my whenever its an Image.
Last edited on
Sure,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//Header.h
#ifndef HEADER_H
#define HEADER_H

class MainWindow
{
	
};

class DocumentViewer : public MainWindow
{
    private:
	bool Borders;
    
    public:
	DocumentViewer();
	bool GetBorders();
        void PutBorders() {}
};

class ImageViewer : public DocumentViewer
{
    private:
	bool AspectRatio;
      
    public:
	ImageViewer() : DocumentViewer() {}
	bool GetAspectRatio();
        void PutAspectRatio();
        
        void borderFunction()
        	{
        	    //statements
        	    PutBorders(/*arguments*/);
        	}
};

#endif 


Note that I haven't defined the MainWindow class since you didn't provide it in your first post.

1
2
3
4
5
6
7
8
9
10
11
//Main.cpp
#include <iostream>
#include "header.h"

using namespace std;

int main()
{
	
	return 0;
}


The main() function was intentionally left blank for the purpose of compiling the header file to see if there were any errors.

Edit: typos
Last edited on
Topic archived. No new replies allowed.