
Header:
http://pastebin.com/ZMv9qny4
Implementation:
http://pastebin.com/KjUizxUR
Code: Select all
class A {
public:
virtual void say() { std::cout << "A::say()\n"; }
};
class B: public A {
public:
void say() { std::cout << "B::say()\n"; }
};
class C: public B {
public:
void say() { std::cout << "C::say()\n"; }
};
int main() {
B b; C c;
dynamic_cast<B*>(&b)->say();
dynamic_cast<B*>(&c)->say();
}
Code: Select all
B::say()
C::say()
Code: Select all
B::say()
B::say()
Nope.Eraser wrote:Doesn't overriding non-virtual methods throw a compiler error in C++? I think the Visual Studio C# compiler does.
Code: Select all
class animal{
public:
virtual ~animal(){}
virtual void function_one(){ std::cout << "generic animal sound one" << std::endl; }
void function_two(){ std::cout << "generic animal sound two" << std::endl; }
virtual void table_example(){ /* I do nothing */ }
};
class cat: public animal{
public:
~cat(){}
virtual void function_one(){ std::cout << "meow" << std::endl; }
void function_two(){ std::cout << "purrrrr" << std::endl; }
virtual void table_example(){ /* I also do nothing*/ }
};
class dog : public animal{
public:
~dog(){}
virtual void function_one(){ std::cout << "woof" << std::endl; }
void function_two(){ std::cout << "grrrrrrrrrrrrrr" << std::endl; }
};
Code: Select all
animal *kitty = new cat();
kitty->function_one(); //calling cat::function_one();
kitty->function_two(); //calling animal::function_two();
delete kitty;
animal *montgomery = new dog();
montgomery->function_one(); //calling dog::function_one();
montgomery->function_two(); //calling animal::function_two();
delete montgomery;
Code: Select all
cat *kitty = new cat();
kitty->function_one();
kitty->function_two();
delete kitty;
My novel in a sentence.^misantropia^ wrote:I think Eraser is confusing C# with C++.
Confusing? I explicitly drew a parallel to C# in my post and asked if the worked the same?^misantropia^ wrote:I think Eraser is confusing C# with C++.