第9节 C++虚函数和多态
虚函数和虚函数表
什么是虚函数?用virtual修饰的成员函数叫做虚函数
虚函数对于类的影响
- 增加一个指针的内存,32位4个字节,6位就是8个字节
虚函数表(了解一下):就是一个指针存储所有虚函数的首地址
#include<iostream>
using namespace std;
class MM
{
public:
virtual void print()
{
cout << "虚函数1" << endl;
}
virtual void print2()
{
cout << "虚函数2" << endl;
}
virtual void print3();
protected:
};
void MM::print3()
{
cout << "虚函数3" << endl;
}
class A
{
int age;
};
void testVirtual()
{
cout << sizeof(MM) << endl;
cout << sizeof(A) << endl;
MM mm;
int** vptr = (int**)&mm;
typedef void(*PF)();
PF func = (PF)vptr[0][0];
func();
func = (PF)vptr[0][1];
func();
}
int main()
{
testVirtual();
return 0;
}
虚函数和多态
多态定义:同一种行为(调用)导致的不同的结果
多态的必要性原则
- 必须父类存在虚函数
- 子类必须采用public继承
- 必须存在指针的引用(使用)
#include<iostream>
using namespace std;
class Man
{
public:
void WC1()
{
cout << "狗妈妈走路" << endl;
}
virtual void WC2()
{
cout << "小狗走路" << endl;
}
protected:
};
class Woman:public Man
{
public:
void WC1()
{
cout << "猫猫走路" << endl;
}
void WC2()
{
cout << "猫猫走路" << endl;
}
protected:
};
void testVirtual()
{
cout << "正常访问,就近原则" << endl;
Man pm;
pm.WC1();
pm.WC2();
Woman woman;
woman.WC1();
woman.WC2();
cout << "指针访问,正常赋值" << endl;
Man* pm1 = new Man;
pm1->WC1();
pm1->WC2();
Woman* pw = new Woman;
pw->WC1();
pw->WC2();
cout << "指针非正常赋值:子类对象初始化父类指针" << endl;
Man* parent = new Woman;
parent->WC1();
parent->WC2();
parent = new Man;
parent->WC2();
}
class Shape
{
public:
virtual void Draw()
{
cout << "绘制过程" << endl;
}
protected:
};
class Rect :public Shape
{
public:
virtual void Draw()
{
cout << "画矩形" << endl;
}
protected:
};
class Circle :public Shape
{
public:
virtual void Draw()
{
cout << "画圆" << endl;
}
protected:
};
void printInfo(Man* parent)
{
parent->WC2();
}
class Triangle :public Shape
{
public:
void Draw()
{
cout << "绘制三角形" <<endl;
}
};
class Elliptical :public Shape
{
public:
void Draw()
{
cout << "绘制椭圆" << endl;
}
};
class Tool
{
public:
void draw(Shape* parent)
{
parent->Draw();
}
protected:
};
int main()
{
testVirtual();
printInfo(new Woman);
Tool* pTool = new Tool;
pTool->draw(new Circle);
pTool->draw(new Rect);
pTool->draw(new Triangle);
pTool->draw(new Elliptical);
return 0;
}
virutal void print()=0;
#include <iostream>
using namespace std;
class Parent
{
public:
virtual void print() = 0;
protected:
};
void testAbstract()
{
Parent* parent = nullptr;
}
class stack
{
public:
virtual void push(int data) = 0;
virtual void pop() = 0;
virtual int top() const = 0;
virtual bool empty() const = 0;
virtual int size() const = 0;
};
class arrayStack :public stack
{
public:
void push(int data)
{
}
void pop()
{
}
int top() const
{
return 1;
}
bool empty() const
{
return true;
}
int size() const
{
return 1;
}
protected:
int* array;
};
struct Node
{
int data;
Node* next;
};
class listStack :public stack
{
public:
void push(int data)
{
}
void pop()
{
}
int top() const
{
return 1;
}
bool empty() const
{
return true;
}
int size() const
{
return 1;
}
protected:
Node* headNode;
};
void testStack(stack* pStack)
{
pStack->push(1);
while (!pStack->empty())
{
cout << pStack->top();
pStack->pop();
}
}
class A
{
public:
virtual void print() = 0;
protected:
};
class B :public A
{
public:
void print()
{
cout << "B" << endl;
}
};
class C :public B
{
public:
void print()
{
cout << "C" << endl;
}
};
void Abtract()
{
C c;
B* pc = new C;
pc->print();
}
int main()
{
testStack(new arrayStack);
testStack(new listStack);
Abtract();
return 0;
}
虚析构函数
#include <iostream>
using namespace std;
class parent
{
public:
virtual ~parent()
{
cout << "父类析构" <<endl;
}
void print(){}
};
class son:public parent
{
public:
~son()
{
cout << "子类析构" <<endl;
}
void print(){}
};
int main()
{
parent* p = new son;
p->print();
delete p;
return 0;
}
|