-
Notifications
You must be signed in to change notification settings - Fork 1
/
example6-inheritance.cpp
66 lines (54 loc) · 1.08 KB
/
example6-inheritance.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
struct Base
{
virtual int foo(int n)
[[ expects: n < 10 ]]
[[ ensures r: r > 100 ]]
{
return n*n;
}
virtual ~Base() = default;
};
struct Derived1 : Base
{
virtual int foo(int n) //override
[[ expects: n < 10 ]]
[[ ensures r: r > 100 ]]
override
{
return n*n*2;
}
virtual ~Derived1() = default;
};
struct Derived2 : Base
{
// Inherits contracts from base
virtual int foo(int n) override
{
return n*3;
}
virtual ~Derived2() = default;
};
int main()
{
Derived1 obj1;
Derived2 obj2;
obj1.foo(100);
obj2.foo(100);
{
Derived1* pobj1 = new Derived1();
Derived2* pobj2 = new Derived2();
pobj1->foo(100);
pobj2->foo(100);
delete pobj1;
delete pobj2;
}
{
Base* pobj1 = new Derived1();
Base* pobj2 = new Derived2();
pobj1->foo(100);
pobj2->foo(100);
delete pobj1;
delete pobj2;
}
return 0;
}