C++ 第五次实验

实验5 多态性与虚函数

实验步骤:
第一步:

#include<iostream>
#include<cstdlib>
using namespace std;
class Rational
{
private:
    //定义一个有理数num为分母,den为分子
    long num,den;
public:
    //构造函数
    Rational(int num=0,int denom=1);
    //重载比较操作符
    int operator<(Rational r) const;
    int operator<=(Rational r) const;
    int operator==(Rational r) const;
    int operator!=(Rational r) const;
};
Rational::Rational(int p,int q):num(p),den(q)
{
    if(den==0)
    {
        cout<<"A Zero denominator is invalid"<<endl;
        exit(1);
    }
}
//重载“小于”
int Rational::operator<(Rational r) const
{
    return num*r.den<r.num*den;
}
//重载“小于等于”
int Rational::operator<=(Rational r) const
{
    return num*r.den<=r.num*den;
}
//重载“相等”
int Rational::operator==(Rational r) const
{
    return num*r.den==r.num*den;
}
//重载“不相等”
int Rational::operator!=(Rational r) const
{
    return num*r.den!=r.num*den;
}
int main()
{
    Rational a(2,3),b(3,4);
    if(a<b) cout<<"\na<b";
    else cout<<"\na>b";
    return 0;
}

第二步:Animal类的调试。

#include<iostream>
#include<cstring>
using namespace std;
class Animal{
protected:
    char *name;
public:
    Animal()
    {
        name=NULL;
    }
    Animal(char *n);
    ~Animal() {delete name;}
    virtual void Identify();
};
Animal::Animal(char *n)
{
    name=new char[strlen(n)+1];
    strcpy(name,n);
}
void Animal::Identify()
{
    cout<<"\ngeneric animal";
}
int main()
{
    Animal a(" ");
    a.Identify();
    return 0;
}

第三步:Cat和Dog类的调试。

#include<iostream>
#include<cstring>
using namespace std;
class Animal{
protected:
    char *name;
public:
    Animal()
    {
        name=NULL;
    }
    Animal(char *n);
    ~Animal() {delete name;}
    virtual void Identify();
};
class Cat:public Animal{
public:
    Cat():Animal(){};
    Cat(char *n):Animal(n){};
    void Identify();
};
class Dog:public Animal{
public:
    Dog():Animal(){};
    Dog(char *n):Animal(n){};
    void Identify();
};
Animal::Animal(char *n)
{
    name=new char[strlen(n)+1];
    strcpy(name,n);
}
void Animal::Identify()
{
    cout<<"\ngeneric aninal";
}
void Cat::Identify()
{
    cout<<"\nI am a cat named "<<name<<endl;
}
void Dog::Identify()
{
    cout<<"\nI am a dog named "<<name<<endl;
}
int main()
{
    Cat cat("Herry");
    cat.Identify();
    Dog dog("Jimmy");
    dog.Identify();
    return 0;
}

第四步:Tiger类的调试,Tiger类继承了Cat类。

#include<iostream>
#include<cstring>
using namespace std;
class Animal{
protected:
    char *name;
public:
    Animal()
    {
        name=NULL;
    }
    Animal(char *n);
    ~Animal() {delete name;}
    virtual void Identify();
};
class Cat:public Animal{
public:
    Cat():Animal(){};
    Cat(char *n):Animal(n){};
    void Identify();
};
class Tiger:public Cat{
public:
    Tiger():Cat(){};
    Tiger(char *n):Cat(n){};
    void Identify();
};
class Dog:public Animal{
public:
    Dog():Animal(){};
    Dog(char *n):Animal(n){};
    void Identify();
};
Animal::Animal(char *n)
{
    name=new char[strlen(n)+1];
    strcpy(name,n);
}
void Animal::Identify()
{
    cout<<"\ngeneric aninal";
}
void Cat::Identify()
{
    cout<<"\nI am a cat named "<<name<<endl;
}
void Tiger::Identify()
{
    cout<<"\nI am a tiger named "<<name<<endl;
}
void Dog::Identify()
{
    cout<<"\nI am a dog named "<<name<<endl;
}
int main()
{
    Cat cat("Herry");
    cat.Identify();
    Dog dog("Jimmy");
    dog.Identify();
    Tiger tiger("Richard");
    tiger.Identify();
    return 0;
}

