析构函数声明为虚函数
#include <iostream>
using namespace std;
class Base{
public:
Base();
virtual ~Base();
protected:
char *str;
};
Base::Base(){
str = new char[100];
cout<<"Base contructor"<<endl;
}
Base::~Base(){
delete[] str;
cout<<"Base destructor"<<endl;
}
class Derived:public Base{
public:
Derived();
~Derived();
private:
char *name;
};
Derived::Derived(){
name = new char[100];
cout<<"Derived construcor"<<endl;
}
Derived::~Derived(){
delete[] name;
cout<<"Derived destructor"<<endl;
}
int main(){
Base *pb = new Derived;
delete pb;
cout<<"-------------------"<<endl;
Derived *pd = new Derived;
delete pd;
system("pause");
return 0;
}
纯虚函数举例
#include <iostream>
using namespace std;
class Line{
public:
Line(float len);
virtual float area() = 0;
virtual float volume() = 0;
protected:
float m_len;
};
Line::Line(float len):m_len(len){}
class Rec:public Line{
public:
Rec(float len,float width);
float area();
protected:
float m_width;
};
Rec::Rec(float len,float width):Line(len),m_width(width){}
float Rec::area(){return m_len*m_width;}
class Cuboid:public Rec{
public:
Cuboid(float len,float width,float height);
float area();
float volume();
protected:
float m_height;
};
Cuboid::Cuboid(float len,float width,float height):Rec(len,width),m_height(height){}
float Cuboid::area(){return 2*(m_len*m_width+m_len*m_height+m_width*m_height);}
float Cuboid::volume(){return m_len*m_width*m_height;}
class Cube:public Cuboid{
public:
Cube(float len);
float area();
float volume();
};
Cube::Cube(float len):Cuboid(len,len,len){}
float Cube::area(){return 6*m_len*m_len;}
float Cube::volume(){return m_len*m_len*m_len;}
int main(){
Line *p = new Cuboid(10,20,30);
cout<<"The area of Cuboid is :"<<p->area()<<endl;
cout<<"The volume of Cuboid is :"<<p->volume()<<endl;
p = new Cube(15);
cout<<"The area of Cube is :"<<p->area()<<endl;
cout<<"The volume of Cube is :"<<p->volume()<<endl;
system("pause");
return 0;
}
理解虚函数对象模型
#include <iostream>
#include <string>
using namespace std;
class People{
public:
People(string name,int age);
public:
virtual void display();
virtual void eating();
protected:
string m_name;
int m_age;
};
People::People(string name,int age):m_name(name),m_age(age){}
void People::display(){
cout<<"class People :"<<m_name<<" 今年:"<<m_age<<"岁了"<<endl;
}
void People::eating(){
cout<<"Class People:我正在吃饭...."<<endl;
}
class Student:public People{
public:
Student(string name,int age,float score);
public:
virtual void display();
virtual void examing();
protected:
float m_score;
};
Student::Student(string name ,int age,float score):
People(name,age),m_score(score){ }
void Student::display(){
cout<<"Class Student: "<<m_name<<"今年"<<m_age<<"岁了,考了"<<m_score<<"分。"<<endl;
}
void Student::examing(){
cout<<"Class Student: "<<m_name<<"正在考试"<<endl;
}
class Senior:public Student{
public:
Senior(string name,int age,float score,bool hasJob);
public:
virtual void display();
virtual void partying();
private:
bool m_hasJob;
};
Senior::Senior(string name,int age,float score,bool hasJob):
Student(name,age,score),m_hasJob(hasJob){}
void Senior::display(){
if(m_hasJob){
cout<<"Class Senior: "<<m_name<<"以"<<m_score<<"的成绩从大学毕业,找到了我工作,今年"<<m_age<<"岁!"<<endl;
}else{
cout<<"Class Senior: "<<m_name<<"以"<<m_score<<"的成绩从大学毕业,没找到工作,几年"<<m_age<<"岁"<<endl;
}
}
void Senior::partying(){
cout<<"Class Senior: 快毕业了,大家吃饭。。。。"<<endl;
}
int main(){
People *p = new People("赵红",20);
p->display();
p = new Student("WANG",16,87);
p->display();
p = new Senior("LI",22,93,true);
p->display();
system("pause");
return 0;
}
ps:摘自c语言中文网