C++面向对象 class seven

  • 多态
#include<iostream>//多态要素,一个父类有多个子类,父类子类间有同样的函数,这个同样的函数是虚函数,通过父类的指针或引用实现 
using namespace std;//多态:一个信号根据运行主体的不用实现不同的效果 
class Shape{//父类指针可以指向子类 ,子类可以给父类,反之不行 
public:
	virtual double getArea(){//虚函数 
	    cout<<"Shape:"<<endl;
		return -1;
	}//根节点的函数定义为虚函数 
	virtual double example() = 0;//纯虚函数 
};//有纯虚函数是抽象类,不能实体化(定义对象) 
class Circle:public Shape{//如果子类没有把父类的所有纯虚函数重写,那么子类也是抽象类 
public:
	Circle(double _r){
		_case = 1;
		r = _r;
	}
    double getArea(){
    	cout<<"Circle:"<<endl;
    	return r*r*3.14;
	}
private:
	double r;
	int _case;
};
class Square:public Shape{
public:
	Square(double _len){
	    _case = 2;
	    len = _len;
    }
    double getArea(){
    	cout<<"Square:"<<endl;
    	return len*len;
	} 
private:
	double len;
	int _case;
};
void printArea(Shape *shape){
	cout<<shape->getArea()<<endl;
}
void printArea(Shape &shape){
	cout<<shape.getArea()<<endl;
}
int main()
{
	Circle cir(2);
	Square squ(4);
	printArea(&cir);
	printArea(cir);
	printArea(&squ);
	printArea(squ);
	return 0;
}
#include <iostream>
using namespace std;
class Animal{
public :
    Animal(int _v = 0,string s = " "){
        speed = _v;
        color = s;
    }
    virtual void eat(){
    	cout<<"Eat nothing"<<endl;
	}
	virtual void _move(){
		cout<<"Move nothing"<<endl;;
	}
	virtual void sound(){
		cout<<"Say nothing"<<endl;
	}
	virtual void show(){
		cout<<"Show nothing"<<endl;
	}
	virtual ~Animal(){
		cout<<"Del animal"<<endl;
	}
protected:
    int speed;
    string color;
};
class Cat:public Animal{
public:
    Cat(int v,string s):Animal(v,s){}
    Cat():Animal(){}
    ~Cat(){
    	cout<<"Del cat"<<endl;
	}    
    void sett(int v,string s){
        speed = v;
        color = s;
    }
    void show(){cout<<"I am a "<<color<<" cat."<<endl;}
    void eat(){cout<<"I eat fishes"<<endl;}
    void _move(){cout<<"I move at speed of"<<speed<<endl;}
    void sound(){cout<<"miaomiao!"<<endl;}
};
class Dog:public Animal{
public:
    Dog(int v,string s):Animal(v,s){}
    Dog():Animal(){}
    ~Dog(){
    	cout<<"Del Dog"<<endl;
	}    
    void sett(int v,string s){
        speed = v;
        color = s;
    }
    void show(){cout<<"I am a "<<color<<" dog."<<endl;}
    void eat(){cout<<"I eat boons"<<endl;}
    void _move(){cout<<"I move at speed of"<<speed<<endl;}
    void sound(){cout<<"wangwang!"<<endl;}
};
class Bird:public Animal{
public:
    Bird(int v,string s):Animal(v,s){}
    Bird():Animal(){}
    ~Bird(){
    	cout<<"Del bird"<<endl;
	}
    void sett(int v,string s){
        speed = v;
        color = s;
    }
    void show(){cout<<"I am a "<<color<<" bird."<<endl;}
    void eat(){cout<<"I eat rice"<<endl;}
    void _move(){cout<<"I fly at speed of"<<speed<<endl;}
    void sound(){cout<<"jiji!"<<endl;}
};
class Zoo{
public:
    Zoo(int numbird,int numcat,int numdog){
        numOfBird = numbird;
        numOfCat = numcat;
        numOfDog = numdog;
        animals = new Animal*[3];
        animals[0] = new Cat;
        animals[1] = new Dog;
        animals[2] = new Bird;
        cout<<"Now we have "<<numOfCat<<" cats and "<<numOfDog<<" dogs and "<<numOfBird<<" birds."<<endl;
    }
    ~Zoo(){
    	for(int i=0;i<3;i++)
    	delete animals[i];
    	delete animals;
	}
    void showNum(){
        cout<<"Now we have "<<numOfCat<<" cats and "<<numOfDog<<" dogs and "<<numOfBird<<" birds."<<endl;
    }
    void setcat(int num,int v,string s){
        cats[num].sett(v,s);
    }
    void setdog(int num,int v,string s){
        dogs[num].sett(v,s);
    }
    void setbird(int num,int v,string s){
        birds[num].sett(v,s);
    }
    void addcat(int v,string s){
        numOfCat++;
        cats[numOfCat].sett(v,s);
    }
    void adddog(int v,string s){
        numOfCat++;
        dogs[numOfDog].sett(v,s);
    }
    void addbird(int v,string s){
        numOfCat++;
        birds[numOfBird].sett(v,s);
    }
    void show(){
        for(int i=1;i<=numOfCat;i++)
        {
            cats[i].show();
            cats[i].sound();
        }
        for(int i=1;i<=numOfDog;i++)
        {
            dogs[i].show();
            dogs[i].sound();
        }
        for(int i=1;i<=numOfBird;i++)
        {
            birds[i].show();
            birds[i].sound();
        }
    }
    void _express(Animal *ani){
//    	ani->show();
    	ani->eat();
	}
    void kaiFan(){
        for(int i=1;i<=numOfCat;i++)
        _express(animals[0]);
        for(int i=1;i<=numOfDog;i++)
        _express(animals[1]);
        for(int i=1;i<=numOfBird;i++)
        _express(animals[2]);
    }
    void cutCat(int n){
        numOfCat-=n;
        cout<<"It is a pity that we lose "<<n<<" cats"<<endl;
    }
    void cutDog(int n){
        numOfDog-=n;
        cout<<"It is a pity that we lose "<<n<<" dogs"<<endl;
    }
    void cutBird(int n){
        numOfBird-=n;
        cout<<"It is a pity that we lose "<<n<<" birds"<<endl;
    }
private:
	Animal **animals;
    Cat cats[505];
    Dog dogs[505];
    Bird birds[505];
    static int numOfBird,numOfCat,numOfDog;
};
int Zoo::numOfCat = 0;
int Zoo::numOfDog = 0;
int Zoo::numOfBird = 0;
int main()
{
    int numcat,numdog,numbird;
    string s;int v;
    cin>>numcat>>numdog>>numbird;
    Zoo zoo(numcat,numdog,numbird);
//    for(int i = 1;i<=numcat;i++)
//    {
//        cin>>v>>s;
//        zoo.setcat(i,v,s);
//    }
//    for(int i = 1;i<=numdog;i++)
//    {
//        cin>>v>>s;
//        zoo.setdog(i,v,s);
//    }
//    for(int i = 1;i<=numbird;i++)
//    {
//        cin>>v>>s;
//        zoo.setbird(i,v,s);
//    }
//    zoo.show();
//    zoo.kaiFan();
//    zoo.addbird(20,"black");
//    zoo.show();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈希表扁豆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值