Should this work??

Feb 18, 2011 at 12:53am
I really don't know, but it works on my compiler and gives me this output:

012

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;

class baseClass
{
public:
	baseClass()
	{
	}
	int GiveMe()
	{
		return 0;
	}
};

class derivedClass : public baseClass
{
public:
	derivedClass()
	{
	}
	virtual int GiveMe()
	{
		return 1;
	}
};

class bob : public derivedClass
{
public:
	bob()
	{
	}
	int GiveMe()
	{
		return 2;
	}
};

int main()
{
	baseClass base;
	derivedClass derived;

	bob Mar;
	

	cout << base.GiveMe();
	cout << derived.GiveMe();
	cout << Mar.GiveMe();
	cin.get();

}
Feb 18, 2011 at 2:29am
Yes, it should, but the virtual keyword is not necessary because you're not using pointers to the base class to enable polymorphism. If those three objects were being accessed through pointers to baseClass, then baseClass' GiveMe() would have to be virtual for that to work. Once a method is declared virtual, it will be virtual for every class that inherits from its own class.
Topic archived. No new replies allowed.