- 多继承,菱形继承,由于似菱形继承产生的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;
}