C++多态与指针的强制转换
2016-02-29 18:05
507 查看
#include "stdafx.h"
#include <iostream>
using namespace std;
class Base
{
public:
virtual void f(float x)
{
cout << "Base::f(float) " << x << endl;
}
void g(float x)
{
cout << "Base::g(float) " << x << endl;
}
void h(float x)
{
cout << "Base::h(float) " << x << endl;
}
};
class Derived : public Base
{
public:
virtual void f(float x)
{
cout << "Derived::f(float) " << x << endl;
}
void g(int x)
{
cout << "Derived::g(int) " << x << endl;
}
void h(float x)
{
cout << "Derived::h(float) " << x << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Derived d;
Base *pb = &d;
Derived *pd = &d;
// Good : behavior depends solely on type of the object
pb->f(3.14f); // Derived::f(float) 3.14
pd->f(3.14f); // Derived::f(float) 3.14
// Bad : behavior depends on type of the pointer
pb->g(3.14f); // Base::g(float) 3.14
pd->g(3.14f); // Derived::g(int) 3 (surprise!)
// Bad : behavior depends on type of the pointer
pb->h(3.14f); // Base::h(float) 3.14 (surprise!)
pd->h(3.14f); // Derived::h(float) 3.14
cout<<endl<<endl;
Base *pnb = new Derived;
Derived *pnd = new Derived;
pnb->f(6.28f);// Derived::f(float) 6.28
pnd->f(6.28f);// Derived::f(float) 6.28
pnb->g(6.28f);//Base::g(float) 6.28
pnd->g(6.28f);// Derived::g(int) 6
pnb->h(6.28f);//Base::h(float) 6.28
pnd->h(6.28f);// Derived::h(float) 6.28
cout<<endl<<endl;
Base *b = new Base;
Derived *pzd = (Derived*)b;
b->f(9.42f);//Base::f(float) 9.42
pzd->f(9.42f);//Base::f(float) 9.42
b->g(9.42f);//Base::g(float) 9.42
pzd->g(9.42f);Derived::g(int) 9
b->h(9.42f);//Base::h(float) 9.42
pzd->h(9.42f);Derived::h(float) 9.42
system("pause");
return 0;
}
#include <iostream>
using namespace std;
class Base
{
public:
virtual void f(float x)
{
cout << "Base::f(float) " << x << endl;
}
void g(float x)
{
cout << "Base::g(float) " << x << endl;
}
void h(float x)
{
cout << "Base::h(float) " << x << endl;
}
};
class Derived : public Base
{
public:
virtual void f(float x)
{
cout << "Derived::f(float) " << x << endl;
}
void g(int x)
{
cout << "Derived::g(int) " << x << endl;
}
void h(float x)
{
cout << "Derived::h(float) " << x << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Derived d;
Base *pb = &d;
Derived *pd = &d;
// Good : behavior depends solely on type of the object
pb->f(3.14f); // Derived::f(float) 3.14
pd->f(3.14f); // Derived::f(float) 3.14
// Bad : behavior depends on type of the pointer
pb->g(3.14f); // Base::g(float) 3.14
pd->g(3.14f); // Derived::g(int) 3 (surprise!)
// Bad : behavior depends on type of the pointer
pb->h(3.14f); // Base::h(float) 3.14 (surprise!)
pd->h(3.14f); // Derived::h(float) 3.14
cout<<endl<<endl;
Base *pnb = new Derived;
Derived *pnd = new Derived;
pnb->f(6.28f);// Derived::f(float) 6.28
pnd->f(6.28f);// Derived::f(float) 6.28
pnb->g(6.28f);//Base::g(float) 6.28
pnd->g(6.28f);// Derived::g(int) 6
pnb->h(6.28f);//Base::h(float) 6.28
pnd->h(6.28f);// Derived::h(float) 6.28
cout<<endl<<endl;
Base *b = new Base;
Derived *pzd = (Derived*)b;
b->f(9.42f);//Base::f(float) 9.42
pzd->f(9.42f);//Base::f(float) 9.42
b->g(9.42f);//Base::g(float) 9.42
pzd->g(9.42f);Derived::g(int) 9
b->h(9.42f);//Base::h(float) 9.42
pzd->h(9.42f);Derived::h(float) 9.42
system("pause");
return 0;
}
相关文章推荐
- 为什么学C++比C#的长期风险低
- 2012年第三届蓝桥杯C/C++省赛 微生物增殖
- c++类的隐式转换与强制转换重载详解
- C++虚基类的实现机制
- c++ 虚函数的实现机制
- C++对象模型笔记:对象的三种内存布局
- C++ sort函数的用法
- 【POJ 3630】Phone List 中文题意&题解&代码(C++)
- C++的个人真实学习经验
- 巧用c语言中数组部分元素初始化
- c++中堆和栈的区别
- 名字修饰约定extern "C"与extern "C++"浅析
- C++:类模板
- c++头文件:stdio.h ,cstdio ,iostream ,bits/stdc++.h
- VS2008解决objcore.cpp Line: 40 Debug Assertion Failed!
- 九度-题目1186:打印日期
- 【C++】数组
- 关于在c++中的string操作题-杭电1004
- C语言中函数参数传递
- [转] 关于C++中模板中的typename和class的区别比较