13.2 抽像类与体类(Abstract & Concrete Classes)

//13.2 抽像类与体类(Abstract & Concrete Classes)
//只要子类中尚有未被覆盖定义的纯虚函数,那么,子类相对于抽像基类的抽像状态不变
//该类继承结构由下列程序代码实现之:

#include <iostream>
class Display{
public:
	virtual void inint()=0;
	virtual void write(char* pStr)=0;
};//定义两个纯虚函数
class Monochrome : public Display
{
    virtual void inint();  //覆盖
	virtual void write(char* pStr); //覆盖
};
void Monochrome::inint(){ };
void Monochrome::write(char* pStr){
	std::cout<<"Monochrome: "<<pStr<<"\n";
};

class ColorAdapter : public Display
{
public:
	virtual void write(char* pStr); //覆盖 
};
void ColorAdapter::write(char* pStr)
{
    std::cout<<"ColorAdapter: "<<pStr<<"\n";
}

class SVGA : public ColorAdapter
{
public:
	virtual void inint();  //覆盖
};
void SVGA::inint(){ }

void g(Display* d)
{
    d->inint();
	d->write("hello.");
}
int main()
{
	std::cout<<"I am main\n";
	Monochrome mc;
	SVGA svga;
	g(&mc);
	g(&svga);
    system("pause");
	return 0;
}

  

posted @ 2012-02-12 22:28  简单--生活  阅读(558)  评论(0编辑  收藏  举报
简单--生活(CSDN)