目录
抽象类
看以下例子,对于Human类中的虚函数,后面加上"=0"就变为了纯虚函数,连空函数都不需要提供,纯虚函数不需要定义,一个类中如果含有纯虚函数就称为抽象类,在main函数中抽象类不能有实例对象
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
int a;
public:
virtual void eating(void) = 0;
virtual void wearing(void) = 0;
virtual void driving(void) = 0;
virtual ~Human() { cout<<"~Human()"<<endl; }
virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};
class Englishman : public Human {
public:
void eating(void) { cout<<"use knife to eat"<<endl; }
void wearing(void) {cout<<"wear english style"<<endl; }
void driving(void) {cout<<"drive english car"<<endl; }
virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};
class Chinese : public Human {
public:
void eating(void) { cout<<"use chopsticks to eat"<<endl; }
void wearing(void) {cout<<"wear chinese style"<<endl; }
void driving(void) {cout<<"drive chinese car"<<endl; }
virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};
int main(int argc, char **argv)
{
//Human h;
Englishman e;
Chinese c;
return 0;
}
如果Chinese类中的driving函数注释了,则Chinese也是一个抽象类,不能被实例化,若子类没有覆写所有的纯虚函数,则子类还是抽象类
class Chinese : public Human {
public:
...
//void driving(void) {cout<<"drive chinese car"<<endl; }
...
};
对于Chinese再延伸出派生类,在子类中覆写driving函数就可以进行实例化
class Guangximan : public Chinese {
void driving(void) {cout<<"drive guangxi car"<<endl; }
};
抽象类界面
一个程序由多个人编写,分为应用编程和类编程,将上述程序分为类编程Human、English和Chinese,和应用编程main.cpp
前奏
如下程序中,English和Chinese都是Human的派生类,因此我们可以把共性的东西抽出来,例如name,这样派生类盘大的时候就可以节省代码
/* Human.h */
#ifndef _HUMAN_H
#define _HUMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
char *name;
public:
void setName(char *name);
char *getName(void);
virtual void eating(void){cout<<"use hand to eat"<<endl;}
virtual void wearing(void){}
virtual void driving(void){}
};
#endif
/* Human.cpp */
#include "Human.h"
void Human::setName(char *name)
{
this->name = name;
}
char *Human::getName(void)
{
return this->name;
}
/* Englishman.h */
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Englishman : public Human {
public:
void eating(void);
void wearing(void);
void driving(void);
~Englishman();
};
#endif
/* Englishman.cpp */
#include "Englishman.h"
void Englishman::eating(void)
{
cout<<"use knife to eat"<<endl;
}
void Englishman::wearing(void)
{
cout<<"wear english style"<<endl;
}
void Englishman::driving(void)
{
cout<<"drive english car"<<endl;
}
Englishman::~Englishman()
{
cout<<"~Englishman()"<<endl;
}
/* Chinese.h */
#ifndef _CHINESE_H
#define _CHINESE_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Chinese : public Human{
public:
void eating(void);
void wearing(void);
void driving(void);
~Chinese();
};
#endif
/* Chinese.cpp */
#include "Chinese.h"
void Chinese::eating(void)
{
cout<<"use chopsticks to eat"<<endl;
}
void Chinese::wearing(void)
{
cout<<"wear chinese style"<<endl;
}
void Chinese::driving(void)
{
cout<<"drive chinese car"<<endl;
}
Chinese::~Chinese()
{
cout<<"~Chinese()"<<endl;
}
/* main.cpp */
#include "Human.h"
#include "Englishman.h"
#include "Chinese.h"
void test_eating(Human *h)
{
h->eating();
}
int main(int argc, char **argv)
{
Englishman e;
Chinese c;
Human* h[2] = {&e, &c};
int i;
for (i = 0; i < 2; i++)
test_eating(h[i]); //需要在human里面实现多态 加上virtual
return 0;
}
执行结果如下,对于test_eating函数实现多态,在基类加上virtual就可以了
use knife to eat
use chopsticks to eat
~Chinese()
~Englishman()
将程序在Human中将函数编写为纯虚函数,则Human类变为抽象类,这样Human节省空间,连空函数都不需要,同时可以防止Human类的实例化,重新编译执行程序,依然可以实现多态
class Human {
private:
char *name;
public:
void setName(char *name);
char *getName(void);
virtual void eating(void) = 0;
virtual void wearing(void) = 0;
virtual void driving(void) = 0;
};
修改Makefile
将应用编程和类编程分开,将类编程编译为动态库,在修改类编程的时候,就不需要重新编译应用编程
Human: main.o libHuman.so
g++ -o $@ $< -L./ -lHuman
%.o : %.cpp
g++ -fPIC -c -o $@ $<
libHuman.so : Englishman.o Chinese.o Human.o
g++ -shared -o $@ $^
clean:
rm -f *.o Human
同时需要指定一下动态库环境变量为当前路径,执行结果和上述一样
export LD_LIBRARY_PATH=./
先来修改程序,修改English.h和English.cpp加上一个数组表示地址
/* English.h */
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Englishman : public Human {
private:
char address[100];
int age;
public:
void eating(void);
void wearing(void);
void driving(void);
Englishman();
Englishman(char *name, int age, char *address);
~Englishman();
};
#endif
/* English.cpp */
#include "Englishman.h"
void Englishman::eating(void)
{
cout<<"use knife to eat"<<endl;
}
void Englishman::wearing(void)
{
cout<<"wear english style"<<endl;
}
void Englishman::driving(void)
{
cout<<"drive english car"<<endl;
}
Englishman::~Englishman()
{
cout<<"~Englishman()"<<endl;
}
Englishman::Englishman() {}
Englishman::Englishman(char *name, int age, char *address)
{
setName(name);
this->age = age;
memset(this->address, 0, 100);
strcpy(this->address, address);
}
/* main.cpp */
#include "Human.h"
#include "Englishman.h"
#include "Chinese.h"
void test_eating(Human *h)
{
h->eating();
}
int main(int argc, char **argv)
{
Englishman e("Bill", 10, "sfwqerfsdfas");
Chinese c;
Human* h[2] = {&e, &c};
int i;
for (i = 0; i < 2; i++)
test_eating(h[i]);
return 0;
}
重新通过make来编译生成库执行程序没有问题,当我们修改以下语句的时候,再调用刚才生成的动态库执行程序,程序崩溃
/* English.h */
char address[1000];
/* English.cpp */
memset(this->address, 0, 1000);
引出抽象类界面
对于上述情况,我们让应用编程只和Human类打交道,将容易变化的类隔离开,先来修改程序
在Chinese.cpp和English.cpp中都加上一个接口
/* Chinese.cpp */
...
Human& CreateChinese(char *name, int age, char *address)
{
return *(new Chinese()); //在Chinese类中没有提供name,这里就不传递参数
}
/* English.cpp */
...
Human& CreateEnglishman(char *name, int age, char *address)
{
return *(new Englishman(name, age, address));
}
然后在Human.h中声明
#ifndef _HUMAN_H
#define _HUMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
char *name;
public:
void setName(char *name);
char *getName(void);
virtual void eating(void) = 0;
virtual void wearing(void) = 0;
virtual void driving(void) = 0;
};
Human& CreateEnglishman(char *name, int age, char *address);
Human& CreateChinese(char *name, int age, char *address);
#endif
在main.cpp中就只需要包含Human.h头文件
#include "Human.h"
//#include "Englishman.h"
//#include "Chinese.h"
void test_eating(Human *h)
{
h->eating();
}
int main(int argc, char **argv)
{
Human& e = CreateEnglishman("Bill", 10, "sfwqerfsdfas");
Human& c = CreateChinese("zhangsan", 11, "beijing");
Human* h[2] = {&e, &c};
int i;
for (i = 0; i < 2; i++)
test_eating(h[i]);
return 0;
}
这样重新编译,再根据上面的方法去修改address的大小,直接执行程序依然正常运行,这样,中间引入了一个相对固定的Human.h,就是所谓的抽象类界面
完善程序加上析构函数,修改各类,同时将析构函数编写为虚函数,实现调用的时候是自己的析构函数,不应该写成纯虚函数,否则在派生类中需要有一个"~Human()"命名的析构函数
class Chinese : public Human{
public:
void eating(void);
void wearing(void);
void driving(void);
virtual ~Chinese();
};
...
class Englishman : public Human {
private:
char address[100];
int age;
public:
void eating(void);
void wearing(void);
void driving(void);
Englishman();
Englishman(char *name, int age, char *address);
virtual ~Englishman();
};
...
class Human {
private:
char *name;
public:
void setName(char *name);
char *getName(void);
virtual void eating(void) = 0;
virtual void wearing(void) = 0;
virtual void driving(void) = 0;
virtual ~Human() {cout<<"~Human"<<endl;}
};
...
int main(int argc, char **argv)
{
Human& e = CreateEnglishman("Bill", 10, "sfwqerfsdfas");
Human& c = CreateChinese("zhangsan", 11, "beijing");
Human* h[2] = {&e, &c};
int i;
for (i = 0; i < 2; i++)
test_eating(h[i]);
delete &e;
delete &c;
return 0;
}