// c_datastructure.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
class Student {
public:
Student() {}
Student(int n, string nam, float sco) :num(n), name(nam), score(sco) {}
virtual ~Student() { cout << "Student destructor" << endl; }
virtual void display();
protected:
int num;
string name;
float score;
};
void Student::display() {
cout << " num : " << num << endl;
cout << " name : " << name << endl;
cout << " score : " << score << endl;
}
class Graduate :public Student{
public:
Graduate() {}
Graduate(int n, string nam, float sco, float w) :Student(n, nam, sco), wage(w) {}
~Graduate() { cout << "Graduate destructor" << endl; }
virtual void display();
private:
float wage;
};
void Graduate::display() {
cout << " num : " << num << endl;
cout << " name : " << name << endl;
cout << " score : " << score << endl;
cout << " wage : " << wage << endl;
}
int main()
{
cout << "************" << endl;
Student *s = new Graduate;
delete s;
// (*str).display();
// system("pause");
/*
int A[] = { 1, -2, 3, 10, -4, 7, 2 };
int maxSum = maxSubArray_baoli(A, 7);
cout << maxSum << endl;
cout << maxSubArray_fenzhi(A, 0, 7) << endl;
cout << maxSubArray_dongtaiguihua(A, 7) << endl;
*/
return 0;
}
在用new建立一个临时的对象,如果基类中有析构函数,同时还定义一个指向基类的指针对象。在程序用delete试图撤销内存时,系统反而会先调用基类的析构函数,不执行派生类析构函数,为什么呢?原因和前面一篇博客介绍一样。如果想要执行派生类的析构函数怎么做呢?那么需要把基类的析构函数声明为虚函数,virtual ~Student(){};
virtual ~Student() { cout << "Student destructor" << endl; }
先调用派生类的析构函数,再调用基类的析构函数。
c++做法最好是在基类中定义析构函数为虚函数,即为常说的虚析构函数,那么在delete对象时候,而且delete对象是指向派生类对象的基类指针,那么系统会相应调用析构函数,从而保证正确处理动态存储空间。C++默认把基类析构函数定义为虚析构函数后,其所有的派生类的析构函数自动变为虚函数。
下面介绍下纯虚函数:
纯虚函数定义:
virtual 函数类型 韩树名 (参数列表) = 0;
上面介绍到虚函数的左右,其实在基类中该函数可能并没有发挥任何作用,但是考虑到派生类的需求,因此在基类中预留了一个函数名,具体实现的功能留给派生类去定义。而纯虚函数没有函数体,最后的0并不是代表该函数被初始化为0,而是在形式上告诉编译系统,这是纯虚函数。到派生类后具体去实现定义,如果在派生类中没有定义,那么该虚函数仍然为纯虚函数,直到下一级的派生类对其定义。
// c_datastructure.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
class Student {
public:
Student() {}
Student(int n, string nam, float sco) :num(n), name(nam), score(sco) {}
virtual ~Student() { cout << "Student destructor" << endl; }
virtual void display();
virtual void fun() = 0;
protected:
int num;
string name;
float score;
};
void Student::display() {
cout << " num : " << num << endl;
cout << " name : " << name << endl;
cout << " score : " << score << endl;
}
class Graduate :public Student{
public:
Graduate() {}
Graduate(int n, string nam, float sco, float w) :Student(n, nam, sco), wage(w) {}
~Graduate() { cout << "Graduate destructor" << endl; }
virtual void display();
void fun() { cout << "我是纯虚函数" << endl; }
protected:
float wage;
};
void Graduate::display() {
cout << " num : " << num << endl;
cout << " name : " << name << endl;
cout << " score : " << score << endl;
cout << " wage : " << wage << endl;
}
int maxSubArray_baoli(int A[], int n);
double maxSubArray_fenzhi(int A[], int left, int right);
double max(double, double);
int maxSubArray_dongtaiguihua(int A[], int n);
int main()
{
cout << "************" << endl;
Student *s = new Graduate;
s->fun();
delete s;
// (*str).display();
// system("pause");
/*
int A[] = { 1, -2, 3, 10, -4, 7, 2 };
int maxSum = maxSubArray_baoli(A, 7);
cout << maxSum << endl;
cout << maxSubArray_fenzhi(A, 0, 7) << endl;
cout << maxSubArray_dongtaiguihua(A, 7) << endl;
*/
return 0;
}
抽象类
有了纯虚函数后,就会出现抽象类,这种只用来作为一种基本类型而不做继承的类,称为抽象类或者抽象基类,包含有纯虚函数的类都是不能建立对象的,因为纯虚函数不能被调用,他只是为下面的派生类族提供一个“公共接口”。因此下面派生类中也可能是抽象类,只要包含有纯虚函数。但是可以定义指向抽象类的数据的指针变量,当派生类成为具体的类后,可以用这种指针指向派生类对象,通过指针变量调用虚函数进行操作,实现多态。