多重继承
虽然多重继承与单一继承相比有很多优点,但是由于虚基类的定义很复杂,因此很多程序员都不愿意使用它。他们认为很多编译器不支持多重继承,代码容易产生多义性,调试起来很困难,而且很多时候不实用多重继承同样可以做到。
我们要注意在用单一继承就可以实现的情况下,不要使用多重继承。
#include <iostream>
using namespace std;
class father
{
public:
father(int height);
virtual ~father(){cout<<"析构父亲...\n";}
virtual void smart()const{cout<<"父亲很聪明"<<endl;}
virtual int getheight()const{return itsheight;}
private:
int itsheight;
};
father::father(int height):itsheight(height)
{
cout<<"创建父亲\n";
}
class mother
{
public:
mother(bool sex);
virtual ~mother(){cout<<"析构母亲\n";}
virtual void beautifual()const{cout<<"母亲很漂亮\n";}
virtual bool getsex()const{return itsex;}
private:
bool itsex;
};
mother::mother(bool sex):itsex(sex)
{
cout<<"创建母亲\n";
}
class son:public father,public mother
{
public:
~son(){cout<<"析构小儿子..."<<endl;}
son(int,bool,long);
virtual long getnum()const
{
return num;
}
private:
long num;
};
son::son(int height,bool sex,long number):father(height),mother(sex),num(number)
{
cout<<"创建小儿子\n";
}
int main()
{
son*ps=new son(5,true,3);
ps->smart();
cout<<"\n小儿子有"<<ps->getheight();
cout<<"英尺高\n";
if(ps->getsex())
{
cout<<"性别:男";
}
else
{
cout<<"性别:女";
}
cout<<"\n在家排行:第"<<ps->getnum()<<endl;
delete ps;
system("pause");
return 0;
}
空的虚函数:派生类继承基类的虚函数不一定要实现特定的功能,有的时候只是为了让他的派生类能够使用该虚函数
#include <iostream>
using namespace std;
class human
{
public:
human(){cout<<"构造人类\n";}
virtual ~human(){cout<<"析构人类\n";}
virtual void print(){cout<<"我是人类"<<endl;}
};
class father:public human
{
public:
father(){cout<<"构造父类\n";}
virtual ~father(){cout<<"析构父类"<<endl;}
};
class son:public father
{
public:
son(){cout<<"构造子类\n";}
virtual ~son(){cout<<"析构子类";}
virtual void print(){cout<<"我是儿子"<<endl;}
};
void show(human*a)
{
a->print();
}
int main()
{
human*h=0;
int choice=0;
while(choice<4)
{
bool quit=false;
cout<<"(0)退出(1)人类(2)父类(3)子类";
cin>>choice;
switch(choice)
{
case 0:quit=true;
break;
case 1:h=new human;
break;
case 2:h=new father;
break;
default:h=new son;
break;
}
if (quit)
{
break;
}
show(h);
delete h;
}
return 0;
}
纯虚函数和抽象类
不可以定义抽象类对象 shape a= new shape是错误的
#include <iostream>
using namespace std;
class shape //抽象类shape
{
public:
virtual double area()=0; //纯虚函数该函数没有任何功能,他只是给他的子类实现多态性的一个函数接口
};
class A:public shape
{
protected:
double h,w; //保护成员 它的子类可以访问
public:
A(double H,double W){h=H;w=W;}
double area(){return h*w/2;} //继承的虚函数还是虚函数不用加virtual关键字
};
class B:public A
{
public:
B(double H,double W):A(W,H){}
double area(){return h*w;} B继承并覆盖了A的area函数
};
class C:public shape
{
protected:
double radius;
public:
C(double r){radius=r;}
double area(){return radius*radius*3.14;}
};
int main()
{
shape *s;
int choice=0;
while(choice<9)
{
choice=0;
bool quit=false;
cout<<"(0)退出(1)三角形(2)长方形(3)圆"<<endl;
cout<<"请选择:";
cin>>choice;
switch(choice)
{
case 0:quit = true;
break;
case 1:s=new A(5.0,6.0);
cout<<"三角形的面积为:"<<s->area()<<endl;
break;
case 2:s=new B(7.0,8.0);
cout<<"正方形的面积为:"<<s->area()<<endl;
break;
case 3:s=new C(9.0);
cout<<"圆的面积为:"<<s->area()<<endl;
break;
default:cout<<"请输入0-3之间的数字";
break;
}
if(choice<4&&choice>0)
delete s;
if(quit)
break;
}
return 0;
}