2012年(1008)
分类:
2012-08-01 11:08:45
原文地址:虚函数实例 作者:luozhiyong131
/*
* 虚函数
* 没有使用虚函数,说明引用虚函数的目的
* Lzy 2011-七夕
*/
#include
using namespace std;
static int Line =1;
class CAnimal
{
protected:
char *name;
public:
CAnimal(char *na){
name = new char[strlen(na)+1];
strcpy(name, na);
}
void display(){cout<
};
class CHerbivorous:public CAnimal
{
public:
CHerbivorous(char *na):CAnimal(na){}
void display(){cout<
};
class CCarnivore:public CAnimal
{
public:
CCarnivore(char *na):CAnimal(na){}
void display(){cout<
};
int main(void)
{
CAnimal animal(""), *ptrAni;
ptrAni = &animal;
ptrAni->display();
CHerbivorous herb("小羊");
ptrAni = &herb;
ptrAni->display();
((CHerbivorous *)ptrAni)->display();
CCarnivore carn("老虎");
ptrAni = &carn;
ptrAni->display();
((CCarnivore *)ptrAni)->display();
return 0;
}
/*
* 虚函数
* Lzy 2011-七夕
*/
#include
using namespace std;
static int Line =1;
class CAnimal
{
protected:
char *name;
public:
CAnimal(char *na){
name = new char[strlen(na)+1];
strcpy(name, na);
}
virtual void display(){cout<
};
class CHerbivorous:public CAnimal
{
public:
CHerbivorous(char *na):CAnimal(na){}
void display(){cout<
};
class CCarnivore:public CAnimal
{
public:
CCarnivore(char *na):CAnimal(na){}
void display(){cout<
};
int main(void)
{
CAnimal animal(""), *ptrAni;
ptrAni = &animal;
ptrAni->display();
CHerbivorous herb("小羊");
ptrAni = &herb;
ptrAni->display();
((CHerbivorous *)ptrAni)->display();
CCarnivore carn("老虎");
ptrAni = &carn;
ptrAni->display();
((CCarnivore *)ptrAni)->display();
return 0;
}
/*
* 虚函数的重载
* 重载一个虚函数要求函数名、返回类型、参数个数参数类型以及参数的顺序都与基类中的原型相同,不能有任何的不同
* Lzy 2011-七夕
*/
#include
#include
using namespace std;
class Person {
protected:
char *name;
public:
Person(char *n) {
name = new char[strlen(n) + 1];strcpy(name,n);}
~Person() {delete[] name;}
virtual void printInfo() {cout<<"姓名:"<<name<<endl;}
};
class Worker: public Person {
public:
Worker(char *n) :
Person(n) {
}
void printInfo() {
cout << "Worker 姓名:" << name << endl;
}
};
class Teacher: public Person {
public:
Teacher(char *n) :Person(n) {}
void printInfo() {
cout << "Teacher 姓名:" << name << endl;
}
};
int main(void) {
Worker w("小明");
Teacher t("Lzy");
Person *p;
p = &w;
p->printInfo();
p = &t;
p->printInfo();
return 0;
}
/*
* 多继承中的虚函数
* Lzy 2011-七夕
*/
#include
#include
using namespace std;
class base1
{
public:
virtual void display(){cout<<"base1"<<endl;}
};
class base2
{
public:
void display(){cout<<"base2"<<endl;}
};
class dervie:public base1, public base2
{
public:
void display(){cout<<"dervie"<<endl;}
};
int main(void)
{
base1 obj1, *ptr1;
base2 obj2, *ptr2;
dervie obj3;
ptr1 = &obj1;
ptr1->display();
ptr1 = &obj3;
ptr1->display(); //动态联编
ptr2 = &obj2;
ptr2->display();
ptr2 = &obj3;
ptr2->display(); //静态联编
((dervie *)ptr2)->display();
return 0;
}
/*
* 多继承中的虚特性传递
* Lzy 2011-七夕
*/
#include
#include
using namespace std;
class A {
public:
virtual void display() {
cout << "A" << endl;
}
};
class base1: virtual public A {
public:
void display() {
cout << "base1" << endl;
}
};
class base2: virtual public A {
public:
void display() {
cout << "base2" << endl;
}
};
class dervie: public base1, public base2 {
public:
void display() {
cout << "dervie" << endl;
}
};
int main(void) {
A obj, *ptr;
base1 obj1, *ptr1;
base2 obj2, *ptr2;
dervie obj3;
ptr = &obj; ptr->display();
ptr1 = &obj1; ptr1->display();
ptr = &obj3; ptr->display();
ptr1 = &obj3; ptr1->display();
ptr2 = &obj3; ptr2->display();
return 0;
}
/*
* 虚析构函数
* 基类的析构函数说明为虚析构函数,则它的派生类中的析构函数也是虚析构函数,不管它是否使用了关键字virtual进行说明
* Lzy 2011-七夕
*/
#include
#include
using namespace std;
class Person {
protected:
char *name;
public:
Person(char *n) {name = new char[strlen(n) + 1];strcpy(name,n);}
virtual ~Person() {delete[] name;cout<<"释放Person子对象"<<endl;}
virtual void printInfo() {cout<<"姓名:"<<name<<endl;}
};
class Student: public Person {
private:
char *special;
public:
Student(char *n, char *s) :Person(n) {special = new char[strlen(s) + 1];strcpy(special,s); }
~Student() {delete[] special;cout<<"释放Student子对象"<<endl;}
void printInfo() {
Person::printInfo();
cout << "专业:" << special << endl;
}
};
int main(void) {
Person *ptr1 = new Person("李芸");
Person *ptr2 = new Student("张雨","嵌入式");
Student *ptr3 =new Student("崔健","计算机");
ptr1->printInfo(); delete ptr1;
ptr2->printInfo(); delete ptr2;
ptr3->printInfo(); delete ptr3;
return 0;
}
/*
* 静态多态性是由函数重载、运算符重载来实现。
* 动态多态性是通过虚函数来实现的
*/