继承抽象类

2023-06-14,

#ifndef VIRTUAL1

#define VIRTUAL1

#include<iostream>

using namespace std;

class Number{

public:

Number(int i){ x = i; }

virtual void show() = 0;

protected:

int x;

};

class dec_type :public Number{//这里必须公有继承,否则派生类对象做实参无法传递给基类的

//引用对象。

public:

dec_type(int i) :Number(i){}

void show(){

cout << dec << x<<endl;

}

};

class hex_type:public Number{

public:

hex_type(int i) :Number(i){}

void show(){

cout << hex << x<<endl;

}

};

class oct_type :public Number{

public:

oct_type(int i) :Number(i){}

void show(){

cout << oct << x<<endl;

}

};

#endif

#include"vitual1.h"

void fun(Number &n){//抽象类可以做引用

n.show();

}

int main(){

oct_type oc(50);

fun(oc);//派生类对象做参数传给基类的引用

system("pause");

return 0;

}

《继承抽象类.doc》

下载本文的Word格式文档,以方便收藏与打印。