参考博客:https://blog.csdn.net/wendy_keeping/article/details/75213671
(一)基类指针指向派生类对象只能访问基类成员函数和派生类中的虚函数
#include <iostream>
using namespace std;
class Base
{
public:
Base(int data) :_ma(data)
{
cout << "Base()" << endl;
}
virtual ~Base()
{
cout << "~Base()" << endl;
}
void Show()
{
cout << "Base::Show()" << endl;
}
private:
int _ma;
};
class Derive : public Base
{
public:
Derive(int data1, int data2) :Base(data1), _mb(data2)
{
cout << "Derive()" << endl;
}
~Derive()
{
cout << "~Derive()" << endl;
}
virtual void Show()
{
cout << "Derive::Show()" << endl;
}
void test()
{
cout << "true" << endl;
}
private:
int _mb;
};
int main()
{
//基类指针指向派生类对象,只能访问基类成员函数和派生类中的虚函数
Base* p = new Derive(10, 10);
p->Show();//编译期间已经静态绑定为基类函数
//p->test(); Base类中没有test()函数,所以编译出错
delete p;
return 0;
}
运行结果:
Base()
Derive()
Base::Show()
~Derive()
~Base()
(二)虚函数实现动态多态
#include <iostream>
using namespace std;
class Base
{
public:
Base(int data) :_ma(data)
{
cout << "Base()" << endl;
}
virtual ~Base()
{
cout << "~Base()" << endl;
}
virtual void Show()
{
cout << "Base::Show()" << endl;
}
void Show(int i)
{
cout << "Base:Show(int)" << endl;
}
private:
int _ma;
};
class Derive : public Base
{
public:
Derive(int data1, int data2) :Base(data1), _mb(data2)
{
cout << "Derive()" << endl;
}
~Derive()
{
cout << "~Derive()" << endl;
}
private:
virtual void Show()
{
cout << "Derive::Show()" << endl;
}
private:
int _mb;
};
int main()
{
Base* p = new Derive(10, 20);//基类指针指向派生类对象
p->Show();//基类中已经声明为虚函数
delete p;
return 0;
}
运行结果:
Base()
Derive()
Derive::Show()
~Derive()
~Base()
说明:多态与成员函bai数的访问权限是没有关du系的, 即两回事.
基类定义了虚函数, 并且是public的。那么子类只要override 虚函数 无论放在什么样dao的访问权限下(private,protect,public), 都以基类的访问权限为准, 即是public的.
(三)虚函数赋值问题
#include <iostream>
using namespace std;
class Base
{
public:
Base(int data) :_ma(data)
{
cout << "Base()" << endl;
}
virtual ~Base()
{
cout << "~Base()" << endl;
}
virtual void Show(int i = 10)//虚函数
{
cout << "Base::Show(), i=" << i << endl;
}
private:
int _ma;
};
class Derive : public Base
{
public:
Derive(int data1, int data2) :Base(data1), _mb(data2)
{
cout << "Derive()" << endl;
}
~Derive()
{
cout << "~Derive()" << endl;
};
virtual void Show(int i = 20)//虚函数
{
cout << "Derive::Show(), i=" << i << endl;
}
private:
int _mb;
};
int main()
{
Base* p = new Derive(10, 10);
p->Show();
delete p;
return 0;
}
若要在虚函数中赋值,不要再基类虚函数中赋予默认值。编译器调用基类的函数进行了值得初始化。
运行结果:
Base()
Derive()
Derive::Show(), i=10
~Derive()
~Base()
(四)非法访问内存
#include <iostream>
using namespace std;
class Base
{
public:
Base(int data) :_ma(data)
{
cout << "Base()" << endl;
Clear();
}
void Clear()
{
memset(this, 0, sizeof(*this));
}
virtual ~Base()
{
cout << "~Base()" << endl;
}
virtual void Show()
{
cout << "Base::Show()" << endl;
}
private:
int _ma;
};
int main()
{
Base b(10);
Base* p = &b;
p->Show();
delete p;
return 0;
}
运行结果:
程序崩溃。
在Base类的构造函数中调用clear()函数,将刚构造的对象b清空了。p调用show(),非法访问内存,程序崩溃!
(五)构造函数中有虚函数
#include <iostream>
using namespace std;
class Base
{
public:
Base(int data) :_ma(data)
{
cout << "Base()" << endl;
Show();
}
virtual ~Base()
{
cout << "~Base()" << endl;
}
virtual void Show()
{
cout << "Base::Show()" << endl;
}
private:
int _ma;
};
class Derive : public Base
{
public:
Derive(int data1, int data2) :Base(data1), _mb(data2)
{
cout << "Derive()" << endl;
Show();
}
~Derive()
{
cout << "~Derive()" << endl;
}
virtual void Show()
{
cout << "Derive::Show()" << endl;
}
private:
int _mb;
};
int main()
{
Derive d(10, 10);
return 0;
}
在构造函数里调用虚函数,都是静态绑定。Base类构造函数调用Base::Show();Derive类构造函数调用Derive::Show()
运行结果
Base()
Base::Show()
Derive()
Derive::Show()
~Derive()
~Base()