C++面向对象 class six

  • 多继承,菱形继承,由于似菱形继承产生的ambiguous,虚继承在这里插入代码片
#include <iostream>
#include <cstring>
using namespace std;
class Base1{
public:
//    void fun(){
//        cout<<"This is Base1."<<endl;
//    }
    void fun(int a){//不能通过重载(严格与Base2不构成重载)避免ambiguous
        cout<<"This is Base1."<<endl;
    }
protected:
    int common;
    int base1;
};
class Base2{
public:
    void fun(){
        cout<<"This is Base2."<<endl;
    }
protected:
    int common;
    int base2;
};
class Derived:public Base1,public Base2{
public:
    void setBase1(int b1){base1 = b1;}
    void setBase2(int b2){base2 = b2;}
    void setcommen(int c){Base1::common = Base2::common = c;}
    void show(){cout<<base1<<' '<<base2<<endl;}
//    void fun(){cout<<"This is derived."<<endl;}
protected:
    int derived;
};
int main()
{
    Derived derived;
    derived.fun();
    derived.setBase1(10);
    derived.setBase2(20);
    derived.show();
    return 0;
}
#include <iostream>//菱形继承
#include <cstring>
using namespace std;
class Base{
public:
    Base(){cout<<"base"<<endl;}
    void fun(){cout<<"this is base."<<endl;}
protected:
    int a;
};
class Base1:public Base{//虚继承,子类只有一个祖父类的备份,在创建子类对象时此构造函数只会被调用一次
public:
    Base1(){cout<<"base1"<<endl;}
protected:
    int b;
};
class Base2:public Base{
public:
    Base2(){cout<<"base2"<<endl;}
protected:
    int c;
};
class Derived:public Base1,public Base2{
public:
    Derived(){cout<<"dirived"<<endl;}
    void setA(int _a){Base1::a = _a; Base2::a = _a+1;}
    void show(){cout<<Base1::a<<' '<<Base2::a<<endl;}
protected:
    int derived;
};
int main()
{
    Derived derived;
//    derived.fun();
//    derived.setA(100);
//    derived.show();
    return 0;
}

改成指针,额外写给更新内存的函数更好。

#include <iostream>
using namespace std;
class Animal{
public :
    Animal(int _v = 0,string s = " "){
        speed = _v;
        color = s;
    }
protected:
    int speed;
    string color;
};
class Cat:public Animal{
public:
    Cat(int v,string s):Animal(v,s){}
    Cat():Animal(){}
    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(){}
    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(){}
    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;
        cout<<"Now we have "<<numOfCat<<" cats and "<<numOfDog<<" dogs and "<<numOfBird<<" birds."<<endl;
    }
    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 kaiFan(){
        for(int i=1;i<=numOfCat;i++)
        {
            cats[i].show();
            cats[i].eat();
        }
        for(int i=1;i<=numOfDog;i++)
        {
            dogs[i].show();
            dogs[i].eat();
        }
        for(int i=1;i<=numOfBird;i++)
        {
            birds[i].show();
            birds[i].eat();
        }
    }
    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:
    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、付费专栏及课程。

余额充值