Created
September 24, 2012 06:38
-
-
Save hyfrey/3774624 to your computer and use it in GitHub Desktop.
Cpp Virtual Function
This file contains hidden or 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
#include <iostream> | |
using namespace std; | |
/* | |
###################################################################################################### | |
###################################################################################################### | |
1 | |
*/ | |
class A { | |
public: | |
void f() { | |
std::cout << "A::f()" << std::endl; | |
} | |
}; | |
class B: public A { | |
public: | |
void f() { | |
std::cout << "B::f()" << std::endl; | |
} | |
}; | |
class C: public B { | |
public: | |
void f() { | |
std::cout << "C::f()" << std::endl; | |
} | |
}; | |
class D: public C { | |
public: | |
// No f() | |
}; | |
int main() | |
{ | |
A *a = new A(); | |
B *b = new B(); | |
C *c = new C(); | |
D *d = new D(); | |
a->f(); | |
b->f(); | |
c->f(); | |
d->f(); | |
((B *)c)->f(); | |
((A *)c)->f(); | |
((A *)b)->f(); | |
return 0; | |
} | |
/* | |
###################################################################################################### | |
###################################################################################################### | |
2 | |
*/ | |
class A { | |
public: | |
virtual void f() { | |
std::cout << "A::f()" << std::endl; | |
} | |
}; | |
class B: public A { | |
public: | |
void f() { | |
std::cout << "B::f()" << std::endl; | |
} | |
}; | |
class C: public B { | |
public: | |
void f() { | |
std::cout << "C::f()" << std::endl; | |
} | |
}; | |
int main() | |
{ | |
A *a = new A(); | |
B *b = new B(); | |
C *c = new C(); | |
a->f(); | |
b->f(); | |
c->f(); | |
((B *)c)->f(); | |
((A *)c)->f(); | |
((A *)b)->f(); | |
return 0; | |
} | |
/* | |
###################################################################################################### | |
###################################################################################################### | |
3 | |
*/ | |
class Base { | |
public: | |
void f(); | |
virtual void vf(); | |
}; | |
class Derived : public Base { | |
public: | |
void f(); | |
void vf(); | |
}; | |
#include <iostream> | |
using namespace std; | |
void Base::f() { | |
cout << "Base f()" << endl; | |
} | |
void Base::vf() { | |
cout << "Base vf()" << endl; | |
} | |
void Derived::f() { | |
cout << "Derived f()" << endl; | |
} | |
void Derived::vf() { | |
cout << "Derived vf()" << endl; | |
} | |
int main() | |
{ | |
Base b1; | |
Derived d1; | |
b1.f(); | |
b1.vf(); | |
d1.f(); | |
d1.vf(); | |
Derived d2; | |
Base* bp = &d2; | |
bp->f(); | |
bp->vf(); | |
return 0; | |
} | |
/* | |
###################################################################################################### | |
###################################################################################################### | |
4 | |
*/ | |
class Base | |
{ | |
public: | |
void f() { | |
cout << "Base f()" << endl; | |
g(); | |
} | |
void g() { | |
cout << "Base g()" << endl; | |
} | |
}; | |
class Derived : public Base | |
{ | |
public: | |
void f() { | |
cout << "Derived f()" << endl; | |
Base::f(); | |
} | |
void g() { | |
cout << "Derived g()" << endl; | |
} | |
}; | |
int main() | |
{ | |
Base *pBase = new Derived; | |
pBase->f(); | |
return 0; | |
} | |
/* | |
###################################################################################################### | |
###################################################################################################### | |
5 | |
*/ | |
class Base | |
{ | |
public: | |
virtual void f() { | |
cout << "Base f()" << endl; | |
g(); | |
} | |
void g() { | |
cout << "Base g()" << endl; | |
} | |
}; | |
class Derived : public Base | |
{ | |
public: | |
void f() { | |
cout << "Derived f()" << endl; | |
Base::f(); | |
} | |
void g() { | |
cout << "Derived g()" << endl; | |
} | |
}; | |
int main() | |
{ | |
Base *pBase = new Derived; | |
pBase->f(); | |
return 0; | |
} | |
/* | |
###################################################################################################### | |
###################################################################################################### | |
6 | |
*/ | |
class Base | |
{ | |
public: | |
void f() { | |
cout << "Base f()" << endl; | |
g(); | |
} | |
virtual void g() { | |
cout << "Base g()" << endl; | |
} | |
}; | |
class Derived : public Base | |
{ | |
public: | |
void f() { | |
cout << "Derived f()" << endl; | |
Base::f(); | |
} | |
void g() { | |
cout << "Derived g()" << endl; | |
} | |
}; | |
int main() | |
{ | |
Base *pBase = new Derived; | |
pBase->f(); | |
return 0; | |
} | |
/* | |
###################################################################################################### | |
###################################################################################################### | |
7 | |
*/ | |
class Base | |
{ | |
public: | |
virtual void f() { | |
cout << "Base f()" << endl; | |
g(); | |
} | |
virtual void g() { | |
cout << "Base g()" << endl; | |
} | |
}; | |
class Derived : public Base | |
{ | |
public: | |
void f() { | |
cout << "Derived f()" << endl; | |
Base::f(); | |
} | |
void g() { | |
cout << "Derived g()" << endl; | |
} | |
}; | |
int main() | |
{ | |
Base *pBase = new Derived; | |
pBase->f(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment