Take a look:
class Base {
//..
virtual void method1() { std::cout << "Base::method1()" << std::endl; }
virtual void method2() { std::cout << "Base::method2()" << std::endl; }
};
class Derieved : public Base {
//..
virtual void method1() { std::cout << "Derieved::method1()" << std::endl; }
};
class Multilply : public Derieved {
//..
virtual void method2() { std::cout << "Multiply::method2()" << std::endl; }
};
Multiply obj;
obj.method1();
obj.method2();
dynamic_cast<Base>(obj).method2();
class Base {
//..
virtual void method1() { std::cout << "Base::method1()" << std::endl; }
virtual void method2() { std::cout << "Base::method2()" << std::endl; }
};
class Derieved : public Base {
//..
virtual void method1() { std::cout << "Derieved::method1()" << std::endl; }
};
class Multilply : public Derieved {
//..
virtual void method2() { std::cout << "Multiply::method2()" << std::endl; }
};
Multiply obj;
obj.method1();
obj.method2();
dynamic_cast<Base>(obj).method2();
To copy to clipboard, switch view to plain text mode
This will produce the following:
Derieved::method1()
Multiply::method2()
Base::method1()
Now line-by-line comments:
- Class Multiply inherits all the methods from class Derieved. It doesn't overload method1(), so Multiply::method1() in fact calls Derieved::method1()
- Mulitply::method2() is overloaded
- Class Base is Muliply class' ancestor, so it could be dynamic_cast<>()ed to Base class to access its methods
Bookmarks