目录
1.多态的基本语法
代码示例:
#include<bits/stdc++.h>
using namespace std;
class music
{
public:
//加virtual实现晚绑定
virtual void listen()
{
cout << "listen music" << endl;
}
};
class rock : public music
{
public:
void listen()
{
cout << "listen rock" << endl;
}
};
//不加virtual是地址早绑定
//如果想要听rock就应该让地址晚绑定,或者说在运行阶段进行绑定
void dolisten(music &m)
{
m.listen();
}
int main()
{
rock r;
dolisten(r);
return 0;
}
2.多态的原理剖析
空类的大小为1
加了virtual后会变为4或8个字节,这恰好是一个指针的大小
#include<bits/stdc++.h>
using namespace std;
class music
{
public:
//加virtual实现晚绑定
virtual void listen()
{
cout << "listen music" << endl;
}
};
class rock : public music
{
public:
void listen()
{
cout << "listen rock" << endl;
}
};
//不加virtual是地址早绑定
//如果想要听rock就应该让地址晚绑定,或者说在运行阶段进行绑定
void dolisten(music &m)
{
m.listen();
}
int main()
{
rock r;
cout << sizeof(rock) << endl;
return 0;
}
vfptr是一个虚函数(表)指针
当子类重写父类的虚函数,子类中的虚函数表位置会替换成子类的虚函数地址
简单理解:就是父类会将它的函数继承给它的子类,但当子类使用virtual(多态后)会将父类继承给它的覆盖掉
多态使用条件:父类的指针或者引用指向子类对象
示例:计算机类
#include<bits/stdc++.h>
using namespace std;
class calculator
{
public:
virtual int getresult()
{
return 0;
}
int x;
int y;
};
class addcalculator : public calculator
{
public:
int getresult()
{
return x + y;
}
};
class subcalculator : public calculator
{
public:
int getresult()
{
return x - y;
}
};
int main()
{
//多态实现计算机的加法和减法
calculator *c = new addcalculator;
c -> x = 50,c -> y = 60;
cout << c -> getresult() << endl;
delete c;
c = new subcalculator;
c -> x = 60,c -> y = 50;
cout << c -> getresult() << endl;
delete c;
return 0;
}
3.纯虚函数和抽象类
#include<bits/stdc++.h>
using namespace std;
class base
{
public:
virtual void func() = 0;
};
class son : public base
{
public:
virtual void func()
{
cout << "func函数调用" << '\n';
}
};
int main()
{
base *b = new son;
b -> func();
delete b;
return 0;
}
示例:制作饮品
#include<bits/stdc++.h>
using namespace std;
class drinking
{
public:
//煮水
virtual void boil() = 0;
//冲泡
virtual void brew() = 0;
//倒入杯中
virtual void pourintocup() = 0;
//加入佐料
virtual void addseasoning() = 0;
//制作饮品
void makedrinking()
{
boil();
brew();
pourintocup();
addseasoning();
}
};
class coffee : public drinking
{
public:
//煮水
virtual void boil(){
cout << "烧开水" << endl;
}
//冲泡
virtual void brew(){
cout << "加咖啡粉" << endl;
}
//倒入杯中
virtual void pourintocup(){
cout << "倒入杯中" << endl;
}
//加入佐料
virtual void addseasoning(){
cout << "加糖,加牛奶" << endl;
}
};
void make(drinking *d)
{
d -> makedrinking();
delete d;
}
int main()
{
//开始做咖啡
make(new coffee);
return 0;
}
4.虚析构和纯虚析构
#include<bits/stdc++.h>
using namespace std;
class animal
{
public:
animal()
{
cout << "animal构造函数调用" << endl;
}
//利用虚析构解决父类指针释放子类对象时不干净的问题
// virtual ~animal()
// {
// cout << "animal虚析构函数调用" << endl;
// }
virtual ~animal() = 0;
//纯虚析构,需要声明也需要实现
//有了纯虚析构之后,这个类也属于抽象类,无法实例化对象
virtual void speak() = 0;
};
animal::~animal()
{
cout << "animal的纯虚构函数调用" << endl;
}
class cat : public animal
{
public:
cat(string name)
{
cout << "cat构造函数调用" << endl;
this -> name = new string(name);
}
virtual void speak()
{
cout << *name << "在说话" << endl;
}
~cat()
{
cout << "cat析构函数调用" << endl;
if(name != NULL)
{
delete name;
name = NULL;
}
}
string *name;
};
int main()
{
animal *a = new cat("Tom");
a -> speak();
delete a;
return 0;
}
示例:电脑组装
#include<bits/stdc++.h>
using namespace std;
class cpu
{
public:
virtual void calculator() = 0;
};
class videocard
{
public:
virtual void display() = 0;
};
class memory
{
public:
virtual void storage() = 0;
};
class computer
{
public:
computer(cpu *c,videocard *v,memory *m)
{
this -> c = c;
this -> v = v;
this -> m = m;
}
void make()
{
c -> calculator();
v -> display();
m -> storage();
}
~computer()
{
if(c != NULL)
{
delete c;
c = NULL;
}
if(v != NULL)
{
delete v;
v = NULL;
}
if(m != NULL)
{
delete m;
m = NULL;
}
}
private:
cpu *c;
videocard *v;
memory *m;
};
class intelcpu : public cpu
{
public:
virtual void calculator()
{
cout << "intelcpu" << endl;
}
};
class amd : public videocard
{
public:
virtual void display()
{
cout << "amdvideocard" << endl;
}
};
class kingston : public memory
{
public:
virtual void storage()
{
cout << "kingstonmemory" << endl;
}
};
int main()
{
cpu *c = new intelcpu;
videocard *v = new amd;
memory *m = new kingston;
computer *com = new computer(c,v,m);
com -> make();
delete com;
return 0;
}