C++-12-虚函数与抽象类

虚函数

//虚函数
//                         联编时间    工程案例
//静态联编(早期联编)     编译        函数重载,运算符重载
//动态联编(滞后联编)     运行        虚函数

#include <iostream>
using namespace std;
const double PI = 3.1415926;
class cpoint
{
public:
    cpoint(double a, double b)
    {
        x = a;
        y = b;
    }
    double area()
    {
        return 0;
    }
private:
    double x, y;
};
class ccircle :public cpoint //圆形类继承cpoint
{
public:
    ccircle(double a, double b, double c) :cpoint(a, b)
    {
        r = c;
    }
    double area()
    {
        return PI * r * r;
    }
private:
    double r;
};
class cractangle :public cpoint //矩形类继承cpoint
{
public:
    cractangle(double a, double b, double c, double d) :cpoint(a, b)
    {
        w = c;
        h = d;
    }
private:
    double w, h;
};
void funcarea(cpoint& p)
{
    cout << "area=" << p.area() << endl;
}
int main()
{
    ccircle cobj(5.5, 6.6, 10);
    funcarea(cobj);//输出area=0

    cractangle cobjc(3, 4, 5, 6);
    funcarea(cobjc);//输出area=0
    return 0;
}
//上诉输出结果原因:
//p是cpoint类的一个对象,由于没有虚函数,执行是静态联编(在编译时已经确定)选择的是cpoint中的area()函数
//类中定义不同类中的同名函数的多态的行为,主要是通过虚函数来实现的
//多态条件
//1.基类必须包含虚函数
//2.派生类中一定要对基类中的虚函数进行重写
//3.通过基类对象的指针或引用调用虚函数
/**************************************
设计一个动物类,及派生出的牛类,狗类,三者都具有叫声callfunc()成员函数,该函数在基类中被声明为虚函数
**************************************/

#include <iostream>
#include <string>
using namespace std;
class canimal
{
public:
    virtual void callfunc();//虚函数
private:
    string name;
};
class ccattle :public canimal//派生牛类
{
public:
    virtual void callfunc();
private:
    int age;
};
class cdog :public canimal//派生狗类
{
public:
    virtual void callfunc();
private:
    int age;
};
void callfunc(canimal* p)//通过传递不同的对象实参,可以调用产生对应的成员函数,以实现多态功能
{
    p->callfunc();
}
void canimal::callfunc()
{
    cout << "动物此类不知道怎么叫...." << endl;
}
void ccattle::callfunc()
{
    cout << "哞哞叫" << endl;
}
void cdog::callfunc()
{
    cout << "汪汪叫" << endl;
}
int main()
{
    ccattle cobj;
    callfunc(&cobj);
    cdog dobj;
    callfunc(&dobj);
    canimal aobj;
    callfunc(&aobj);
    return 0;
}

抽象类

//抽象类:带有纯虚函数的类
//纯虚函数是一个在基类中声明的虚函数,它在该基类中没有定义具体的操作内容
//要求各派生类根据实际需要给出各自的定义。

#include <iostream>
#include <string>
using namespace std;
class canimal//抽象类
{
public:
    virtual void callfunc()=0;//纯虚函数
private:
    string name;
};
class ccattle :public canimal//派生牛类
{
public:
    virtual void callfunc();
private:
    int age;
};
class cdog :public canimal//派生狗类
{
public:
    virtual void callfunc();
private:
    int age;
};
//通过指针
void callfunc(canimal* p)//通过传递不同的对象实参,可以调用产生对应的成员函数,以实现多态功能
{
    p->callfunc();
}
//也可以通过引用方式进行
//void callfunc(canimal& sp)
//{
//    sp.callfunc();
//}
void ccattle::callfunc()
{
    cout << "哞哞叫" << endl;
}
void cdog::callfunc()
{
    cout << "汪汪叫" << endl;
}
int main()
{
    ccattle cobj;
    callfunc(&cobj);
    cdog dobj;
    callfunc(&dobj);
   //引用方式
    /*ccattle cobj;
    callfunc(cobj);
    cdog dobj;
    callfunc(dobj);*/
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值