面向对象的抽象法则
面向对象的设计在与在原有类的基础之上对原有类进行扩展
如下,我们定义Shape基类,希望可以用circle,squre,来继承基类,
#include<iostream>
using namespace std;
class Shape
{
public:
double Area() const { return 0; }
void Display()
{
cout << Area() << endl;
}
};
class Squre: public Shape
{
public:
Squre(double len) :_len(len) { ; }
double Area() const
{
return _len * _len;
}
private:
double _len;
};
class circle :public Shape
{
public:
circle(double radius) :_radius(radius) { ; }
double Area() const
{
return 3.1415926*_radius*_radius;
}
double _radius;
};
int main()
{
Squre s1(2.0);
circle c1(2.0);
Shape* shapes[2];
shapes[0] = &s1;
shapes[1] = &c1;
for (unsigned int index = 0; index < 2; index++)
{
shapes[index]->Display();
}
return 0;
}
返回值如下:
0
0
D:\VC++\刷题1\Project4\Debug\Project4.exe (进程 5740)已退出,返回代码为: 0。
若要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口...
此时的返回值为0,0,而不是我们想要的circle, squre的面积,为什么会这样呢,第一想法,可以通过查看c1和c2的地址来看他们调用详情,
可以看出shapes存储的地址是不相同的,因此他们调用的均是基类的成员函数,那么为什么会这样
virture关键字
将第六行添加virtule
virtual double Area() const { return 0; }
4
12.5664
D:\VC++\刷题1\Project4\Debug\Project4.exe (进程 21256)已退出,返回代码为: 0。
若要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口...
可以看出此时输出值改变,再看一下内存
那么为什么会这样的,实现的机理又是什么,还是先贴一下代码
#include<iostream>
using namespace std;
class Shape
{
public:
// double Area() const { return 0; }
virtual double Area() const = 0;
virtual void Show()=0; //纯虚函数
void SetColor(int color) { _color = color; }
void Display()
{
cout << Area() << endl;
}
private:
int _color;
};
class Squre: public Shape
{
public:
Squre(double len) :_len(len) { ; }
void Show() { cout << "Squre" << endl; }
double Area() const
{
return _len * _len;
}
private:
double _len;
};
class circle :public Shape
{
public:
circle(double radius) :_radius(radius) { ; }
void Show() { cout << "circle" << endl; }
double Area() const
{
return 3.1415926*_radius*_radius;
}
private:
double _radius;
};
class Triangle :public Shape
{
public:
Triangle(double len, double height) :_len(len),_height(height) { ; }
void Show() { cout << "Triangle" << endl; }
double Area() const
{
return 0.5*_len*_height;
}
private:
double _len;
double _height;
};
int main()
{
//const int shapeNum = 3;
Squre s1(2.0);
s1.SetColor(1);
circle c1(2.0);
//Triangle t1(2.0, 3.0);
Shape* shapes[2];
shapes[0] = &s1;
shapes[1] = &c1;
// shapes[2] = &t1;
for (unsigned int index = 0; index < 2; index++)
{
shapes[index]->Display();
}
cout << sizeof(s1) << endl;
return 0;
}
然后在看一下运行内存,
可以看出squre 中有三个单元,一个是本身的double数据,一个是从基类中继承而来的int数据,还有一个_vfptr,可以观察一下右侧_vptr的内存数据,可以看出,其存储了[0],[1]的内存地址,分别表示了两个成员函数的地址,如果有多个子类时,每个的虚函数表又会怎样,
实际上c++会为每个对象创建一个续表,对象的首地址存放的元素便是虚表的地址
可以清晰的看出,对象的地址就存放着虚表,那么我们就找到了虚表的位置,而虚表的地址存放的内容我们上面已经看过,就是对应的第一个虚函数,在32位操作系统是4个字节。此处有一个问题,既然虚表指向第一个·虚函数,那么怎么选择调用的哪个虚函数呢?
补充一点同一个类的对象,共用一个虚表,虚表的地址相同