pure virtual function

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
54
 #include <iostream>
  2 
  3 using namespace std;
  4 
  5 
  6 class base
  7 {
  8 protected:
  9         base(int i){val = i;cout<<"base"<<endl;}
 10         int val;
 11 public:
 12         virtual void show() = 0;
 13 
 14 };
 15 
 16 class derived1:virtual public base
 17 {
 18 public:
 19         derived1(int i):base(i){cout<<"derived1"<<endl;}
 20         void show(){cout<<hex<<val<<endl;}
 21 };
 22 
 23 class derived2:virtual public base
 24 {
 25 public:
 26         derived2(int i):base(i){cout<<"derived2"<<endl;}
 27         void show(){cout<<oct<<val<<endl;}
 28 
 29 };
 30 
 31 
 32 class derived3:public derived1,public derived2
 33 {
 34 public:
 35         derived3(int i):derived1(i),derived2(i),base(i){cout<<"derived3"<<endl;}
 36         void show(){cout<<val<<endl;}
 37 
 38 };
 39 
 40 
 41 int main()
 42 {
 43         derived1 d1(100);
 44         derived2 d2(120);
 45         cout<<"************************"<<endl;
 46         derived3 d3(12);
 47 
 48 
 49         d1.show();
 50         d2.show();
 51         d3.show();
 52 
 53 return 0;
 54 }



base
derived1
base
derived2
************************
base
derived1
derived2
derived3
64
170
14


why does d3.show() gives the octal result and not the decimal result?
closed account (D80DSL3A)
Because show() is not virtual in derived2.
IIRC, virtual is automatically there if the function is defined virtual in the base class...but maybe the virtual inheritance changes it.
The reason is that the effect from the oct manipulator is permanent. You have to use the dec manipulator in derived3.
Yes simeonz is correct, the stream manipulator in C++ is permanent , so we need to set it explicitly if we have to use any other.
my reply to fun2code: show() is virtual in all derived classes as in lieu with inheritance.
To the rest: simeonz is correct abt the effect of the oct manipulator. Thanks.
Topic archived. No new replies allowed.