author:&Carlton
tag:C++
topic:【C++】多态、黑马程序员案例— —电脑组装、Visual Studio开发人员工具查看内部结构,cl /d1 reportSingleClassLayout
website:黑马程序员C++
date:2023年7月24日
目录
纯虚函数、抽象类
案例— —电脑组装
抽象类(父类):CPU、显卡、内存条
零件厂商类(子类):Intel、Amd
电脑类:零件搭配,零件工作调用
三个测试对象(电脑对象)
源代码
运行结果
虚函数作用效果与内部结构变化
运行结果变化
内部结构变化
纯虚函数、抽象类
类中只要有一个纯虚函数那这个类就是抽象类,不能实例化对象,而且它的子类必须重写纯虚函数,否则也为抽象类。
案例— —电脑组装
抽象类(父类):CPU、显卡、内存条
//抽象CPU类
class AbstractCPU
{
public:
virtual void calculate() = 0; //纯虚函数
};
//抽象显卡类
class AbstractVideoCard
{
public:
virtual void display() = 0;
};
//抽象内存条类
class AbstractMemory
{
public:
virtual void storage() = 0;
};
零件厂商类(子类):Intel、Amd
//Intel厂商CPU类
class IntelCPU:public AbstractCPU
{
public:
void calculate() //函数重写
{
cout << "Intel的CPU在工作" << endl;
}
};
//Intel厂商显卡类
class IntelVideoCard :public AbstractVideoCard
{
public:
void display()
{
cout << "Intel的显卡在工作" << endl;
}
};
//Intel厂商内存条类
class IntelMemory :public AbstractMemory
{
public:
void storage()
{
cout << "Intel的内存条在工作" << endl;
}
};
//Amd厂商CPU类
class AmdCPU :public AbstractCPU
{
public:
void calculate()
{
cout << "Amd的CPU在工作" << endl;
}
};
//Amd厂商显卡类
class AmdVideoCard :public AbstractVideoCard
{
public:
void display()
{
cout << "Amd的显卡在工作" << endl;
}
};
//Amd厂商内存条类
class AmdMemory :public AbstractMemory
{
public:
void storage()
{
cout << "Amd的内存条在工作" << endl;
}
};
电脑类:零件搭配,零件工作调用
//电脑类
class Computer
{
public:
//使用父类指针定义子类对象
AbstractCPU* m_cpu;
AbstractVideoCard* m_videocard;
AbstractMemory* m_memory;
//初始化搭配零件
Computer(AbstractCPU* cpu,AbstractVideoCard* videocard,AbstractMemory* memory)
{
m_cpu = cpu;
m_videocard = videocard;
m_memory = memory;
}
//电脑工作函数
//调用三个零件
void work()
{
m_cpu->calculate();
m_videocard->display();
m_memory->storage();
}
};
三个测试对象(电脑对象)
//组装第一台电脑
void test01()
{
cout << "第一台电脑在工作:" << endl;
//建立子类指针指向堆区内存区
IntelCPU* cpu = new (IntelCPU);
IntelVideoCard* videocard = new (IntelVideoCard);
IntelMemory* memory = new (IntelMemory);
Computer c1(cpu, videocard, memory);
c1.work();
delete cpu;
delete videocard;
delete memory;
}
//组装第二台电脑
void test02()
{
cout << "第二台电脑在工作:" << endl;
AmdCPU* cpu = new (AmdCPU);
AmdVideoCard* videocard = new (AmdVideoCard);
AmdMemory* memory = new (AmdMemory);
Computer c1(cpu, videocard, memory);
c1.work();
delete cpu;
delete videocard;
delete memory;
}
//组装第三台电脑
void test03()
{
cout << "第三台电脑在工作:" << endl;
IntelCPU* cpu = new (IntelCPU);
AmdVideoCard* videocard = new (AmdVideoCard);
IntelMemory* memory = new (IntelMemory);
Computer c1(cpu, videocard, memory);
c1.work();
delete cpu;
delete videocard;
delete memory;
}
源代码
#include <iostream>
using namespace std;
//抽象CPU类
class AbstractCPU
{
public:
virtual void calculate() = 0; //纯虚函数
};
//抽象显卡类
class AbstractVideoCard
{
public:
virtual void display() = 0;
};
//抽象内存条类
class AbstractMemory
{
public:
virtual void storage() = 0;
};
//Intel厂商CPU类
class IntelCPU:public AbstractCPU
{
public:
void calculate() //函数重写
{
cout << "Intel的CPU在工作" << endl;
}
};
//Intel厂商显卡类
class IntelVideoCard :public AbstractVideoCard
{
public:
void display()
{
cout << "Intel的显卡在工作" << endl;
}
};
//Intel厂商内存条类
class IntelMemory :public AbstractMemory
{
public:
void storage()
{
cout << "Intel的内存条在工作" << endl;
}
};
//Amd厂商CPU类
class AmdCPU :public AbstractCPU
{
public:
void calculate()
{
cout << "Amd的CPU在工作" << endl;
}
};
//Amd厂商显卡类
class AmdVideoCard :public AbstractVideoCard
{
public:
void display()
{
cout << "Amd的显卡在工作" << endl;
}
};
//Amd厂商内存条类
class AmdMemory :public AbstractMemory
{
public:
void storage()
{
cout << "Amd的内存条在工作" << endl;
}
};
//电脑类
class Computer
{
public:
//使用父类指针定义子类对象
AbstractCPU* m_cpu;
AbstractVideoCard* m_videocard;
AbstractMemory* m_memory;
//初始化搭配零件
Computer(AbstractCPU* cpu,AbstractVideoCard* videocard,AbstractMemory* memory)
{
m_cpu = cpu;
m_videocard = videocard;
m_memory = memory;
}
//电脑工作函数
//调用三个零件
void work()
{
m_cpu->calculate();
m_videocard->display();
m_memory->storage();
}
};
//组装第一台电脑
void test01()
{
cout << "第一台电脑在工作:" << endl;
//建立子类指针指向堆区内存区
IntelCPU* cpu = new (IntelCPU);
IntelVideoCard* videocard = new (IntelVideoCard);
IntelMemory* memory = new (IntelMemory);
Computer c1(cpu, videocard, memory);
c1.work();
delete cpu;
delete videocard;
delete memory;
}
//组装第二台电脑
void test02()
{
cout << "第二台电脑在工作:" << endl;
AmdCPU* cpu = new (AmdCPU);
AmdVideoCard* videocard = new (AmdVideoCard);
AmdMemory* memory = new (AmdMemory);
Computer c1(cpu, videocard, memory);
c1.work();
delete cpu;
delete videocard;
delete memory;
}
//组装第三台电脑
void test03()
{
cout << "第三台电脑在工作:" << endl;
IntelCPU* cpu = new (IntelCPU);
AmdVideoCard* videocard = new (AmdVideoCard);
IntelMemory* memory = new (IntelMemory);
Computer c1(cpu, videocard, memory);
c1.work();
delete cpu;
delete videocard;
delete memory;
}
int main()
{
test01();
cout << "---------------" << endl;
test02();
cout << "---------------" << endl;
test03();
return 0;
}
运行结果
虚函数作用效果与内部结构变化
更改抽象CPU类的calculate函数,不使用虚函数
运行结果变化
”CPU不再工作“,不再打印CPU在工作的这一句话,说明此时即便父类指针指向子类对象,但由于没有在父类(即这里的抽象类)上用virtual关键字修饰使用虚函数,没有完成运行多态,对象调用的是父类的calculate函数(即一个空函数)
内部结构变化
使用virtual修饰:
不适用虚函数后:
欢迎指正与分享,谢谢!