-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9abf23c
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Base.cpp | ||
Investigation of virtual methods and overriding | ||
*/ | ||
|
||
#include <string> | ||
#include <cassert> | ||
|
||
class Base { | ||
public: | ||
std::string m1() const { return "Base::m1()"; } | ||
virtual std::string m2() const { return "Base::m2()"; } | ||
}; | ||
|
||
class Derived : public Base { | ||
public: | ||
std::string m1() const { return "Derived::m1()"; } | ||
std::string m2() const { return "Derived::m2()"; } | ||
}; | ||
|
||
std::string m1BValue(Base b) { | ||
return b.m1(); | ||
} | ||
|
||
std::string m2BValue(Base b) { | ||
return b.m2(); | ||
} | ||
|
||
std::string m1BPtr(Base* pb) { | ||
return pb->m1(); | ||
} | ||
|
||
std::string m2BPtr(Base* pb) { | ||
return pb->m2(); | ||
} | ||
|
||
std::string m1BRef(Base& b) { | ||
return b.m1(); | ||
} | ||
|
||
std::string m2BRef(Base& b) { | ||
return b.m2(); | ||
} | ||
|
||
int main() { | ||
|
||
{ | ||
Base b; | ||
|
||
Base* pb = &b; | ||
|
||
Base& rb = b; | ||
|
||
assert(m1BValue(b) == "Base::m1()"); | ||
assert(m2BValue(b) == "Base::m2()"); | ||
|
||
assert(m1BPtr(pb) == "Base::m1()"); | ||
assert(m2BPtr(pb) == "Base::m2()"); | ||
|
||
assert(m1BRef(rb) == "Base::m1()"); | ||
assert(m2BRef(rb) == "Base::m2()"); | ||
} | ||
|
||
{ | ||
Derived d; | ||
|
||
Base* pb = &d; | ||
|
||
Base& rb = d; | ||
|
||
assert(m1BValue(d) == "Base::m1()"); | ||
assert(m2BValue(d) == "Base::m2()"); | ||
|
||
assert(m1BPtr(pb) == "Base::m1()"); | ||
assert(m2BPtr(pb) == "Derived::m2()"); | ||
|
||
assert(m1BRef(rb) == "Base::m1()"); | ||
assert(m2BRef(rb) == "Derived::m2()"); | ||
|
||
Derived* pd = &d; | ||
|
||
Derived& rd = d; | ||
|
||
assert(m1BValue(d) == "Base::m1()"); | ||
assert(m2BValue(d) == "Base::m2()"); | ||
|
||
assert(m1BPtr(pd) == "Base::m1()"); | ||
assert(m2BPtr(pd) == "Derived::m2()"); | ||
|
||
assert(m1BRef(rd) == "Base::m1()"); | ||
assert(m2BRef(rd) == "Derived::m2()"); | ||
} | ||
|
||
return 0; | ||
} |