多态与虚函数

1、在非构造函数,非析构函数的成员函数中调用虚函数,是多态。程序示例:

#include <iostream>
using namespace std; 
class Base
{
public:
void fun1()
{
this->fun2();
}  
virtual void fun2()
{
cout << "Base::fun2()";
} 
};
class Derived :public Base
{
public:
virtual void fun2()
{
cout << "Derived:fun2()" << endl;
}
}; 
int main()
{
Derived d;
Base * pBase = &d;
pBase->fun1();
return 0;
}

2、在构造函数和析构函数中调用虚函数,不是多态。编译时即可确定调用的函数是自己的类或者基类中定义的函数,不会等到运行时才确定调用的是自己的还是派生类的函数。原因:当一个派生类的对象,当被执行时,首先执行的是基类的构造函数,如果是多态的话,派生类的构造函数还未初始化,就会引发错误。

#include<iostream>
using namespace std;
class myclass
{
public:
virtual void hello()
{ cout << "hello from myclass" << endl;
}
virtual void bye()
{ cout << "bye from myclass" << endl;  
}
};
class son :public myclass//派生类和基类中虚函数同名同参数表的函数,不加virtual也自动生成为虚函数
{
public:
void hello() { cout << "hello from son" << endl; 
}
son() { hello(); };
~son() { bye(); };
};
class grandson :public son
{
public:
void hello(){ cout << "hello from grandson" << endl; }
void bye()   { cout << "bye from grandson" << endl; }
grandson()  { cout << "constructing grandson" << endl; };
~grandson(){ cout << "destructing grandson" << endl; };
};
int main()
{
grandson gson;
son *pson;
pson = &gson;
pson->hello();//多态
return 0;
}

3、多态使用例子

#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
class CShape
{
public:
virtual double Area() = 0;//纯虚函数
virtual void PrintInfo() = 0;
};
class CRectangle :public CShape
{
public:
int w, h;
virtual double Area();
virtual void PrintInfo();
};
class CCircle :public CShape
{
public:
int r;
virtual double Area();
virtual void PrintInfo();
};
class CTriangle :public CShape
{
public:
int a,b,c;
virtual double Area();
virtual void PrintInfo();
};
//***********************CRectangle
double CRectangle::Area()
{
return w* h;
}
void CRectangle::PrintInfo()
{
cout << "Rectangle:" << Area() << endl;
}
//*************************CCircle
double CCircle::Area()
{
return 3.14 * r * r;
}
void CCircle::PrintInfo()
{
cout << "Circle:" << Area() << endl;
}
//************************CTriangle
double CTriangle::Area()
{
double p = (a + b + c) / 2;
return sqrt(p * (p - a) * (p - b) * (p - c));
}
void CTriangle::PrintInfo()
{
cout << "Triangle:" << Area() << endl;
}
CShape * pShapes[100];
int MyCompare(const void *s1, const void * s2) 
{ 
double a1, a2;
CShape * * p1;
CShape * * p2;
p1 = (CShape**)s1;
p2 = (CShape**)s2;
a1 = (*p1)->Area();
a2 = (*p2)->Area();
if (a1 < a2)
return -1;
else if (a2 < a1)
return 1;
else
return 0;
}
int main()
{
int i, n;
cin >> n;
CRectangle *pr; CCircle *pc; CTriangle *pt;
for (i = 0; i < n; i++)
{
char c;
cin >> c;
switch (c)
{
case 'R':
pr = new CRectangle();
cin >> pr->w >> pr->h;
pShapes[i] = pr;
break;
case 'C':
pc = new CCircle();
cin >> pc->r;
pShapes[i] = pc;
break;
case 'T':
pt = new CTriangle();
cin >> pt->a >> pt->b >>pt->c;
pShapes[i] = pt;
break; 
}
}
qsort(pShapes, n, sizeof(CShape*), MyCompare);
for (i = 0; i < n; i++)
pShapes[i]->PrintInfo();
return 0; 
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值