纯虚函数
1.1纯虚函数是在声明虚函数时被“初始化”为0的函数。声明纯虚函数的一般形式为:
virtual 函数类型 函数名 (参数列表) =0;
如 virtual float area() const =0;
1.2纯虚函数没有函数体,而且这是一个声明语句后面应有分号。
1.3纯虚函数最后面的“=0”并不表示返回值为0,它只是起形式上的作用,告诉编译器这是纯虚函数。
1.4纯虚函数的作用是在基类中为其派生类保留一个函数的名字,以便派生类型根据需要对它进行定义。
1.5如果在一个类中定义了纯虚函数,而在其派生类中没有对该函数定义,则该虚函数在派生类中仍为纯虚函数。
抽象类
1.1什么是抽象类?
一种不用来定义对象而用来作为一种基本类型而作为继承的类,称为抽象类,由于它常作为基类,故又称为抽象基类。
1.2凡是包含纯虚函数的类都是抽象类。因为纯虚函数是无法调用的,包含纯虚函数的类是无法建立对象的。
1.3往往一些好的面向对象的系统,其顶部是一个抽象类,甚至顶部有好几层抽象类。
1.4如果派生类对基类的所有纯虚函数进行了定义,那么这些函数就被赋予了功能,可以被调用,这个派生类就不是抽象类,而是具体的类。
1.5一段代码:
#include <iostream>
using namespace std;
class Shape
{
public:
virtual float area() const{return 0;}
virtual float volume() const{return 0;}
virtual void shapeName() const=0;
};
class Point:public Shape
{
public:
Point(float a=0,float b=0);
void setPoint(float a,float b);
float getX() const {return x;}
float getY() const {return y;}
virtual void shapeName() const {cout << "Points" << endl;}
friend ostream& operator << (ostream& output,Point& p);
protected:
float x,y;
};
Point::Point(float a, float b)
{
x=a;
y=b;
}
void Point::setPoint(float a, float b)
{
x=a;
y=b;
}
ostream& operator << (ostream& output,Point& p)
{
output << "[" << p.getX() << "," << p.getY() << "]" << endl;
return output;
}
class Circle:public Point
{
public:
Circle(float a,float b,float r):Point(a,b),radius(r){}
void setRadius(float r);
float getRadius() const {return radius;}
virtual float area() const ;
virtual void ShapeName() const {cout << "Circle" << endl;}
friend ostream& operator << (ostream& output,Circle &c);
protected:
float radius;
};
void Circle::setRadius(float r)
{
radius=r;
}
float Circle::area() const
{
return 3.14159*radius*radius;
}
ostream& operator << (ostream& output,Circle &c)
{
output << "Center [" << c.x << "," << c.y << "] radius=" << c.radius
<< " area=" << c.area() << endl;
return output;
}
class Cylinder:public Circle
{
public:
Cylinder(float a,float b,float r,float h):Circle(a,b,r),height(h){}
void setHeight(float h){height=h;}
float getHeight() const{return height;}
virtual float area() const;
virtual float volume() const;
virtual void ShapeName() const {cout << "Cylinder" << endl;}
friend ostream& operator << (ostream& output,Cylinder& cy);
protected:
float height;
};
float Cylinder::area() const
{
return 2*Circle::area()+2*3.14159*radius*height;
}
float Cylinder::volume() const
{
return Circle::area()*height;
}
ostream& operator << (ostream& output,Cylinder& cy)
{
output << "Center [" << cy.getX() << "," << cy.getY() << "] r="
<< cy.radius << "\narea=" << cy.area() << " volume=" << cy.volume()
<< endl;
return output;
}
int main()
{
Point p(1,2);
Circle cir(1,2,3);
Cylinder cy(1,2,3,4);
cout <<"Cylinder\n" << "Center [" << cy.getX() << "," << cy.getY() << "] r="
<< cy.getRadius() << "\narea=" << cy.area() << " volume=" << cy.volume()
<< endl;
cy.setHeight(1);
cy.setPoint(2,3);
cy.setRadius(4);
cout << "Cylinder\n" << cy << endl;
Point &px=cy;
cout << "Point\n" << px;
Circle &c=cy;
cout << "Circle\n" << c;
Shape *pt=&p;
pt->shapeName();
cout << "area=" << pt->area() << " volume=" << pt->volume() << endl;
pt=○
pt->shapeName();
cout << "area=" << pt->area() << " volume=" << pt->volume() << endl;
pt=&cy;
pt->shapeName();
cout << "area=" << pt->area() << " volume=" << pt->volume() << endl;
return 0;
}1.6一个基类如果包括一个或一个以上的纯虚函数,就是抽象基类。抽象基类不能也不必要定义对象。
1.7抽象基类与普通基类不同,它一般不是现实存在的类的抽象,它可以没有任何物理意义上或其它实际意义方面上的含义。
1.8在类的层次机构中,顶层或最上面的几层可以是抽象基类。
1.9抽象基类是本类族的公共接口。
2.0区别静态关联和动态关联。
2.1如果在基类声明了虚函数,则在派生类中凡是与该函数有相同的函数名,函数类型,参数个数与参数类型的函数,均为虚函数(不论在派生类中是否用virtual声明)。
2.2使用虚函数提高了函数的可扩充性。