Can a base class access variables in its derived class?

Hello guys, I'm fairly new to c++, and just started learning inheritance. I came up with a quick question here. In the code I created below, can the base class display the value of x in showBase() method? Or would it be impossible?
Thank you!

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
#include <iostream>
class Base{
	public:
		void showBase(){
			std::cout << "value of x: ";
		}
};

class Derived:public Base{
	public:
		int x;
	private:
		Dervied(int n){
			x = n;
		}
};


int main(){
	Derived d(3);
        Base b;
        b.showBase(); // or I could even do d.showBase() right?
	return 0;
}
1. typo on 13, Derived not Dervied
2. 12: ctor is private, so 20 will not work
3.
or I could even do d.showBase() right?
yes, provided you fix #2 above
4.
Can a base class access variables in its derived class?
https://stackoverflow.com/questions/2436125/c-access-derived-class-member-from-base-class-pointer
Oh I really appreciate it!
I didn't know I had that many typos/mistakes in my code haha
thank you. That helped me a lot hehe
Last edited on
... didn't know I had that many typos/mistakes in my code ...

good idea to compile (and run) program at least once before posting
can the base class display the value of x in showBase() method? Or would it be impossible?

No, a class can never assume that anything has been derived from it. A class knows only itself and its bases.

No, it is not impossible. That is what virtual functions are for:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
class Base{
	public:
		virtual void show() {}
};

class Derived : public Base {
		int x;
	public:
		Derived(int n) : x(n) {}
		virtual void show() { std::cout << x; }
};

int main() {
	Derived d( 3 );
        Base* pb = d;
        pb->show();
	return 0;
}



Serious logical issue here:
1
2
3
4
5
6
int main(){
	Derived d(3);
        Base b;
        b.showBase();
	return 0;
}

The d IS-A Derived object.
The d IS-A Base object, because Derived inherits Base.

The b and d are two entirely separate objects.

The b IS-A Base object and nothing more. It cannot know anything about any Derived objects.


You have a wallet. You have a neighbors with wallets. Should your wallet know how much money a neighbor wallet has? No.
No, b.showBase(); is not there to tell about the d.

Lets do it again:
1
2
3
4
5
6
7
8
9
int main(){
	Derived d(3);
	Derived fubar(42);
	Derived snafu(7);
        Base xyz;
        Base b;
        b.showBase();
	return 0;
}

What does the b know?
Last edited on
@gunnerfunner
Thanks for the advice :)) I did compile my program before posting but when I copied the code from my compiler and pasted in here it all appeared in one line for some reason so I just decided to copy it word by word and maybe I made some mistakes as I was doing that hehe ;)

@keskiverto
I really really appreciate it!
Now I realized I was misunderstanding the relationship between the two...
Thank you so much for all the information and help! :DD
Last edited on
Topic archived. No new replies allowed.