#include<iostream>
using namespace std;
class Animal{
protected:
    char *name;
public:
    Animal()
    {
        name=NULL;
    }
    Animal(char *n);
    ~Animal() {delete name;}
    virtual void Identify();
};
class Cat:public Animal{
public:
    Cat():Animal(){};
    Cat(char *n):Animal(n){};
    void Identify();
};
class Tiger:public Cat{
public:
    Tiger():Cat(){};
    Tiger(char *n):Cat(n){};
    void Identify();
};
class Dog:public Animal{
public:
    Dog():Animal(){};
    Dog(char *n):Animal(n){};
    void Identify();
};

第五步:增加Zoo类。

#include<iostream>
#include<cstring>
using namespace std;
class Animal{
protected:
    char *name;
public:
    Animal()
    {
        name=NULL;
    }
    Animal(char *n);
    ~Animal() {delete name;}
    virtual void Identify();
};
class Cat:public Animal{
public:
    Cat():Animal(){};
    Cat(char *n):Animal(n){};
    void Identify();
};
class Tiger:public Cat{
public:
    Tiger():Cat(){};
    Tiger(char *n):Cat(n){};
    void Identify();
};
class Dog:public Animal{
public:
    Dog():Animal(){};
    Dog(char *n):Animal(n){};
    void Identify();
};
class Zoo
{
private:
    unsigned int maxanimals;    //动物园最多能圈养的动物数
    unsigned int numanimals;    //动物园当前圈养的动物数
    Animal **residents;          //指向动物园圈养的动物对象的指针数组
public:
    Zoo(unsigned int max);      //构造函数,max为最多能圈养动物数
    ~Zoo(){delete residents;}    //析构函数
    unsigned int Accept(Animal *d);//接收动物
    void ListAnimals();             //显示动物园所有动物
};
Animal::Animal(char *n)
{
    name=new char[strlen(n)+1];
    strcpy(name,n);
}
void Animal::Identify()
{
    cout<<"\ngeneric aninal";
}
void Cat::Identify()
{
    cout<<"\nI am a cat named "<<name<<endl;
}
void Tiger::Identify()
{
    cout<<"\nI am a tiger named "<<name<<endl;
}
void Dog::Identify()
{
    cout<<"\nI am a dog named "<<name<<endl;
}
Zoo::Zoo(unsigned int max)
{
    maxanimals=max;
    numanimals=0;
    residents=new Animal *[maxanimals];     //分配空间
    for(int i=0;i<maxanimals;i++)            // 指针清空
        residents[i]=NULL;
}
unsigned int Zoo::Accept(Animal *d)
{
    if(numanimals==maxanimals)      //超过最大圈养数
        return 0;
    ++numanimals;                   //当前圈养数加1
    int i=0;
    while(residents[i]!=NULL)       //指针指向接收的动物对象
    {
        ++i;
    }
    residents[i]=d;
    return ++i;
}
void Zoo::ListAnimals()
{
    cout<<"\n (: There are ";
    cout<<numanimals<<" animals in Zoo.\n";//显示动物园总的动物数
    if(numanimals>0)
        for(int i=0;i<maxanimals;++i)
            if(residents[i]!=NULL)
                residents[i]->Identify();       //显示动物对象的名称和种类
}
int main()
{
    Cat cat("Herry"),*q;
    cat.Identify();
    Dog dog("Jimmy"),*p;
    dog.Identify();
    Tiger tiger("Richard"),*r;
    tiger.Identify();
    Zoo beiging(20);
    q=&cat;
    p=&dog;
    r=&tiger;
    unsigned i=beiging.Accept(p);
    beiging.ListAnimals();
    i=beiging.Accept(q);
    beiging.ListAnimals();
    i=beiging.Accept(r);
    beiging.ListAnimals();
    return 0;
}

日期:2020年3月25日

发布了86 篇原创文章 · 获赞 97 · 访问量 4814
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览