含有纯虚函数的类叫做抽象类
抽象类无法实例化对象
抽象类的子类也可以是抽象类
Person.h
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
using namespace std;
class Person {
public:
Person(string name);
virtual void work() = 0;
virtual ~Person() {};
private:
string m_strName;
};
#endif // !PERSON_H
Person.cpp
#include "Person.h"
Person::Person(string name) {
m_strName = name;
}
Worker.h
#ifndef WORKER_H
#define WORKER_H
#include "Person.h"
class Worker :public Person{
public:
Worker(string name, int age);
//virtual void work();
private:
int m_iAge;
};
#endif // !WORKER_H
Worker.cpp
#include "Worker.h"
#include <iostream>
using namespace std;
Worker::Worker(string name, int age) :Person(name)
{
m_iAge = age;
}
//void Worker::work() {
//cout << "work" << endl;
//}
Dustman.h
#ifndef DUSTMAN_H
#define DUSTMAN_H
#include "Worker.h"
class Dustman :public Worker
{
public:
Dustman(string name, int age);
virtual void work();
};
#endif // !DUSTMAN_H
Dustman.cpp
#include "Dustman.h"
#include <iostream>
using namespace std;
Dustman::Dustman(string name, int age) :Worker(name, age)
{
}
void Dustman::work()
{
cout << "扫地" << endl;
}
Demo.cpp
#include <stdlib.h>
#include "Dustman.h"
using namespace std;
int main(void)
{
Dustman dustman("Zhangsan", 20);
dustman.work();
system("pause");
return 0;
}