C++纯虚函数与抽象类
2013-10-29 16:21
281 查看
在很多的情况下,在基类中一般都不能给出虚函数的具体而有意义的定义,这时我们就可以将它说明为纯虚函数。它的具体的定义由它的派生类具体完成,这样可以使类之间的结构更加清晰,同时也更容易理解。
含有纯虚函数的类叫抽象类。
说明纯虚函数的一般格式:
class 类名
{
virtual 返回值类型 函数名(参数列表)=0;
}
1在纯函数中,不能提供出函数的具体实现,而是需要在派生类中再加以具体实现。
2,一个类中可以有多个纯虚函数。包含纯虚函数的类被称为抽象类。
3.一个抽象类只能作为基类来派生新类,而不能说明抽象类的对象和对象数组。
抽象类只能定义指针。 不能定义对象和数组。
4.可以说明抽象类对象的指针和引用。
5.从一个抽象派生的类必须提供纯虚函数的实现代码,或在该派生类中仍将它说明为纯虚函数。
6,在抽象类中,至少有一个虚函数是纯虚函数。
#ifndef _______Shape__
#define _______Shape__
#include <iostream>
class Shape
{
private:
int x;
int y;
public:
void setX(int _x,int _y);
int getX();
int getY();
virtual float area()=0;//纯虚函数。
};
#endif /* defined(_______Shape__) */
#include "Shape.h"
void Shape::setX(int _x, int _y)
{
x=_x;
y=_y;
}
int Shape::getX()
{
return x;
}
int Shape::getY()
{
return y;
}
#ifndef _______Rectangle__
#define _______Rectangle__
#include <iostream>
#include "Shape.h"
class Rectangle :public Shape
{
private:
int width;
int height;
public:
void setWH(int _w,int _h);
int getWidth();
int getHeigth();
float area();
};
#endif /* defined(_______Rectangle__) */
#include "Rectangle.h"
void Rectangle::setWH(int _w, int _h)
{
width=_w;
height=_h;
}
int Rectangle:: getWidth()
{
return width;
}
int Rectangle::getHeigth()
{
return height;
}
float Rectangle:: area()
{
return width*height;
}
#include <iostream>
#include "Rectangle.h"
#include "Shape.h"
using namespace std;
int main(int argc, const char * argv[])
{
Shape *p;
Rectangle rect;
rect.setWH(10, 20);
p=▭//加上virtual,基类可以调用派生类的函数。
cout<<p->area()<<endl;//调用派生类的函数,实现多态5
含有纯虚函数的类叫抽象类。
说明纯虚函数的一般格式:
class 类名
{
virtual 返回值类型 函数名(参数列表)=0;
}
1在纯函数中,不能提供出函数的具体实现,而是需要在派生类中再加以具体实现。
2,一个类中可以有多个纯虚函数。包含纯虚函数的类被称为抽象类。
3.一个抽象类只能作为基类来派生新类,而不能说明抽象类的对象和对象数组。
抽象类只能定义指针。 不能定义对象和数组。
4.可以说明抽象类对象的指针和引用。
5.从一个抽象派生的类必须提供纯虚函数的实现代码,或在该派生类中仍将它说明为纯虚函数。
6,在抽象类中,至少有一个虚函数是纯虚函数。
#ifndef _______Shape__
#define _______Shape__
#include <iostream>
class Shape
{
private:
int x;
int y;
public:
void setX(int _x,int _y);
int getX();
int getY();
virtual float area()=0;//纯虚函数。
};
#endif /* defined(_______Shape__) */
#include "Shape.h"
void Shape::setX(int _x, int _y)
{
x=_x;
y=_y;
}
int Shape::getX()
{
return x;
}
int Shape::getY()
{
return y;
}
#ifndef _______Rectangle__
#define _______Rectangle__
#include <iostream>
#include "Shape.h"
class Rectangle :public Shape
{
private:
int width;
int height;
public:
void setWH(int _w,int _h);
int getWidth();
int getHeigth();
float area();
};
#endif /* defined(_______Rectangle__) */
#include "Rectangle.h"
void Rectangle::setWH(int _w, int _h)
{
width=_w;
height=_h;
}
int Rectangle:: getWidth()
{
return width;
}
int Rectangle::getHeigth()
{
return height;
}
float Rectangle:: area()
{
return width*height;
}
#include <iostream>
#include "Rectangle.h"
#include "Shape.h"
using namespace std;
int main(int argc, const char * argv[])
{
Shape *p;
Rectangle rect;
rect.setWH(10, 20);
p=▭//加上virtual,基类可以调用派生类的函数。
cout<<p->area()<<endl;//调用派生类的函数,实现多态5
相关文章推荐
- C++纯虚函数 抽象类
- 31.C++纯虚函数与抽象类
- C++纯虚函数和抽象类
- C++纯虚函数与抽象类
- C++纯虚函数和抽象类的一些要点
- 25、C++纯虚函数与抽象类
- C++纯虚函数与抽象类
- c++纯虚函数和抽象类
- C++纯虚函数与抽象类
- (42)抽象类和抽象方法的应用(43)接口与面向对象中多态的应用
- java-计算几何图形的面积(抽象类)
- J2SE基础夯实系列之抽象类和接口的区别(太透彻了!)
- 抽象类的概念
- C#之美—抽象类与接口孽缘
- 共同学习Java源代码--数据结构--AbstractList抽象类(二)
- JAVA 接口与抽象类
- 抽象类abstract详解
- 简单理解接口和抽象类
- 抽象类是否可以继承实体类
- 抽象类与接口的区别