What happens in this program ?

Please refer the following code :

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
#include<iostream>
using namespace std;
class abc
{
	private:int a;
	public: abc()
	{
		a=7;
	}
	public:void display()
	{
		cout<<a;
	}
};

class bcd: public abc
{

};


int main()
{
	
	bcd b1;
	b1.display();
	
	
}


It gives the output 7 .

looks like "b1.display()" is actually executing "abc::display()"
???

@Question:
Yes, it does. Why wouldn't it? bcd inherits abc and doesn't overload display().

Lines 5, 6, and 10:
Naw, you don't need to have the access specifiers in the same line as each declaration, like in Java. This is perfectly valid:

1
2
3
4
5
6
7
8
9
10
11
12
class abc
{
private:
	int a;
public:
	abc() {
		a=7;
	}
	void display() {
		cout<<a;
	}
};


EDIT: -Albatross
Last edited on
You mean, if in case a derived function is not overridden, base class function will be called always?
Yes. That's what inheritance is!
thnx !
Topic archived. No new replies allowed.