C++虚函数与多态性(2)

虚函数练习:

1.定义一个类Animal,该类中包含数据成员name用来记录动
物的名字,并有一个虚函数show用于显示动物的种类。 2.定义两个类Cat和Dog,都继承自Animal;包含show函数,
不但要显示动物的种类,还要显示动物的名字。 3.定义一个Tiger类,继承自Cat,包含show函数,显示动
物的种类和名字。
编写主函数,在主函数中定义一个基类指针,并用这个指针
指向派生类的对象,通过基类指针调用派生类的show函数,
实现运行时的多态。

#include <iostream>
#include <string>
using namespace std;
class Animal{
    private:
    string name;
    public:
    Animal(string name){this->name=name;}
    virtual void show(){}
    void show_name(){cout<<" The name is "<<name<<".";}
    virtual ~Animal(){cout<<"delete Animal."<<endl;};
    };
class Cat:public Animal{
    private:
    string kind;
    public:
    Cat(string name,string kind):Animal(name){this->kind=kind;}
    void show(){show_name();cout<<"cat show It's a "<<kind<<endl;}
    ~Cat(){cout<<"delete Cat."<<endl;};
    };
class Dog:public Animal{
    private:
    string kind;
    public:
    Dog(string name,string kind):Animal(name){this->kind=kind;}
    void show(){show_name();cout<<"dog show It's a "<<kind<<endl;}
    ~Dog(){cout<<"delete Dog."<<endl;};
    };
class Tiger:public Cat{
    public:
    Tiger(string name,string kind):Cat(name,kind){}
    ~Tiger(){cout<<"delete Tiger."<<endl;};
    };
int main()
{
    Animal *p;
    Cat cat("Tom","Cat");
    Dog dog("Jerry","Dog");
    Tiger tiger("DUDU","Tiger");
    p=&cat; p->show();
    p=&dog; p->show();
    p=&tiger; p->show();
    return 0;
}

纯虚函数和抽象类

纯虚函数是一种特殊的虚函数,在许多情况下,在基类中不能对虚函数给出有意义的实现,而把它声明为纯虚函数,它的实现留给该基类的派生类去做。
纯虚函数是一个在基类中说明的虚函数,在基类中没有定义,要求任何派生类都要定义自己的版本。
纯虚函数为各派生类提供了一个公共的界面。
纯虚函数说明形式:

virtual 类型 函数名 (参数表)=0

一个具有纯虚数函数的基类称为抽象类

例简单图形类
问题:为什么在基类的构造函数有两个参数,而circle类在主函数中只有一个参数,不会报错?

#include <iostream>
using namespace std;
class figure{
    protected:
    double x,y;
    public:
    void set_dim(double x,double y=0){this->x=x;this->y=y;}//?
    virtual void show_area()=0; //纯虚函数
    };
class triangle:public figure{
    public:
    void show_area(){cout<<"Triangle with high "<<x<<" and base "<<y<<" has an area of "<<x*0.5*y<<endl;}
    };
class square:public figure{
    public:
    void show_area(){cout<<"Square with dimension "<<x<<"*"<<y<<" has an area of "<<x*y<<endl;}
    };
class circle:public figure{
    public:
    void show_area(){
        cout<<"Circle with radius "<<x;
        cout<<" has an area of "<<3.14*x*x<<endl;}
        };
int main()
{
    triangle t;
    square s;
    circle c;
    t.set_dim(10.0,5.0);
    t.show_area();
    s.set_dim(10.0,5.0);
    s.show_area();
    c.set_dim(9.0);//???
    c.show_area();
    return 0;
}

在这里插入图片描述

虚函数与多态的应用

虚函数和多态性使成员函数根据调用对象的类型产生不同的动作,多态性特别适合于实现分层结构的软件系统,便于对问题抽象时定义共性,实现时定义区别。
例:Employee->Manager、Hourworker、Priceworker

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
class Employee{
    public:
    Employee(const long k,const char* str){number=k;strcpy(name,str);}
    virtual ~Employee(){name[0]='\0';}//虚析构函数
    const char* getname() const {return name;}
    const long getnumber() const {return number;}
    virtual double earnings() const =0;//纯虚函数
    virtual void print() const {cout<<number<<setw(20)<<name;}
    protected:
    long number;
    char name[20];
    };
class Manager:public Employee{
    public:
    Manager(const long k,const char* str,double salary=0.0):Employee(k,str){setmonthlysalary(salary);}
    ~Manager(){}//析构函数
    void setmonthlysalary(double salary){monthlysalary=salary;}
    virtual double earnings() const{return monthlysalary;}
    virtual void print() const{
        Employee::print();
        cout<<setw(16)<<"Manager\n";
        cout<<" earned$"<<earnings()<<endl;}
    private:
    double monthlysalary;
    };
class HourWorker:public Employee{
    public:
    HourWorker(const long k,char* str,double w=0.0,int h=0):Employee(k,str){
        setwage(w);sethours(h);}
    ~HourWorker(){}//析构函数
    void setwage(double w){wage=w;}
    void sethours(int h){hours=h;}
    virtual double earnings() const{return wage*hours;}
    virtual void print() const{
        Employee::print();
        cout<<setw(16)<<"HourWorker\n"<<"\t\twageperhour "<<wage<<" Hour "<<hours;
        cout<<" earned$"<<earnings()<<endl;}
    private:
    double wage;
    double hours;
    };
class PriceWorker:public Employee{
    public:
    PriceWorker(const long k,char* str,double wage=0.0,int quantity=0):Employee(k,str){
        setwage(wage);setquantity(quantity);}
    ~PriceWorker(){}//析构函数
    void setwage(double w){wageperprice=w;}
    void setquantity(int q){quantity=q;}
    virtual double earnings() const{return wageperprice*quantity;}
    virtual void print() const{
        Employee::print();
        cout<<setw(16)<<"Price Worker\n";
        cout<<"\t\twageperprice "<<wageperprice<<" quantity "<<quantity;
        cout<<" earned$"<<earnings()<<endl;}
    private:
    double wageperprice;
    int quantity;
    };

void test1()
{
    cout<<setiosflags(ios::fixed|ios::showpoint)<<setprecision(2);
    Manager m1(10135,"Cheng ShaoHua",1200);
    Manager m2(10201,"Yan HaiFeng");
    m2.setmonthlysalary(5300);
    HourWorker h1(30712,"Zhao XiaoMing",5,8*20);
    HourWorker h2(30649,"Gao DongSheng");
    h2.setwage(4.5);
    h2.sethours(10*30);
    PriceWorker p1(20382,"Xiu LiWei",0.5,2850);
    PriceWorker p2(20946,"Huang DongLin");
    p2.setwage(0.75);
    p2.setquantity(1850);
    Employee *basePtr;
    basePtr=&m1; basePtr->print();
    basePtr=&m2; basePtr->print();
    basePtr=&h1; basePtr->print();
    basePtr=&h2; basePtr->print();
    basePtr=&p1; basePtr->print();
    basePtr=&p2; basePtr->print();
    }
int main()
{
    test1();
    return 0;
}

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值